Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-108901: Add Signature.from_code method#108902
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
566790c20875cb604f25bd69623bb3e7978efdb61777da9c6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2416,20 +2416,40 @@ def _signature_from_function(cls, func, skip_bound_arg=True, | ||
| s = getattr(func, "__text_signature__", None) | ||
| if s: | ||
| return _signature_fromstr(cls, func, s, skip_bound_arg) | ||
| return _signature_from_code(cls, | ||
| func.__code__, | ||
| globals=globals, | ||
| locals=locals, | ||
| eval_str=eval_str, | ||
| is_duck_function=is_duck_function, | ||
| func=func) | ||
| def _signature_from_code(cls, | ||
| func_code, | ||
| globals=None, | ||
| locals=None, | ||
| eval_str=False, | ||
| is_duck_function=False, | ||
| func=None): | ||
| """Private helper function to get signature for code objects.""" | ||
| Parameter = cls._parameter_cls | ||
| # Parameter information. | ||
| func_code = func.__code__ | ||
| pos_count = func_code.co_argcount | ||
| arg_names = func_code.co_varnames | ||
| posonly_count = func_code.co_posonlyargcount | ||
| positional = arg_names[:pos_count] | ||
| keyword_only_count = func_code.co_kwonlyargcount | ||
| keyword_only = arg_names[pos_count:pos_count + keyword_only_count] | ||
| annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str) | ||
| defaults = func.__defaults__ | ||
| kwdefaults = func.__kwdefaults__ | ||
| if func is not None: | ||
| annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str) | ||
| defaults = func.__defaults__ | ||
| kwdefaults = func.__kwdefaults__ | ||
| else: | ||
| annotations ={} | ||
| defaults = None | ||
| kwdefaults = None | ||
| if defaults: | ||
| pos_default_count = len(defaults) | ||
| @@ -3107,6 +3127,17 @@ def from_callable(cls, obj, *, | ||
| follow_wrapper_chains=follow_wrapped, | ||
| globals=globals, locals=locals, eval_str=eval_str) | ||
| @classmethod | ||
| def from_code(cls, co): | ||
| """Constructs Signature for the given code object. | ||
sobolevn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| Signatures created from code objects cannot know about annotations | ||
| or default values. | ||
| """ | ||
| if not iscode(co): | ||
| raise TypeError(f'code object was expected, got{type(co).__name__!r}') | ||
| return _signature_from_code(cls, co) | ||
| @property | ||
| def parameters(self): | ||
| return self._parameters | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3674,6 +3674,43 @@ def test_signature_on_noncallable_mocks(self): | ||
| with self.assertRaises(TypeError): | ||
| inspect.signature(mock) | ||
| def test_signature_from_code(self): | ||
| def func1( | ||
| a, b: int, | ||
| /, | ||
| c, d: str = 'a', | ||
| *args: int, | ||
| e, f: bool = True, | ||
| **kwargs: str, | ||
| ) -> float: | ||
| ... | ||
| def func2(a: str, b: int = 0, /): ... | ||
| def func3(): ... | ||
| def func4(*a, **k): ... | ||
| def func5(*, kw=False): ... | ||
| known_sigs ={ | ||
| func1: '(a, b, /, c, d, *args, e, f, **kwargs)', | ||
| func2: '(a, b, /)', | ||
| func3: '()', | ||
| func4: '(*a, **k)', | ||
| func5: '(*, kw)', | ||
| } | ||
Comment on lines +3693 to +3699 Member
| ||
| for test_func, expected_sig in known_sigs.items(): | ||
| with self.subTest(test_func=test_func, expected_sig=expected_sig): | ||
| self.assertEqual( | ||
| str(inspect.Signature.from_code(test_func.__code__)), | ||
| expected_sig, | ||
| ) | ||
| with self.assertRaisesRegex( | ||
| TypeError, | ||
| "code object was expected, got 'int'", | ||
| ): | ||
| inspect.Signature.from_code(1) | ||
| def test_signature_equality(self): | ||
| def foo(a, *, b:int) -> float: pass | ||
| self.assertFalse(inspect.signature(foo) == 42) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :meth:`inspect.Signature.from_code` to be able | ||
| to construct :class:`inspect.Signature` objects from :class:`types.CodeType`. |
Uh oh!
There was an error while loading. Please reload this page.