Typing and Generics

typing.NewType

Creates a distinct static subtype of an existing type for preventing accidental mixing of domain values. Calling it returns the identical input object at runtime, not a wrapper or validation result.

Formal Syntax & Structure

python syntaxpython
typing.NewType(name, tp)

Parameters & Attributes

NameTypeDescription
namestrName of the static subtype.
tptypeUnderlying type.

Return Value

NewType callable

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import NewType
UserId=NewType('UserId',int)value=int('12345')print((UserId(value) is value, UserId.__supertype__ is int))
Python Reference