Registry / type-stubs / optype

optype

JSON →
library0.18.0pypypi✓ verified 24d ago

OpType is a Python library (v0.17.0) providing building blocks for precise and flexible type hints, offering single-method protocols for dunder methods, exact types that reject sneaky subtypes, and typed operators. It aims to make type-checking more robust and expressive. The library is actively maintained with somewhat frequent minor releases and supports various modern type checkers like mypy, pyright, and pyrefly.

pip install optype
INSTALL
IMPORT
SIG · OPTYPE
O
optype
type-stubspythonv0.18.0
Install
2.6s avg
Import
192ms
Disk
89MB
Pass rate
8/ 10
Env Coverage8 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 2.65s
py 3.11
✓ —
✓ 2.7s
py 3.12
✓ —
✓ 2.55s
py 3.13
✓ —
✓ 2.5s
py 3.9
✕ build_error
✕ build_error
89MB installed
● package 89MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CanAdd
from optype import CanAdd
CanAbs
from optype import CanAbs
JustAny
from optype import JustAny
HasDataclassFields
from optype.dataclasses import HasDataclassFields
do_add
from optype import do_add
import operator; operator.add
optype.do_add provides a correctly typed, runtime-checkable version of the add operator.

This example demonstrates how to define a function `twice` that accepts any type `x` for which `2 * x` is valid, using `optype.CanRMul` for precise type hinting. It also shows a more flexible version using `CanMul` as a fallback, leveraging `isinstance` due to `optype` protocols being runtime-checkable.

from typing import Literal, TypeVar from optype import CanMul, CanRMul Y = TypeVar('Y') Two: Literal[2] = 2 def twice(x: CanRMul[Literal[2], Y]) -> Y: return Two * x # Example usage with different types print(f"twice(2) = {twice(2)}") print(f"twice(3.14) = {twice(3.14)}") print(f"twice('I') = {twice('I')}") # Fallback for types that implement __mul__ but not __rmul__ def twice_flexible(x: CanRMul[Literal[2], Y] | CanMul[Literal[2], Y]) -> Y: if isinstance(x, CanRMul): return Two * x else: return x * Two print(f"twice_flexible(5) = {twice_flexible(5)}")
Debug
Known issues
breakingIn v0.15.0, several `optype.Can*` generic type parameters (e.g., `CanBytes`, `CanStr`, `CanLen`) intended for literal types were removed. This impacts protocols like `CanBytes`, `CanStr`, `CanIndex`, `CanRepr`, etc.
fix
Review the specific `Can*` protocols affected and adjust type parameter usage according to the updated documentation or remove redundant parameters.
affects: >=0.15.0
breakingThe `optype.dataclasses.HasDataclassFields` protocol had its generic type parameter removed in v0.17.0, and `__dataclass_fields__` was turned into a `ClassVar`. This fixes an assignability issue with dataclass instances but requires adjusting code that relied on the generic parameter.
fix
Remove the generic type parameter from `HasDataclassFields` usage. For example, change `HasDataclassFields[T]` to `HasDataclassFields`.
affects: >=0.17.0
gotchaWhen using `optype.numpy`, ensure `numpy-typing-compat` is installed. As of v0.13.0, this dependency is required for robust static typing compatibility with NumPy versions and is automatically installed with the `optype[numpy]` extra.
fix
Install with `pip install optype[numpy]` or explicitly `pip install numpy-typing-compat` alongside `optype`.
affects: >=0.13.0
gotchaWhile `optype` protocols are runtime-checkable (e.g., `isinstance('snail', optype.CanAdd)`), it is considered bad practice to use them as base classes for your own implementations. They are pure interfaces, not abstract base classes like `collections.abc` protocols.
fix
Use `optype` protocols for type checking and runtime `isinstance` checks, but inherit from standard ABCs or implement methods directly in your classes rather than inheriting from `optype.Can*` protocols.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'optype'
The 'optype' library is not installed in the Python environment, or the Python interpreter cannot find it in its search path.
fix
Install the library using pip: `pip install optype`
ModuleNotFoundError: No module named 'typing_extensions'
When using 'optype' on Python versions 3.13 or newer, a dependency on 'typing_extensions' might incorrectly be triggered, leading to this error if 'typing_extensions' is not installed. This was a known issue fixed in optype v0.17.0, but could still occur in specific environments or if using a slightly older patch version.
fix
Install the missing dependency: `pip install typing_extensions`
Argument 1 to "my_function" has incompatible type "DerivedClass"; expected "optype.Exact[BaseClass]"
This Mypy (or similar type checker) error occurs when a function or variable annotated with `optype.Exact[BaseClass]` is provided with an instance of `DerivedClass`, where `DerivedClass` is a subtype of `BaseClass`. `optype.Exact` specifically rejects subtypes, requiring the type to be precisely `BaseClass`.
fix
Ensure that the value passed to the `optype.Exact` annotation is of the exact specified type, not a subtype. If a subtype is acceptable, use `BaseClass` directly or `typing.Union` instead of `optype.Exact`.
Argument 1 to "my_function" has incompatible type "MyObject"; expected "optype.CanAdd[int, int]"
This Mypy (or similar type checker) error indicates that an object of type `MyObject` was passed to a function expecting a type that implements the `optype.CanAdd` protocol (meaning it must have a `__add__` method compatible with the specified arguments and return type), but `MyObject` does not correctly implement this protocol.
fix
Modify `MyObject` to implement the `__add__` dunder method with a signature compatible with `optype.CanAdd[int, int]`, or pass an object that already satisfies the `optype.CanAdd` protocol.
Upgrade
Version history
0.18.0latest on PyPI · released Jun 7, 2026
Audit
Dependencies
numpy-typing-compatoptionalRequired for the 'numpy' extra to ensure static typing compatibility with NumPy versions.
Agent activity
36 hits · last 30 days
node
32
OpenAI (training)
1
Resources
optype — pip install optype · libregistry