Under The Hood
backend python CPython 3.12+

Typing Engine: TypeVar, Generic, Protocol, and get_type_hints

Last updated
Prerequisites:
Python Type Annotations (PEP 484)
OOP Abstract Base Classes
  • python
  • typing
  • typevar
  • protocol
  • generics
  • structural-typing
  • pydantic

Read at your depth

The practical view

Python type hints (PEP 484) provide static typing for IDEs and linters (Mypy, Pyright). In modern Python (3.12+), you can define generics using the native syntax `class Stack[T]:` (replacing `TypeVar('T')`). `typing.Protocol` enables **Structural Subtyping** (static duck typing): any class that implements the methods declared in the protocol matches the type without explicit inheritance. `typing.get_type_hints()` allows runtime inspection of annotations.

The same idea in other frameworks

python equivalent
`class Renderable(Protocol): def render(self) -> str: ...`.

Legacy vs modern

Nominal Inheritance (ABC) vs Structural Typing (Protocol)

ABCs require rigid class inheritance; `Protocol` allows any class with matching methods to satisfy the type statically.

before → after
Nominal ABC Inheritance
class Renderer(ABC): @abstractmethod def render(self): ... // ❌ Forces explicit subclassing
Structural Protocol Typing (PEP 544)
class Renderer(Protocol): def render(self) -> str: ... // ✅ NEW: Static duck typing

Interview gotchas

Context

Interviewers test your knowledge of annotation evaluation performance and runtime reflection.

The mechanical answer

Normally, type annotations are evaluated as live Python expressions when the module loads (meaning `x: SomeClass` requires `SomeClass` to already be defined). With `from __future__ import annotations`, **all type annotations are stored as raw strings in `__annotations__` without being evaluated**. This prevents circular import errors and speeds up module load times, but requires runtime libraries (like Pydantic v1) to use `typing.get_type_hints()` to evaluate the strings.

Trap

If using stringified annotations, accessing `obj.__annotations__['x']` directly returns the string `'SomeClass'` instead of the class object; always use `typing.get_type_hints()`.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.