Skip to main content

Provides a metaclass that extends runtime type checking to the full Python typing spec.

Usage

isinstance

subtype.isinstance(obj, T) answers whether obj is assignable to the type expression T under the typing spec, and narrows obj to T for a static checker when it is. Code can then enforce its own annotations on values that only exist at runtime, where a static checker can't see them.

import subtype

assert subtype.isinstance([[1, 2], [3]], list[list[int]])
assert not subtype.isinstance([[1, 2], ["3"]], list[list[int]])

Where the builtin isinstance raises TypeError for a parameterized generic, subtype.isinstance checks the items of a collection against its type arguments, including nested collections.

from collections.abc import Iterator

assert subtype.isinstance(zip(range(3), "abc"), Iterator[tuple[int, str]])

subtype.isinstance infers an iterator's item type from the objects the iterator was built from, without advancing it.

from collections.abc import Callable
from typing import Protocol


class StrCall(Protocol):
    def __call__(self, o: object, /) -> str:
        return str(o)


assert subtype.isinstance(str, StrCall)
assert subtype.isinstance(str, Callable[[object], str])

A class matches a __call__ protocol by its constructor, the same way it matches Callable[...].

subtype

subtype(T) turns the type expression T into a class that issubclass accepts, and answers whether a type is assignable to T under the typing spec.

from collections.abc import Sequence

from subtype import subtype

assert issubclass(list[bool], subtype(Sequence[int]))
assert not issubclass(list[bool], subtype(list[int]))

issubclass takes a type expression as its first argument, and compares type arguments by the variance of their type parameters.

from typing import Self

assert issubclass(str, subtype(Sequence[Self]))

subtype binds Self to the class being checked, so str is checked against Sequence[str].

from collections.abc import Callable
from typing import Protocol, overload


class Decode(Protocol):
    @overload
    def __call__(self, data: bytes) -> str: ...
    @overload
    def __call__(self, data: None) -> None: ...


assert issubclass(Decode, subtype(Callable[[bytes], str]))

Protocols are matched by the signatures of their members, not only by member names as with runtime_checkable. A protocol's __call__ is checked against Callable[...] like any other callable, overloads included.

Installation

Install the package via pip:

pip install subtype

Metadata

Release files for subtype 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subtype 0.1.1
File Size Uploaded
subtype-0.1.1.tar.gz 26.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for subtype 0.1.1
File Interpreter ABI Platform
subtype-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 44.8 kB

Release files / subtype-0.1.1.tar.gz

Download URL subtype-0.1.1.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ab493a61cdf3db78bdcddf3857df7018018890ce9a60029ba2857d803ee67745
BLAKE2b-256 checksum
How to use checksums
3bddd08ee65704f14b59d1cf18e08e1b9777332503e7bc89d2c16abdbd5e7049
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release files / subtype-0.1.1-py3-none-any.whl

Download URL subtype-0.1.1-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77280d2f3d39372b8e259db8a241768b181243b4dd3f0376229227736b203b0c
BLAKE2b-256 checksum
How to use checksums
71cede0877f6adb47b7f8e7b1263245a8635f4d81e56c94ff05fbb20748de815
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page