typemap

What is typemap?

Understand what typemap is and why it exists

A Runtime Type Manipulation Library

typemap is a library that lets you work with types at runtime. Think of it as bringing TypeScript's type system capabilities to Python.

The Problem

In Python, types exist only at compile time (when type checkers like mypy run). Once your code is running, the types are gone:

class User:
    name: str
    age: int

# At runtime, we just see a class - no type info!

But what if you needed to:

  • Dynamically generate API endpoints from a model?
  • Create a form system that adapts to your data classes?
  • Build an ORM that knows about your fields?

The Solution

typemap brings type manipulation to runtime:

from typemap import eval_typing
import typemap_extensions as tm

class User:
    name: str
    age: int
    email: str

# Get all field names at runtime!
keys = eval_typing(tm.KeyOf[User])
# Returns: tuple[Literal["name"], Literal["age"], Literal["email"]]

What Can You Do?

With typemap, you can:

  • Introspect types - Get field names, types, annotations
  • Transform types - Pick, omit, make optional, etc.
  • Build dynamically - Create protocols, classes at runtime
  • Validate - Check type compatibility at runtime

Why PEP 827?

This library implements PEP 827, a proposed Python Enhancement Protocol that adds type manipulation operators to Python's typing module.

It's inspired by TypeScript's type system - if you've used TypeScript, you'll feel at home!

Who is this for?

  • Framework authors building ORMs, form libraries, API generators
  • Library authors needing runtime type introspection
  • Curious developers exploring Python's type system

Note: This library requires Python 3.14+ as it uses features from PEP 827 which is not yet finalized.

On this page