Is `Annotated` compatible with `type[T]`?

The change improves type safety in functions that take type arguments. If you receive an argument of type type, it should be safe to do things with it that you can normally do with types, such as passing it as the second argument to issubclass(). However, Annotated[int, ...] is not valid there.

I do agree that it’s worth it for type checkers to be conservative in making changes here, at least until TypeForm is available. Mypy’s behavior is already very inconsistent here though, so it would be hard to justify retaining this exact behavior:

from typing import Annotated, TypeVar

T = TypeVar("T")

def takes_type(t: type[T]) -> T:
    return t()

AnnotatedOptInt = Annotated[int | None, ""]
takes_type(AnnotatedOptInt)  # Error

AnnotatedInt = Annotated[int, ""]
takes_type(AnnotatedInt)  # No error
takes_type(Annotated[int, ""])  # Error
1 Like