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
| Name | Type | Description |
|---|---|---|
| name | str | Name of the static subtype. |
| tp | type | Underlying 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))