eval_typing
Evaluate type expressions at runtime
The main function to evaluate types at runtime.
Quick Example
from typemap import eval_typing
import typemap_extensions as tm
class User:
name: str
age: int
result = eval_typing(tm.KeyOf[User])
# Returns: tuple[Literal["name"], Literal["age"]]Signature
def eval_typing(type_expr: Any) -> Any:
"""Evaluate a type expression and return the resolved type."""What It Does
eval_typing takes a type expression and:
- Resolves any type aliases
- Applies type operators (like
KeyOf,Partial) - Evaluates special forms (like
IsAssignable) - Returns the concrete type
Usage
Basic Type
class MyClass:
x: int
y: str
result = eval_typing(MyClass)
# Returns the class itselfWith Type Operators
# Get all field names
keys = eval_typing(tm.KeyOf[User])
# Make fields optional
partial = eval_typing(tm.Partial[User])
# Pick specific fields
picked = eval_typing(tm.Pick[User, tuple["name"]])With Special Forms
# Check type compatibility
is_compatible = eval_typing(tm.IsAssignable[int, float])
# Returns: TrueError Handling
from typemap.type_eval import TypeMapError, StuckException
try:
result = eval_typing(some_type)
except TypeMapError as e:
print(f"Type error: {e}")
except StuckException as e:
print(f"Can't evaluate: {e}")When to Use
Use eval_typing when you need to:
- Get information about a type at runtime
- Transform types dynamically
- Build things based on type metadata
Tip: For most use cases, you'll use eval_typing combined with type operators from typemap_extensions.