Open tristanlatr opened 10 months ago
Here are the relevant parts of the specifications:
When the new syntax is used, a new lexical scope is introduced (called def695 scope), and this scope includes the type parameters. Type parameters can be accessed by name within inner scopes. As with other symbols in Python, an inner scope can define its own symbol that overrides an outer-scope symbol of the same name.
the new scopes behave like function scopes, except for a number of minor differences listed below.
Eagerly evaluated values: The type parameters of generic type aliases The type parameters and annotations of generic functions The type parameters and base class expressions of generic classes
Lazily evaluated values: The value of generic type aliases The bounds of type variables The constraints of type variables
Type parameters are visible to other type parameters declared elsewhere in the list. This allows type parameters to use other type parameters within their definition.
A compiler error or runtime exception is generated if the definition of an earlier type parameter references a later type parameter even if the name is defined in an outer scope.
Type parameters declared using the new syntax will not appear within the dictionary returned by globals() or locals().
I'm working on a patch.
As if python scoping rules were not already complicated enough…
Here is an implementation: https://github.com/serge-sans-paille/beniget/commit/1bce48d9b652b045c0c4d57730f898395d4c4c36
https://peps.python.org/pep-0695/