typing.NewType
E911260
typing.NewType is a Python typing construct that allows you to create distinct, type-checked aliases of existing types without runtime overhead.
Statements (47)
| Predicate | Object |
|---|---|
| instanceOf |
Python typing construct
ⓘ
function ⓘ |
| advantageOverSubclassing | no additional runtime cost ⓘ |
| alternative | subclassing built-in types for runtime distinction ⓘ |
| availableFrom | typing.NewType ⓘ |
| availableIn | Python standard library ⓘ |
| category | static typing feature ⓘ |
| commonPattern | NewType for IDs, tokens, and other opaque identifiers ⓘ |
| createdTypeAttribute | __supertype__ ⓘ |
| createdTypeInstanceOf | function that returns underlying value ⓘ |
| createdTypeIsSubclassOf | underlying type for static type checkers only ⓘ |
| createsDistinctType | true ⓘ |
| definedInModule | typing ⓘ |
| definedInPEP | PEP 484 NERFINISHED ⓘ |
| designGoal | zero-cost abstraction at runtime ⓘ |
| discouragedPattern | nesting NewType over another NewType ⓘ |
| distinguishesFrom | simple type aliases ⓘ |
| documentationLocation | Python standard library typing module docs ⓘ |
| exampleSignature | def NewType(name: str, tp: Type[_T]) -> Type[_T]: ... ⓘ |
| exampleUse | UserId and ProductId both based on int but not interchangeable for type checkers ⓘ |
| hasNoRuntimeOverhead | true GENERATED ⓘ |
| introducedInPythonVersion | 3.5 ⓘ |
| isErasedAtRuntime | true ⓘ |
| language | Python NERFINISHED ⓘ |
| limitations | no runtime validation of values ⓘ |
| parameter |
name
ⓘ
tp ⓘ |
| purpose | create distinct type-checked aliases of existing types ⓘ |
| relatedTo |
mypy
NERFINISHED
ⓘ
typing.Annotated ⓘ typing.TypeAlias ⓘ |
| requires | from typing import NewType ⓘ |
| returns | a callable that behaves like the underlying type at runtime ⓘ |
| runtimeBehavior | returns the underlying value unchanged at runtime ⓘ |
| runtimeType | underlying type, e.g. int for NewType('UserId', int) ⓘ |
| semantics | nominal typing over an existing structural type ⓘ |
| simpleAliasComparison | type aliases are only synonyms and not distinct types for type checkers ⓘ |
| staticTypeChecking | enforced by type checkers only ⓘ |
| status | stable ⓘ |
| supportedBy |
Pyre
NERFINISHED
ⓘ
mypy ⓘ pyright ⓘ |
| supportsIntrospection | has __supertype__ attribute in created types GENERATED ⓘ |
| syntaxExample | UserId = NewType('UserId', int) ⓘ |
| useCase |
prevent mixing of semantically different but structurally identical types
ⓘ
strongly type domain-specific identifiers ⓘ |
| worksWith | functions, methods, and variables annotations ⓘ |
Referenced by (1)
Full triples — surface form annotated when it differs from this entity's canonical label.