Skip to content
Merged
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
83 changes: 42 additions & 41 deletions Lib/annotationlib.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -939,48 +939,49 @@ def get_annotations(
ifnoteval_str:
returndict(ann)

ifisinstance(obj, type):
# class
obj_globals=None
module_name=getattr(obj, "__module__", None)
ifmodule_name:
module=sys.modules.get(module_name, None)
ifmodule:
obj_globals=getattr(module, "__dict__", None)
obj_locals=dict(vars(obj))
unwrap=obj
elifisinstance(obj, types.ModuleType):
# module
obj_globals=getattr(obj, "__dict__")
obj_locals=None
unwrap=None
elifcallable(obj):
# this includes types.Function, types.BuiltinFunctionType,
# types.BuiltinMethodType, functools.partial, functools.singledispatch,
# "class funclike" from Lib/test/test_inspect... on and on it goes.
obj_globals=getattr(obj, "__globals__", None)
obj_locals=None
unwrap=obj
else:
obj_globals=obj_locals=unwrap=None

ifunwrapisnotNone:
whileTrue:
ifhasattr(unwrap, "__wrapped__"):
unwrap=unwrap.__wrapped__
continue
iffunctools:=sys.modules.get("functools"):
ifisinstance(unwrap, functools.partial):
unwrap=unwrap.func
ifglobalsisNoneorlocalsisNone:
ifisinstance(obj, type):
# class
obj_globals=None
module_name=getattr(obj, "__module__", None)
ifmodule_name:
module=sys.modules.get(module_name, None)
ifmodule:
obj_globals=getattr(module, "__dict__", None)
obj_locals=dict(vars(obj))
unwrap=obj
elifisinstance(obj, types.ModuleType):
# module
obj_globals=getattr(obj, "__dict__")
obj_locals=None
unwrap=None
elifcallable(obj):
# this includes types.Function, types.BuiltinFunctionType,
# types.BuiltinMethodType, functools.partial, functools.singledispatch,
# "class funclike" from Lib/test/test_inspect... on and on it goes.
obj_globals=getattr(obj, "__globals__", None)
obj_locals=None
unwrap=obj
else:
obj_globals=obj_locals=unwrap=None

ifunwrapisnotNone:
whileTrue:
ifhasattr(unwrap, "__wrapped__"):
unwrap=unwrap.__wrapped__
continue
break
ifhasattr(unwrap, "__globals__"):
obj_globals=unwrap.__globals__

ifglobalsisNone:
globals=obj_globals
iflocalsisNone:
locals=obj_locals
iffunctools:=sys.modules.get("functools"):
ifisinstance(unwrap, functools.partial):
unwrap=unwrap.func
continue
break
ifhasattr(unwrap, "__globals__"):
obj_globals=unwrap.__globals__

ifglobalsisNone:
globals=obj_globals
iflocalsisNone:
locals=obj_locals

# "Inject" type parameters into the local namespace
# (unless they are shadowed by assignments *in* the local namespace),
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Only fetch globals and locals if necessary in
:func:`annotationlib.get_annotations`