Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[3.14] gh-132493: Remove __annotations__ usage in inspect._signature_is_functionlike (GH-133415) #133796

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

Open
wants to merge 1 commit into
base: 3.14
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions 4 Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2074,13 +2074,11 @@ def _signature_is_functionlike(obj):
code = getattr(obj, '__code__', None)
defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
annotations = getattr(obj, '__annotations__', None)

return (isinstance(code, types.CodeType) and
isinstance(name, str) and
(defaults is None or isinstance(defaults, tuple)) and
(kwdefaults is None or isinstance(kwdefaults, dict)) and
(isinstance(annotations, (dict)) or annotations is None) )
(kwdefaults is None or isinstance(kwdefaults, dict)))


def _signature_strip_non_python_syntax(signature):
Expand Down
31 changes: 31 additions & 0 deletions 31 Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,37 @@ def test_signature_annotation_format(self):
with self.assertRaisesRegex(NameError, "undefined"):
signature_func(ida.f)

def test_signature_deferred_annotations(self):
def f(x: undef):
pass

class C:
x: undef

def __init__(self, x: undef):
self.x = x

sig = inspect.signature(f, annotation_format=Format.FORWARDREF)
self.assertEqual(list(sig.parameters), ['x'])
sig = inspect.signature(C, annotation_format=Format.FORWARDREF)
self.assertEqual(list(sig.parameters), ['x'])

class CallableWrapper:
def __init__(self, func):
self.func = func
self.__annotate__ = func.__annotate__

def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)

@property
def __annotations__(self):
return self.__annotate__(Format.VALUE)

cw = CallableWrapper(f)
sig = inspect.signature(cw, annotation_format=Format.FORWARDREF)
self.assertEqual(list(sig.parameters), ['args', 'kwargs'])

def test_signature_none_annotation(self):
class funclike:
# Has to be callable, and have correct
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid accessing ``__annotations__`` unnecessarily in
:func:`inspect.signature`.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.