| 1 |
from __future__ import annotations |
| 2 |
from typing import Callable, Unpack |
| 3 |
|
| 4 |
|
| 5 |
class A[T, *Ts, **P]: |
| 6 |
x: T |
| 7 |
y: tuple[*Ts] |
| 8 |
z: Callable[P, str] |
| 9 |
|
| 10 |
|
| 11 |
class B[T, *Ts, **P]: |
| 12 |
T = int |
| 13 |
Ts = str |
| 14 |
P = bytes |
| 15 |
x: T |
| 16 |
y: Ts |
| 17 |
z: P |
| 18 |
|
| 19 |
|
| 20 |
Eggs = int |
| 21 |
Spam = str |
| 22 |
|
| 23 |
|
| 24 |
class C[Eggs, **Spam]: |
| 25 |
x: Eggs |
| 26 |
y: Spam |
| 27 |
|
| 28 |
|
| 29 |
def generic_function[T, *Ts, **P]( |
| 30 |
x: T, *y: Unpack[Ts], z: P.args, zz: P.kwargs |
| 31 |
) -> None: ... |
| 32 |
|
| 33 |
|
| 34 |
def generic_function_2[Eggs, **Spam](x: Eggs, y: Spam): pass |
| 35 |
|
| 36 |
|
| 37 |
class D: |
| 38 |
Foo = int |
| 39 |
Bar = str |
| 40 |
|
| 41 |
def generic_method[Foo, **Bar]( |
| 42 |
self, x: Foo, y: Bar |
| 43 |
) -> None: ... |
| 44 |
|
| 45 |
def generic_method_2[Eggs, **Spam](self, x: Eggs, y: Spam): pass |
| 46 |
|
| 47 |
|
| 48 |
# Eggs is `int` in globals, a TypeVar in type_params, and `str` in locals: |
| 49 |
class E[Eggs]: |
| 50 |
Eggs = str |
| 51 |
x: Eggs |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def nested(): |
| 56 |
from types import SimpleNamespace |
| 57 |
from inspect import get_annotations |
| 58 |
|
| 59 |
Eggs = bytes |
| 60 |
Spam = memoryview |
| 61 |
|
| 62 |
|
| 63 |
class F[Eggs, **Spam]: |
| 64 |
x: Eggs |
| 65 |
y: Spam |
| 66 |
|
| 67 |
def generic_method[Eggs, **Spam](self, x: Eggs, y: Spam): pass |
| 68 |
|
| 69 |
|
| 70 |
def generic_function[Eggs, **Spam](x: Eggs, y: Spam): pass |
| 71 |
|
| 72 |
|
| 73 |
# Eggs is `int` in globals, `bytes` in the function scope, |
| 74 |
# a TypeVar in the type_params, and `str` in locals: |
| 75 |
class G[Eggs]: |
| 76 |
Eggs = str |
| 77 |
x: Eggs |
| 78 |
|
| 79 |
|
| 80 |
return SimpleNamespace( |
| 81 |
F=F, |
| 82 |
F_annotations=get_annotations(F, eval_str=True), |
| 83 |
F_meth_annotations=get_annotations(F.generic_method, eval_str=True), |
| 84 |
G_annotations=get_annotations(G, eval_str=True), |
| 85 |
generic_func=generic_function, |
| 86 |
generic_func_annotations=get_annotations(generic_function, eval_str=True) |
| 87 |
) |