named

raw JSON →
1.4.2 verified Mon Apr 27 auth: no python

A Python library providing named types (newtypes) with runtime validation and type-checking support. Current version 1.4.2, requires Python ≥3.8, follows semantic versioning with regular releases.

pip install named
error ImportError: cannot import name 'Named' from 'named'
cause The correct import is `import named` then use `named.Named`; there is no direct `from named import Named`.
fix
Use import named and reference named.Named.
error TypeError: 'Named' object does not support item assignment
cause Named objects are immutable and do not support setting attributes after creation.
fix
Create a new named instance instead of modifying existing one.
gotcha Named types are immutable after creation: you cannot reassign the value attribute.
fix Create a new instance with the desired value instead of modifying an existing one.
gotcha The `Named` class does not support custom `__init__` methods; initialization is handled by the parent class. Overriding `__init__` may break validation.
fix Use class methods or properties to add custom behavior without overriding `__init__`.

Define a named type by subclassing `Named`. The subclass provides runtime type checking and can be used with type hints.

import named

# Define a named type for UserId
class UserId(named.Named):
    pass

# Create an instance with validation
uid = UserId(42)
print(uid)  # UserId(42)

# Access the underlying value
print(uid.value)  # 42