Skip to main content

PEP 827 type manipulation library

Project description

typemap

PEP 827 type manipulation library for Python 3.14+

Installation

pip install typemap

Overview

typemap provides utilities for working with PEP 827 type manipulation operators. It includes:

  • eval_typing - Evaluate type expressions at runtime
  • Type operators via typemap_extensions:
    • Member - Access type members by name
    • Attrs - Get class attributes
    • Iter - Iterate over type contents
    • Param - Define callable parameters
    • UpdateClass - Generate class modifications
    • NewProtocol - Create protocols dynamically
    • IsAssignable - Check type assignability
    • KeyOf - Get all member names as tuple of Literals
    • Template - Build template literal strings
    • DeepPartial - Make all fields recursively optional
    • Partial - Make all fields optional (non-recursive)
    • Required - Remove Optional from all fields
    • Pick - Pick specific fields from a type
    • Omit - Omit specific fields from a type

Usage

Evaluating Types

from typemap import eval_typing

class MyClass:
    x: int
    y: str

result = eval_typing(MyClass)
print(result)

Type Utilities

typemap provides several type utility operators for transforming types:

from typing import Literal
import typemap_extensions as tm

# KeyOf - Get all member names as tuple of Literals
class User:
    name: str
    age: int

keys = tm.KeyOf[User]
# Result: tuple[Literal["name"], Literal["age"]]

# Partial - Make all fields optional (non-recursive)
PartialUser = tm.Partial[User]
# Result: class with name: str | None, age: int | None

# DeepPartial - Make all fields recursively optional
DeepUser = tm.DeepPartial[User]
# Result: class with all nested fields optional

# Required - Remove Optional from all fields
class OptionalUser:
    name: str | None
    age: int | None

RequiredUser = tm.Required[OptionalUser]
# Result: class with name: str, age: int

# Pick - Select specific fields
PublicUser = tm.Pick[User, tuple["name"]]
# Result: class with only name: str

# Omit - Exclude specific fields
SafeUser = tm.Omit[User, tuple["password"]]
# Result: class without password field

Using UpdateClass

from typing import Callable, Literal, Self, Never
import typemap_extensions as typing

type InitFn[T] = typing.Member[
    Literal["__init__"],
    Callable[
        [
            typing.Param[Literal["self"], Self],
            *[
                typing.Param[p.name, p.type]
                for p in typing.Iter[typing.Attrs[T]]
            ],
        ],
        None,
    ],
    Literal["ClassVar"],
]

class Model:
    def __init_subclass__[T](cls: type[T]) -> typing.UpdateClass[InitFn[T]]:
        super().__init_subclass__()

class User(Model):
    name: str
    age: int

Development

# Install dependencies
cd packages/typemap
uv sync

# Run tests
uv run pytest

# Run type checking
uv run mypy src/typemap/

# Run linting
uv run ruff check src/typemap/

Requirements

  • Python 3.14+
  • typing_extensions >= 4.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typemap-0.2.3.tar.gz (68.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typemap-0.2.3-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

Details for the file typemap-0.2.3.tar.gz.

File metadata

  • Download URL: typemap-0.2.3.tar.gz
  • Upload date:
  • Size: 68.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typemap-0.2.3.tar.gz
Algorithm Hash digest
SHA256 0d4f26986469ed341c5b40e9eb5dcab4bc66cfdbf36ab2478accaf50b8372b2f
MD5 b4507ed3642be8d5e525441db4c59be2
BLAKE2b-256 74ed2b298c23048121118fdfb1c8107385bd50e53903d5b84b06abbdd8c0d2df

See more details on using hashes here.

File details

Details for the file typemap-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: typemap-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 33.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typemap-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 84c818c21867c723c9f610e5c115926084591021e74a8a197f94fc3748836411
MD5 2bca797457700284be9ce45f7ae11c67
BLAKE2b-256 57610b0d6d7b425c2283078ed64a3b08cd46757afece5457e7db74e4d8c74547

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page