| 
									
										
										
										
											2024-06-13 23:41:14 +02:00
										 |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-15 15:16:40 +02:00
										 |  |  | # Eggs is `int` in globals, a TypeVar in type_params, and `str` in locals: | 
					
						
							|  |  |  | class E[Eggs]: | 
					
						
							|  |  |  |     Eggs = str | 
					
						
							|  |  |  |     x: Eggs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 23:41:14 +02:00
										 |  |  | def nested(): | 
					
						
							|  |  |  |     from types import SimpleNamespace | 
					
						
							|  |  |  |     from inspect import get_annotations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Eggs = bytes | 
					
						
							|  |  |  |     Spam = memoryview | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-15 15:16:40 +02:00
										 |  |  |     class F[Eggs, **Spam]: | 
					
						
							| 
									
										
										
										
											2024-06-13 23:41:14 +02:00
										 |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-15 15:16:40 +02:00
										 |  |  |     # Eggs is `int` in globals, `bytes` in the function scope, | 
					
						
							|  |  |  |     # a TypeVar in the type_params, and `str` in locals: | 
					
						
							|  |  |  |     class G[Eggs]: | 
					
						
							|  |  |  |         Eggs = str | 
					
						
							|  |  |  |         x: Eggs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 23:41:14 +02:00
										 |  |  |     return SimpleNamespace( | 
					
						
							| 
									
										
										
										
											2024-06-15 15:16:40 +02:00
										 |  |  |         F=F, | 
					
						
							|  |  |  |         F_annotations=get_annotations(F, eval_str=True), | 
					
						
							|  |  |  |         F_meth_annotations=get_annotations(F.generic_method, eval_str=True), | 
					
						
							|  |  |  |         G_annotations=get_annotations(G, eval_str=True), | 
					
						
							| 
									
										
										
										
											2024-06-13 23:41:14 +02:00
										 |  |  |         generic_func=generic_function, | 
					
						
							|  |  |  |         generic_func_annotations=get_annotations(generic_function, eval_str=True) | 
					
						
							|  |  |  |     ) |