mirror of
https://github.com/python/cpython.git
synced 2025-10-21 14:12:27 +00:00
Issue #19611: handle implicit parameters in inspect.signature
inspect.signature now reports the implicit ``.0`` parameters generated by the compiler for comprehension and generator expression scopes as if they were positional-only parameters called ``implicit0``. Patch by Jelle Zijlstra.
This commit is contained in:
parent
d62548afed
commit
b4b966ece2
5 changed files with 56 additions and 0 deletions
|
@ -2903,6 +2903,10 @@ class TestParameterObject(unittest.TestCase):
|
|||
'is not a valid parameter name'):
|
||||
inspect.Parameter('$', kind=inspect.Parameter.VAR_KEYWORD)
|
||||
|
||||
with self.assertRaisesRegex(ValueError,
|
||||
'is not a valid parameter name'):
|
||||
inspect.Parameter('.a', kind=inspect.Parameter.VAR_KEYWORD)
|
||||
|
||||
with self.assertRaisesRegex(ValueError, 'cannot have default values'):
|
||||
inspect.Parameter('a', default=42,
|
||||
kind=inspect.Parameter.VAR_KEYWORD)
|
||||
|
@ -2986,6 +2990,17 @@ class TestParameterObject(unittest.TestCase):
|
|||
with self.assertRaisesRegex(TypeError, 'name must be a str'):
|
||||
inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY)
|
||||
|
||||
@cpython_only
|
||||
def test_signature_parameter_implicit(self):
|
||||
with self.assertRaisesRegex(ValueError,
|
||||
'implicit arguments must be passed in as'):
|
||||
inspect.Parameter('.0', kind=inspect.Parameter.POSITIONAL_ONLY)
|
||||
|
||||
param = inspect.Parameter(
|
||||
'.0', kind=inspect.Parameter.POSITIONAL_OR_KEYWORD)
|
||||
self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_ONLY)
|
||||
self.assertEqual(param.name, 'implicit0')
|
||||
|
||||
def test_signature_parameter_immutability(self):
|
||||
p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY)
|
||||
|
||||
|
@ -3234,6 +3249,17 @@ class TestSignatureBind(unittest.TestCase):
|
|||
ba = sig.bind(args=1)
|
||||
self.assertEqual(ba.arguments, {'kwargs': {'args': 1}})
|
||||
|
||||
@cpython_only
|
||||
def test_signature_bind_implicit_arg(self):
|
||||
# Issue #19611: getcallargs should work with set comprehensions
|
||||
def make_set():
|
||||
return {z * z for z in range(5)}
|
||||
setcomp_code = make_set.__code__.co_consts[1]
|
||||
setcomp_func = types.FunctionType(setcomp_code, {})
|
||||
|
||||
iterator = iter(range(5))
|
||||
self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
|
||||
|
||||
|
||||
class TestBoundArguments(unittest.TestCase):
|
||||
def test_signature_bound_arguments_unhashable(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue