Skip to main content
typing.NewType is a utility used to create distinct, strongly-typed aliases for existing types that are enforced exclusively by static type checkers. At runtime, a NewType is completely transparent, introduces zero overhead, and resolves directly to its original base type.

Syntax

Static Type Checker Behavior

To static type analyzers (like mypy or pyright), NewType creates a unidirectional inheritance relationship. The analyzer treats the NewType as a subclass of the base type, enforcing standard subtyping rules (the Liskov Substitution Principle).
  1. Subtyping: A NewType can be passed to any function expecting the base type, just as a subclass can be used in place of its base class.
  2. Strictness: The base type cannot be passed to a function expecting the NewType.
  3. Operation resolution: Performing standard operations (like arithmetic) on a NewType yields the base type, not the NewType.

Runtime Behavior

At runtime, NewType does not create a new class or allocate new memory structures.
  • Pre-Python 3.10: NewType returned a standard nested function: def new_type(x): return x.
  • Python 3.10+: NewType returns an instance of the typing.NewType class, which implements a __call__ method that acts as an identity function, returning the exact object passed to it.
Because it does not generate a true Python class, it is subject to strict runtime limitations:

NewType vs. TypeAlias

It is critical to distinguish NewType from standard type aliases.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More