PyDocs

Nav

Development Tools

NewType

typing

class · Added in 3.5.2 (source)
  • NewType(name, tp)
NewType creates simple unique types with almost zero runtime overhead.

NewType(name, tp) is considered a subtype of tp
by static type checkers. At runtime, NewType(name, tp) returns
a dummy callable that simply returns its argument.

Usage::

    UserId = NewType('UserId', int)

    def name_by_id(user_id: UserId) -> str:
        ...

    UserId('user')          # Fails type check

    name_by_id(42)          # Fails type check
    name_by_id(UserId(42))  # OK

    num = UserId(5) + 1     # type: int
Changed in 3.10
  • 3.10: NewType is now a class rather than a function.
Members of NewType

_

__module__
The name of the module in which the new type is defined.
__name__
The name of the new type.
__supertype__
The type that the new type is based on.
0 Examples
No examples for typing/NewType.
See Also
No see-alsos for typing/NewType
0 Notes
No notes for NewType