--- id: optype version: "0.18.0" license: BSD-3-Clause license_treatment: permissive maintenance: active --- # optype — Building Blocks for Precise & Flexible Type Hints License: permissive · Maintenance: active · Downloads: 3.9M/mo ## 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 above — 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 pip install optype uv add optype poetry add optype ## Installing 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_current - Install friction: low - Maintenance: active - Downloads: 3.9M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags type hints protocols, precise type checking, dunder method protocols, typed operators, mypy pyright support, type annotation building blocks, flexible type hints, type-hints, protocols, type-checking [View on SkillFed](https://skillfed.io/packages/optype) · [View on PyPI](https://pypi.org/project/optype/)