Skip to content

[3.11] gh-103406: Modernize pos-only arguments usage in test_signature (GH-103407) #103536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,14 +2882,9 @@ def foo(a=1, b=2, c=3):
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)),
Expand Down Expand Up @@ -3394,14 +3389,9 @@ def test_signature_str_positional_only(self):
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)')

Expand Down Expand Up @@ -3431,6 +3421,14 @@ def test() -> 42:
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):
Expand Down Expand Up @@ -3962,16 +3960,9 @@ def test(a, *args, b, z=100, **kwargs):
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, {}))

Expand Down