On this page
  1. typing.NewType
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

typing.NewType

Prev: typing.IO | Up: typing | Next: typing.ParamSpec
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.NewType
Tags: #public #python3 #stdlib #typing


Import

import typing
from typing import NewType

Signature & Overview

typing.NewType(name, tp)

NewType creates simple unique types with almost zero runtime overhead.

Usage

from typing import NewType

UserId = NewType("UserId", int)
uid = UserId(42)
assert uid == 42 and type(uid) is int

Gotchas

REPL Inspect

from typing import NewType
help(NewType)

Linked from

Related notes