Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

[3.12] gh-114053: Fix bad interaction of PEP 695, PEP 563 andinspect.get_annotations (GH-120270)#120475

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
8 changes: 7 additions & 1 deletionLib/inspect.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -280,7 +280,13 @@ def get_annotations(obj, *, globals=None, locals=None, eval_str=False):
ifglobalsisNone:
globals=obj_globals
iflocalsisNone:
locals=obj_locals
locals=obj_localsor {}

# "Inject" type parameters into the local namespace
# (unless they are shadowed by assignments *in* the local namespace),
# as a way of emulating annotation scopes when calling `eval()`
iftype_params:=getattr(obj,"__type_params__", ()):
locals= {param.__name__:paramforparamintype_params}|locals

return_value= {key:
valueifnotisinstance(value,str)elseeval(value,globals,locals)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
from __future__importannotations
fromtypingimportCallable,Unpack


classA[T,*Ts,**P]:
x:T
y:tuple[*Ts]
z:Callable[P,str]


classB[T,*Ts,**P]:
T=int
Ts=str
P=bytes
x:T
y:Ts
z:P


Eggs=int
Spam=str


classC[Eggs,**Spam]:
x:Eggs
y:Spam


defgeneric_function[T,*Ts,**P](
x:T,*y:Unpack[Ts],z:P.args,zz:P.kwargs
)->None: ...


defgeneric_function_2[Eggs,**Spam](x:Eggs,y:Spam):pass


classD:
Foo=int
Bar=str

defgeneric_method[Foo,**Bar](
self,x:Foo,y:Bar
)->None: ...

defgeneric_method_2[Eggs,**Spam](self,x:Eggs,y:Spam):pass


defnested():
fromtypesimportSimpleNamespace
frominspectimportget_annotations

Eggs=bytes
Spam=memoryview


classE[Eggs,**Spam]:
x:Eggs
y:Spam

defgeneric_method[Eggs,**Spam](self,x:Eggs,y:Spam):pass


defgeneric_function[Eggs,**Spam](x:Eggs,y:Spam):pass


returnSimpleNamespace(
E=E,
E_annotations=get_annotations(E,eval_str=True),
E_meth_annotations=get_annotations(E.generic_method,eval_str=True),
generic_func=generic_function,
generic_func_annotations=get_annotations(generic_function,eval_str=True)
)
103 changes: 103 additions & 0 deletionsLib/test/test_inspect/test_inspect.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,6 +17,7 @@
importsys
importtypes
importtextwrap
fromtypingimportUnpack
importunicodedata
importunittest
importunittest.mock
Expand All@@ -40,6 +41,7 @@
fromtest.test_inspectimportinspect_stock_annotations
fromtest.test_inspectimportinspect_stringized_annotations
fromtest.test_inspectimportinspect_stringized_annotations_2
fromtest.test_inspectimportinspect_stringized_annotations_pep695


# Functions tested in this suite:
Expand DownExpand Up@@ -1505,6 +1507,107 @@ def wrapper(a, b):
self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations), {'x':'mytype'})
self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations,eval_str=True), {'x':int})

deftest_pep695_generic_class_with_future_annotations(self):
ann_module695=inspect_stringized_annotations_pep695
A_annotations=inspect.get_annotations(ann_module695.A,eval_str=True)
A_type_params=ann_module695.A.__type_params__
self.assertIs(A_annotations["x"],A_type_params[0])
self.assertEqual(A_annotations["y"].__args__[0],Unpack[A_type_params[1]])
self.assertIs(A_annotations["z"].__args__[0],A_type_params[2])

deftest_pep695_generic_class_with_future_annotations_and_local_shadowing(self):
B_annotations=inspect.get_annotations(
inspect_stringized_annotations_pep695.B,eval_str=True
)
self.assertEqual(B_annotations, {"x":int,"y":str,"z":bytes})

deftest_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self):
ann_module695=inspect_stringized_annotations_pep695
C_annotations=inspect.get_annotations(ann_module695.C,eval_str=True)
self.assertEqual(
set(C_annotations.values()),
set(ann_module695.C.__type_params__)
)

deftest_pep_695_generic_function_with_future_annotations(self):
ann_module695=inspect_stringized_annotations_pep695
generic_func_annotations=inspect.get_annotations(
ann_module695.generic_function,eval_str=True
)
func_t_params=ann_module695.generic_function.__type_params__
self.assertEqual(
generic_func_annotations.keys(), {"x","y","z","zz","return"}
)
self.assertIs(generic_func_annotations["x"],func_t_params[0])
self.assertEqual(generic_func_annotations["y"],Unpack[func_t_params[1]])
self.assertIs(generic_func_annotations["z"].__origin__,func_t_params[2])
self.assertIs(generic_func_annotations["zz"].__origin__,func_t_params[2])

deftest_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self):
self.assertEqual(
set(
inspect.get_annotations(
inspect_stringized_annotations_pep695.generic_function_2,
eval_str=True
).values()
),
set(
inspect_stringized_annotations_pep695.generic_function_2.__type_params__
)
)

deftest_pep_695_generic_method_with_future_annotations(self):
ann_module695=inspect_stringized_annotations_pep695
generic_method_annotations=inspect.get_annotations(
ann_module695.D.generic_method,eval_str=True
)
params= {
param.__name__:param
forparaminann_module695.D.generic_method.__type_params__
}
self.assertEqual(
generic_method_annotations,
{"x":params["Foo"],"y":params["Bar"],"return":None}
)

deftest_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self):
self.assertEqual(
set(
inspect.get_annotations(
inspect_stringized_annotations_pep695.D.generic_method_2,
eval_str=True
).values()
),
set(
inspect_stringized_annotations_pep695.D.generic_method_2.__type_params__
)
)

deftest_pep_695_generics_with_future_annotations_nested_in_function(self):
results=inspect_stringized_annotations_pep695.nested()

self.assertEqual(
set(results.E_annotations.values()),
set(results.E.__type_params__)
)
self.assertEqual(
set(results.E_meth_annotations.values()),
set(results.E.generic_method.__type_params__)
)
self.assertNotEqual(
set(results.E_meth_annotations.values()),
set(results.E.__type_params__)
)
self.assertEqual(
set(results.E_meth_annotations.values()).intersection(results.E.__type_params__),
set()
)

self.assertEqual(
set(results.generic_func_annotations.values()),
set(results.generic_func.__type_params__)
)


classTestFormatAnnotation(unittest.TestCase):
deftest_typing_replacement(self):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
Fix erroneous:exc:`NameError` when calling:func:`inspect.get_annotations`
with ``eval_str=True``` on a class that made use of:pep:`695` type
parameters in a module that had ``from __future__ import annotations`` at
the top of the file. Patch by Alex Waygood.

[8]ページ先頭

©2009-2025 Movatter.jp