forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect…
….get_annotations` (python#120270)
- Loading branch information
1 parent
d88a1f2
commit 42351c3
Showing
4 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Lib/test/test_inspect/inspect_stringized_annotations_pep695.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import annotations | ||
from typing import Callable, Unpack | ||
|
||
|
||
class A[T, *Ts, **P]: | ||
x: T | ||
y: tuple[*Ts] | ||
z: Callable[P, str] | ||
|
||
|
||
class B[T, *Ts, **P]: | ||
T = int | ||
Ts = str | ||
P = bytes | ||
x: T | ||
y: Ts | ||
z: P | ||
|
||
|
||
Eggs = int | ||
Spam = str | ||
|
||
|
||
class C[Eggs, **Spam]: | ||
x: Eggs | ||
y: Spam | ||
|
||
|
||
def generic_function[T, *Ts, **P]( | ||
x: T, *y: Unpack[Ts], z: P.args, zz: P.kwargs | ||
) -> None: ... | ||
|
||
|
||
def generic_function_2[Eggs, **Spam](x: Eggs, y: Spam): pass | ||
|
||
|
||
class D: | ||
Foo = int | ||
Bar = str | ||
|
||
def generic_method[Foo, **Bar]( | ||
self, x: Foo, y: Bar | ||
) -> None: ... | ||
|
||
def generic_method_2[Eggs, **Spam](self, x: Eggs, y: Spam): pass | ||
|
||
|
||
def nested(): | ||
from types import SimpleNamespace | ||
from inspect import get_annotations | ||
|
||
Eggs = bytes | ||
Spam = memoryview | ||
|
||
|
||
class E[Eggs, **Spam]: | ||
x: Eggs | ||
y: Spam | ||
|
||
def generic_method[Eggs, **Spam](self, x: Eggs, y: Spam): pass | ||
|
||
|
||
def generic_function[Eggs, **Spam](x: Eggs, y: Spam): pass | ||
|
||
|
||
return SimpleNamespace( | ||
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2024-06-08-15-15-29.gh-issue-114053.WQLAFG.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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. |