mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
gh-103406: Modernize pos-only arguments usage in test_signature
(#103407)
This commit is contained in:
parent
a3d313a9e2
commit
7569781176
1 changed files with 11 additions and 20 deletions
|
@ -3044,14 +3044,9 @@ class TestSignatureObject(unittest.TestCase):
|
|||
self.assertEqual(_foo(*ba.args, **ba.kwargs), (12, 10, 20))
|
||||
|
||||
|
||||
def foo(a, b, c, d, **kwargs):
|
||||
def foo(a, b, /, c, d, **kwargs):
|
||||
pass
|
||||
sig = inspect.signature(foo)
|
||||
params = sig.parameters.copy()
|
||||
params['a'] = params['a'].replace(kind=Parameter.POSITIONAL_ONLY)
|
||||
params['b'] = params['b'].replace(kind=Parameter.POSITIONAL_ONLY)
|
||||
foo.__signature__ = inspect.Signature(params.values())
|
||||
sig = inspect.signature(foo)
|
||||
self.assertEqual(str(sig), '(a, b, /, c, d, **kwargs)')
|
||||
|
||||
self.assertEqual(self.signature(partial(foo, 1)),
|
||||
|
@ -3556,14 +3551,9 @@ class TestSignatureObject(unittest.TestCase):
|
|||
P = inspect.Parameter
|
||||
S = inspect.Signature
|
||||
|
||||
def test(a_po, *, b, **kwargs):
|
||||
def test(a_po, /, *, b, **kwargs):
|
||||
return a_po, kwargs
|
||||
|
||||
sig = inspect.signature(test)
|
||||
new_params = list(sig.parameters.values())
|
||||
new_params[0] = new_params[0].replace(kind=P.POSITIONAL_ONLY)
|
||||
test.__signature__ = sig.replace(parameters=new_params)
|
||||
|
||||
self.assertEqual(str(inspect.signature(test)),
|
||||
'(a_po, /, *, b, **kwargs)')
|
||||
|
||||
|
@ -3593,6 +3583,14 @@ class TestSignatureObject(unittest.TestCase):
|
|||
self.assertEqual(sig.return_annotation, 42)
|
||||
self.assertEqual(sig, inspect.signature(test))
|
||||
|
||||
def test_signature_replaced(self):
|
||||
def test():
|
||||
pass
|
||||
|
||||
spam_param = inspect.Parameter('spam', inspect.Parameter.POSITIONAL_ONLY)
|
||||
sig = test.__signature__ = inspect.Signature(parameters=(spam_param,))
|
||||
self.assertEqual(sig, inspect.signature(test))
|
||||
|
||||
def test_signature_on_mangled_parameters(self):
|
||||
class Spam:
|
||||
def foo(self, __p1:1=2, *, __p2:2=3):
|
||||
|
@ -4157,16 +4155,9 @@ class TestSignatureBind(unittest.TestCase):
|
|||
def test_signature_bind_positional_only(self):
|
||||
P = inspect.Parameter
|
||||
|
||||
def test(a_po, b_po, c_po=3, foo=42, *, bar=50, **kwargs):
|
||||
def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
|
||||
return a_po, b_po, c_po, foo, bar, kwargs
|
||||
|
||||
sig = inspect.signature(test)
|
||||
new_params = collections.OrderedDict(tuple(sig.parameters.items()))
|
||||
for name in ('a_po', 'b_po', 'c_po'):
|
||||
new_params[name] = new_params[name].replace(kind=P.POSITIONAL_ONLY)
|
||||
new_sig = sig.replace(parameters=new_params.values())
|
||||
test.__signature__ = new_sig
|
||||
|
||||
self.assertEqual(self.call(test, 1, 2, 4, 5, bar=6),
|
||||
(1, 2, 4, 5, 6, {}))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue