Skip to content
Discussion options

You must be logged in to vote

I think the behaviour you're witnessing is correct, because the PEP 695 syntax introduces a new annotation scope.

In other words, T in class BB(Generic[T]): ... (with the old syntax) is indeed a reference to T = TypeVar("T", int, bytes), but the T inside the body of AA is a different symbol.

The class AA[T]: declaration defines a new TypeVar that, because it's also named T, shadows the one defined at module scope - and because this new T does not have the (int, bytes) bound, the instantiation AA("aa") type checks.

You can think of your example as if it was roughly equivalent to the following:

from typing import TypeVar, Generic

T = TypeVar("T", int, bytes)


def _aa():
    T = TypeVar("T")…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mtsokol
Comment options

@RBerga06
Comment options

Answer selected by mtsokol
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