Skip to content

Type variable has no meaning in variable type annotation #9826

Closed Answered by erictraut
injust asked this question in Q&A
Discussion options

You must be logged in to vote

This behavior is mandated by the Python typing spec. A type variable is not valid unless it is associated with a generic class, function, or type alias declaration. You're using the type variable here for a variable declaration, which isn't a valid context for a type variable.

Historically, type variable scoping in Python has been very confusing for users, so you're not alone here. PEP 695 (which was introduced in Python 3.12) modernizes the syntax and eliminates most of the confusion around scoping because the scope is explicit with the new syntax.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@erictraut
Comment options

Answer selected by injust
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants