Question presented to candidate:
"Python 3.12 lets you write class Box[T]: and type Pair[T] = tuple[T, T]. What does that replace, what actually changes at runtime, and why is it more than just shorter syntax?"
What a strong answer should cover:
- PEP 695 (Python 3.12) adds type parameter lists:
def first[T](xs: list[T]) -> T,class Box[T]:, with noTypeVarimport and noGeneric[T]base (the class still getsGenericin its MRO) - Bounds and constraints are written inline:
[N: float],[S: (str, bytes)]; variance is inferred by the checker instead of declared withcovariant=True - The
typestatement creates aTypeAliasTypewhose value is evaluated lazily, so recursive and forward-referencing aliases need no quotes;typeis a soft keyword - Parameters live in their own annotation scope: they do not leak into the module, and bounds are evaluated only when accessed (
__type_params__,__bound__,__value__) - Python 3.13 adds defaults (
class Response[Body = str], PEP 696); the old and new styles cannot be mixed in one class, and an alias is not a class (isinstancerejects it)
Code / implementation expected: Optional β rewrite an old TypeVar / Generic class and a TypeAlias in the new syntax, and say which Python version each needs.