Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Type Parameter Syntax (python 3.12) #82

Open
tristanlatr opened this issue Nov 13, 2023 · 6 comments
Open

Add support for Type Parameter Syntax (python 3.12) #82

tristanlatr opened this issue Nov 13, 2023 · 6 comments

Comments

@tristanlatr
Copy link
Contributor

https://peps.python.org/pep-0695/

@tristanlatr
Copy link
Contributor Author

tristanlatr commented Nov 19, 2023

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.

    • If a def695 scope is immediately within a class scope, or within another def695 scope that is immediately within a class scope, then names defined in that class scope can be accessed within the def695 scope. (Regular functions, by contrast, cannot access names defined within an enclosing class scope.)
    • Names bound within def695 scopes cannot be rebound with a nonlocal statement in nested scopes.
    • yield, yield from await and the walrus operator cannot be used directly within a def695 scope.
  • 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().

@tristanlatr
Copy link
Contributor Author

I'm working on a patch.

@tristanlatr
Copy link
Contributor Author

As if python scoping rules were not already complicated enough…

@tristanlatr
Copy link
Contributor Author

Here is an implementation: 1bce48d

@tristanlatr
Copy link
Contributor Author

This feature is not particularly high priority since this syntax should not be used in stub files until python 3.11 reaches end-of-life (2027-10). Sot that give us some time to come up with a better implementation ;)

@tristanlatr
Copy link
Contributor Author

Relevant corder case: https://github.com/microsoft/pyright/pull/9821/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant