classes
Smart, pythonic, ad-hoc, typed polymorphism for Python
What it is and what it does
Classes provides a typeclass system for Python—a way to bind behavior to types without creating class hierarchies or interfaces. Instead of writing many subclasses (like IntField, BoolField, etc.), you define a typeclass as a function and register implementations for specific types using decorators. This approach reduces boilerplate, improves type safety through mypy integration, and makes it easier to extend behavior for new types without modifying existing code.
The package is designed for declarative, functional-style business logic where different data types need different handling. A common use case is serialization (converting objects to JSON based on their type), but it applies anywhere you'd otherwise use a type-based dispatch pattern. It requires mypy plugin configuration to achieve full type safety; without it, the type checker will report violations.
Use it for:
- Implement serialization/deserialization logic that varies by type (e.g., custom JSON converters for int, bool, datetime).
- Build validation rules that apply different checks to different data types without inheritance chains.
- Create extensible handler registries where third-party code can add support for custom types.
- Write functional-style data transformation pipelines where operations dispatch based on input type.
- Reduce boilerplate in libraries that need type-specific behavior (like ORMs or REST frameworks).
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Implements typeclasses for Python, enabling type-based dispatch and polymorphic behavior without inheritance or interfaces, with full mypy support.
Yes, with conditions. Install if you need ad-hoc polymorphism without inheritance and are comfortable with dormant maintenance (last release 2022-04-11). The package is stable, has no known vulnerabilities, and low install friction. Not recommended if you require active upstream support or need assurance of mypy plugin compatibility with the latest type-checker versions.
Install
classes on PyPI
pip
pip install classesuv
uv add classespoetry
poetry add classesInstalling classes
Before you install
Low friction install with a single runtime dependency (typing_extensions). Maintenance is dormant—last release was 2022-04-11, over 1586 days ago, though the repository remains active with a recent commit on 2024-12-17. Suitable for stable, mature use cases but not for projects requiring active upstream support.
License in practice
BSD-2-Clause is permissive and imposes minimal restrictions. You can use, modify, and distribute this package freely in commercial and private projects provided you include the license notice.
Quickstart
pip install classes
from classes import typeclass
@typeclass
def to_json(instance) -> str:
"""Convert things to json."""
@to_json.instance(int)
def _to_json_int(instance: int) -> str:
return str(instance)
result = to_json(value)
Requires mypy plugin configuration in setup.cfg or mypy.ini under [mypy] with 'plugins = classes.contrib.mypy.classes_plugin' for full type safety; without it, type violations will be reported.
Verify before relying
- Whether the mypy plugin remains compatible with recent mypy versions given the package's dormant status since 2022.
- Real-world performance characteristics when dispatching on complex type hierarchies or with many registered instances.
Package facts
| License | BSD-2-Clause (permissive) |
| Python support | supports the current Python release (>=3.7,<4.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — typing_extensions |
| Maintenance | dormant — 1,586 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 135,192/month — #11,450 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: classes-0.4.1-py3-none-any.whl
Keywords: functional programming, fp, type class, typeclass, typeclasses, ad-hoc, composition, type-safety, mypy, stubs
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
fasttransformfasttransform provides a Transform class for…
permissive · top 15,000 on PyPI
overloadingProvides function and method overloading based…
permissive · top 15,000 on PyPI
multipledispatchEnables functions to dispatch on the types of…
permissive · top 5,000 on PyPI
visitorProvides a base Visitor class to implement the…
permissive · top 15,000 on PyPI
simplegenericProvides a lightweight decorator-based system…
unclear · top 15,000 on PyPI
methoddispatchAdds single-dispatch method support to Python…
permissive · top 15,000 on PyPI
plum-dispatchPlum implements multiple dispatch in Python,…
permissive · top 5,000 on PyPI
singledispatchProvides single-dispatch generic functions—a…
permissive · top 5,000 on PyPI
returnsProvides functional programming…
permissive · top 5,000 on PyPI
mypy-zopeA mypy plugin that treats zope.interface…
permissive · top 15,000 on PyPI