Commit 4eab6df
committed
gh-79644: Fix create_autospec signature handling for functools partial and partialmethod
Fixes both cases reported in gh-79644:
1. functools.partial: _get_signature_object() fell through to inspecting
func.__call__ for anything that wasn't a plain function / method. For
a functools.partial, that resolves to the C slot wrapper's own generic
"(*args, **kwargs)" signature rather than the partial's effective,
post-partial-application one. inspect.signature() already has
first-class support for computing a functools.partial's effective
signature directly; skip the func.__call__ detour and let it do so.
2. functools.partialmethod: _must_skip() special-cases
staticmethod / classmethod / plain functions to decide whether `self`
should be dropped from an autospecced method's signature, but had no
branch for functools.partialmethod. The raw partialmethod object
stored in klass.__dict__ matched none of those cases and fell through
to the "not a method" branch, always returning False regardless of
`is_type`. The resolved attribute used to build the child mock's
signature (obtained via getattr(), which goes through the descriptor
protocol) is a plain function whose signature still includes `self`
when accessed unbound at the class level, so without this fix self's
position was checked against the partialmethod's own pre-bound
argument instead of being skipped.
Signed-off-by: Claudiu Belu <cbelu@cloudbasesolutions.com>1 parent c98e984 commit 4eab6df
3 files changed
Lines changed: 62 additions & 2 deletions
File tree
- Lib
- test/test_unittest/testmock
- unittest
- Misc/NEWS.d/next/Library
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
954 | 955 | | |
955 | 956 | | |
956 | 957 | | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
957 | 997 | | |
958 | 998 | | |
959 | 999 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
110 | 118 | | |
111 | 119 | | |
112 | 120 | | |
| |||
2917 | 2925 | | |
2918 | 2926 | | |
2919 | 2927 | | |
2920 | | - | |
| 2928 | + | |
2921 | 2929 | | |
2922 | 2930 | | |
| 2931 | + | |
| 2932 | + | |
| 2933 | + | |
2923 | 2934 | | |
2924 | 2935 | | |
2925 | 2936 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
0 commit comments