optype
Building Blocks for Precise & Flexible Type Hints
What it is and what it does
optype is a library for building precise type hints using single-method protocols and exact operator types. It lets you define what operations your code actually accepts—like SupportsAdd for anything that implements __add__—rather than forcing broad type categories. This bridges the gap between what your code needs and what type checkers can verify, working with mypy, pyright, and basedpyright.
The package is designed for developers who want type safety without fighting their type checker. It includes optional NumPy support and is built on typing-extensions, so it works across modern Python versions. The library is actively maintained, battle-tested, and carries no external dependencies beyond typing-extensions.
Use it for:
- Define generic numeric operations that accept anything supporting __add__, __mul__, etc., without forcing a specific numeric type
- Write library code that accepts operator-compatible objects while rejecting incompatible subtypes
- Build precise function signatures for scientific or mathematical code that needs exact protocol matching
- Create type stubs or annotations for NumPy-adjacent code with proper operator typing
- Improve type checker cooperation in codebases using mypy or pyright by expressing protocol requirements explicitly
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
optype provides single-method protocols and typed operators for precise type hints, letting you express exact types that reject sneaky subtypes and work cooperatively with type checkers like mypy and pyright.
Yes. optype is actively maintained, has zero external dependencies beyond typing-extensions, carries no known vulnerabilities, and is BSD-3-Clause licensed. It solves a real problem—expressing exact operator requirements in type hints—that most projects encounter when writing generic or mathematical code. Install it if you write code that needs to accept objects based on what operations they support rather than their concrete type.
Install
optype on PyPI
pip
pip install optypeuv
uv add optypepoetry
poetry add optypeInstalling optype
Before you install
Low install friction; pure Python wheel with only typing-extensions as a runtime dependency. Actively maintained with recent releases and a live repository.
License in practice
BSD-3-Clause is permissive; you can use, modify, and distribute optype freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install optype
from optype import SupportsAdd
def add_numbers(x: SupportsAdd, y: SupportsAdd) -> SupportsAdd:
return x + y
Requires Python 3.12 or later
Verify before relying
- Whether optional NumPy support requires additional setup or is automatically available
- Specific type checker versions known to work best with optype
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (>=3.12) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — typing-extensions |
| Maintenance | actively maintained — 68 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 3,867,980/month — #2,472 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: optype-0.18.0-py3-none-any.whl
Keywords: annotations, basedpyright, checker, checking, hinting, hints, mypy, numpy, numpy-typing-compat, pyright, protocols, stubs, type, typechecking, typehinting, typehints, typing
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
types-dockerProvides type hints for the docker package,…
permissive · top 5,000 on PyPI
types-grpcio-reflectionProvides type hints for grpcio-reflection,…
permissive · top 15,000 on PyPI
types-paramikoProvides type hints for paramiko, enabling…
permissive · top 1,000 on PyPI
types-braintreeProvides type hints for the braintree payment…
permissive · top 15,000 on PyPI
types-psycopg2Provides type hints for psycopg2, enabling…
permissive · top 5,000 on PyPI
types-assertpyProvides type stubs enabling static type…
permissive · top 15,000 on PyPI
numeraryProvides runtime-checkable protocols and type…
permissive · top 15,000 on PyPI
numpy-typing-compatProvides version-specific type annotations for…
permissive · top 5,000 on PyPI
basedpyrightBasedpyright is a static type checker for…
permissive · top 5,000 on PyPI