skillfed

optype

Building Blocks for Precise & Flexible Type Hints

optype v0.18.0 3.9M downloads/30d#2,472 on PyPI94
Permissive license BSD-3-Clause Active released

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 optype

uv

uv add optype

poetry

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 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

Development Status :: 4 - BetaIntended Audience :: DevelopersIntended Audience :: Science/ResearchOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.15Topic :: Scientific/EngineeringTopic :: Software DevelopmentTyping :: Typed

Tags

type hints protocolsprecise type checkingdunder method protocolstyped operatorsmypy pyright supporttype annotation building blocksflexible type hints
type-hintsprotocolstype-checking

More Software Development packages