Runtype is a Python library providing utilities for fast run-time type validation and multiple dispatch. It enhances Python's built-in `dataclasses` with runtime type validation and automatic casting, offers smart alternatives to `isinstance` and `issubclass` for complex types, and implements a performant multiple-dispatch decorator. The library is currently at version 0.5.3, with a regular release cadence addressing bug fixes and performance improvements.
pip install runtypeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic runtime type validation using `isa` and shows how to define a type-safe dataclass using `runtype.dataclass`. It includes examples of both successful validation and instances where validation fails, raising a TypeError.
Avoid iterating directly over `runtype.dataclass` instances. Use explicit methods like `asdict()` or `astuple()` if available, or access attributes directly.
Review usage of `Any` in type annotations within `runtype` contexts. If `All` (a new type introduced in 0.5.0) is more appropriate for your use case, consider switching to it. Ensure your type annotations accurately reflect the desired flexibility or strictness.
For versions 0.3.5 and later, `multidispatch` on `__init__` will augment rather than replace the default dataclass initialization. If you relied on `runtype` completely overriding `__init__`, you might need to adjust your dispatch logic or use `__post_init__` for additional setup.
Instead of `for item in my_dataclass_instance:`, use `my_dataclass_instance.asdict().items()` or access specific attributes: `my_dataclass_instance.field_name`.
Ensure that the input values strictly match the type hints in your `runtype.dataclass`. If you want `runtype` to attempt automatic type conversion, decorate your dataclass with `@dataclass(check_types='cast')`.
Refactor your dispatch logic to use concrete types or broader type unions instead of `Literal` for dispatch arguments. `Literal` types are typically for static checking, not runtime dispatch values.
No dependency data recorded yet.