Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

typing.NewType

Create a distinct static subtype of an existing type, useful for separating identifiers with the same representation.

Version: Python 3.14; repository image, production runtime not attested

Syntax

UserId = NewType("UserId", int)

Create a distinct static subtype of an existing type, useful for separating identifiers with the same representation.

A user identifier can stand in for an integer, while a plain integer does not automatically satisfy the identifier type. The callable returns the very object passed in and performs no runtime validation.

Arithmetic on these integer identifiers yields an ordinary integer.

Examples

from typing import NewType
UserId = NewType("UserId", int)
raw = int("1200")
value = UserId(raw)
print(value is raw, type(value + 1).__name__)
print(UserId("x"), UserId.__supertype__ is int)

Result

True int
x True

Arithmetic on these integer identifiers yields an ordinary integer.

Sources