1
1
"""
2
+ Tests for function/method signatures compliance
3
+
2
4
We're not interested in being 100% strict - instead we focus on areas which
3
5
could affect interop, e.g. with
4
6
@@ -20,7 +22,7 @@ def squeeze(x, /, axis):
20
22
"""
21
23
from inspect import Parameter , Signature , signature
22
24
from types import FunctionType
23
- from typing import Any , Callable , Dict , List , Literal , Sequence , get_args
25
+ from typing import Any , Callable , Dict , List , Literal , get_args
24
26
25
27
import pytest
26
28
from hypothesis import given , note , settings
@@ -67,10 +69,7 @@ def _test_inspectable_func(sig: Signature, stub_sig: Signature):
67
69
param = params [i ]
68
70
69
71
# We're not interested in the name if it isn't actually used
70
- if stub_param .kind not in [
71
- Parameter .POSITIONAL_ONLY ,
72
- * VAR_KINDS ,
73
- ]:
72
+ if stub_param .kind not in [Parameter .POSITIONAL_ONLY , * VAR_KINDS ]:
74
73
assert (
75
74
param .name == stub_param .name
76
75
), f"Expected argument '{ param .name } ' to be named '{ stub_param .name } '"
@@ -100,7 +99,7 @@ def get_dtypes_strategy(func_name: str) -> st.SearchStrategy[DataType]:
100
99
return xps .scalar_dtypes ()
101
100
102
101
103
- def make_pretty_func (func_name : str , args : Sequence [ Any ], kwargs : Dict [ str , Any ] ):
102
+ def make_pretty_func (func_name : str , * args : Any , ** kwargs : Any ):
104
103
f_sig = f"{ func_name } ("
105
104
f_sig += ", " .join (str (a ) for a in args )
106
105
if len (kwargs ) != 0 :
@@ -129,14 +128,18 @@ def _test_uninspectable_func(
129
128
"and is too troublesome to test for otherwise"
130
129
)
131
130
if func_name in [
131
+ # 0d shapes
132
132
"__bool__" ,
133
133
"__int__" ,
134
134
"__index__" ,
135
135
"__float__" ,
136
+ # x2 elements must be >=0
136
137
"pow" ,
137
138
"bitwise_left_shift" ,
138
139
"bitwise_right_shift" ,
140
+ # axis default invalid with 0d shapes
139
141
"sort" ,
142
+ # shape requirements
140
143
* matrixy_names ,
141
144
]:
142
145
pytest .skip (skip_msg )
@@ -176,15 +179,15 @@ def _test_uninspectable_func(
176
179
kwargs : Dict [str , Any ] = {
177
180
p .name : v for p , v in param_to_value .items () if p .kind == Parameter .KEYWORD_ONLY
178
181
}
179
- f_func = make_pretty_func (func_name , args , kwargs )
182
+ f_func = make_pretty_func (func_name , * args , ** kwargs )
180
183
note (f"trying { f_func } " )
181
184
func (* args , ** kwargs )
182
185
183
186
184
187
def _test_func_signature (func : Callable , stub : FunctionType , array = None ):
185
188
stub_sig = signature (stub )
186
189
# If testing against array, ignore 'self' arg in stub as it won't be present
187
- # in func (which should be an array method).
190
+ # in func (which should be a method).
188
191
if array is not None :
189
192
stub_params = list (stub_sig .parameters .values ())
190
193
del stub_params [0 ]
0 commit comments