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

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.0
File Size Uploaded
subtype-0.1.0.tar.gz 26.8 kB Details

Built distribution (wheel)

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

Total release size: 44.6 kB

Release files / subtype-0.1.0.tar.gz

Download URL subtype-0.1.0.tar.gz
Size 26.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0179c3fb0da003b6b90d6d49ed8b7be16b5bb1ec79a1cec0d39cc130070ff2a4
BLAKE2b-256 checksum
How to use checksums
aefe50d3d28c5d20b31841d7256ce1f3db8da9544f9aa3ee20c16b9e38b46ebf
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.0-py3-none-any.whl

Download URL subtype-0.1.0-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5576ebb8a5ee2b3c4865944c20ad2d6098eb91e4c55ef12c43e8758142e3cdea
BLAKE2b-256 checksum
How to use checksums
08dfc549de36e7984b5380aff4f9c219ba8ab53f64e0a33fcc75f182c149951f
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

0.1.1

2 release files

This release

0.1.0 This release

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