Skip to main content

Check Status Code style: black Security: bandit Pre-commit Semantic Versions Coverage Report

safecheck

Opinionated combination of typechecking libraries. Safecheck is a (very) minimal wrapper of the following libraries to provide a unified and simple-to-use interface:

Safecheck configures a unified typecheck decorator that invokes beartype.beartype if the function annotations do not contain any jaxtyping-related types. If the function contains jaxtyping-related types typecheck invokes jaxtyping.jaxtyped with beartype.beartype as a runtime type-checker. safecheck is highly-efficient, it adds no measurable overhead to the underlying type and shape checking logic.

One of the goals of safecheck is to abstract over the runtime-typechecker and -shapechecker such that the concrete implementation can be swapped without requiring changes to the codebase.

We re-export most of the functionality of beartype and jaxtyping, and it might be a good idea to disallow imports from beartype and jaxtyping if you are using safecheck, e.g. using ruff or Flake8.

To unify the jaxtyping.Array interface, we export jax.Array as JaxArray if Jax is available, torch.Tensor as TorchArray if PyTorch is available and numpy.typing.NDArray as NumpyArray if NumPy is available.

In addition to the unified typecheck, the library provides a typecheck_overload decorator.

API

decorators

typecheck(fn)

typechecks a function without jaxtyping annotations, otherwise additionally shapecheck the function.

typecheck_overload(fn)

ensures that an implementing function satisfied at least one of its defined overloads.

introspection

is_instance(obj, hint)

like isinstance(...), but better.

assert_instance(obj, hint)

like assert isinstance(...), but better.

is_subtype(subhint, superhint)

tests if a type is a subtype of another type.

validators

Validators enable runtime validation using typing.Annotated, but these annotations are not enforced by any static type checker and always require a runtime @typecheck.

Is

for example: Annotated[str, Is[lambda x: x > 0)]]

IsAttr

for example: Annotated[NumpyArray, IsAttr["ndim", IsEqual[1]]]

IsEqual

for example: Annotated[list, IsEqual[list(range(42))]]

IsSubclass

for example: Annotated[type, IsSubclass[str, bytes]]

IsInstance

for example: Annotated[object, IsInstance[str, bytes]]

union array types

Exported union array types from safecheck.

Shaped      # Any type at all (e.g. object or string)
Num         # Any integer, unsigned integer, floating, or complex
Real        # Any integer, unsigned integer or floating
Inexact     # Any floating or complex
Float       # Any floating point
Complex     # Any complex
Integer     # Any integer or unsigned integer
UInt        # Any unsigned integer
Int         # Any signed integer

concrete array types

Exported array types from safecheck.

Int8
Int16
Int32
Int64
Float16
Float32
Float64
Bool
UInt8
UInt16
UInt32
UInt64
Complex64
Complex128

Examples

Type-checking a simple function.

from safecheck import typecheck


@typecheck
def f(x: int) -> int:
    return x

# f(1) -> 1
# f("1") -> fails

Type-checking a simple method.

from safecheck import typecheck


class A:
    @typecheck
    def f(self, x: int) -> int:
        return x

# A().f(1) -> 1
# A().f("1") -> fails

Shape-checking a simple function.

from safecheck import typecheck, NumpyArray, Integer


@typecheck
def f(x: Integer[NumpyArray, "n"]) -> Integer[NumpyArray, "n"]:
    return x

# import numpy as np
# f(np.array([1, 2, 3, 4, 5])) -> array([1, 2, 3, 4, 5])
# f(np.array([1.0, 2.0, 3.0, 4.0, 5.0])) -> fails
# f(np.array([[1], [2], [3], [4], [5]])) -> fails

Shape-checking a simple method.

from safecheck import typecheck, NumpyArray, Integer


class A:
    @typecheck
    def f(self, x: Integer[NumpyArray, "n"]) -> Integer[NumpyArray, "n"]:
        return x

# import numpy as np
# A().f(np.array([1, 2, 3, 4, 5])) -> array([1, 2, 3, 4, 5])
# A().f(np.array([1.0, 2.0, 3.0, 4.0, 5.0])) -> fails
# A().f(np.array([[1], [2], [3], [4], [5]])) -> fails

Type-checking an overloaded function.

from typing_extensions import overload  # python < 3.11, otherwise ``from typing import overload``
from safecheck import typecheck_overload


@overload
def f(x: int) -> int:
    ...


@typecheck_overload
def f(x):
    return x

# f(1) -> 1
# f("1") -> fails

Type-checking an overloaded method.

from typing_extensions import overload  # python < 3.11, otherwise ``from typing import overload``
from safecheck import typecheck_overload


class A:
    @overload
    def f(self, x: int) -> int:
        ...

    @typecheck_overload
    def f(self, x):
        return x

# A().f(1) -> 1
# A().f("1") -> fails

Shape-checking an overloaded function.

from typing_extensions import overload  # python < 3.11, otherwise ``from typing import overload``
from safecheck import typecheck_overload, NumpyArray, Integer


@overload
def f(x: Integer[NumpyArray, "n"]) -> Integer[NumpyArray, "n"]:
    ...


@typecheck_overload
def f(x):
    return x

# import numpy as np
# f(np.array([1, 2, 3, 4, 5])) -> array([1, 2, 3, 4, 5])
# f(np.array([1.0, 2.0, 3.0, 4.0, 5.0])) -> fails
# f(np.array([[1], [2], [3], [4], [5]])) -> fails

Shape-checking an overloaded method.

from typing_extensions import overload  # python < 3.11, otherwise ``from typing import overload``
from safecheck import typecheck_overload, NumpyArray, Integer


class A:
    @overload
    def f(self, x: Integer[NumpyArray, "n"]) -> Integer[NumpyArray, "n"]:
        ...

    @typecheck_overload
    def f(self, x):
        return x

# import numpy as np
# A().f(np.array([1, 2, 3, 4, 5])) -> array([1, 2, 3, 4, 5])
# A().f(np.array([1.0, 2.0, 3.0, 4.0, 5.0])) -> fails
# A().f(np.array([[1], [2], [3], [4], [5]])) -> fails

Metadata

Release files for safecheck 0.5.3

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

Source distribution (sdist)

Source distribution for safecheck 0.5.3
File Size Uploaded Provenance Actions
safecheck-0.5.3.tar.gz 8.7 kB Without provenance Details for safecheck-0.5.3.tar.gz

Built distribution (wheel)

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

Total release size: 17.9 kB

Release files / safecheck-0.5.3.tar.gz

Download URL safecheck-0.5.3.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fd8b830d0b2d3609c3d56206b77d8c43c04360f8663e3f22b2bb7d8f3cd6f0c6
BLAKE2b-256 checksum
How to use checksums
9fde4c4a4f9e705db311d5f9650890891d356faaa821def856fc90a91e7e8473
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Release files / safecheck-0.5.3-py3-none-any.whl

Download URL safecheck-0.5.3-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37b3f7d8b7f3ae654b9e3ef4dd5ffa6bc558c1e56d2bc87a2e0d17595d1341ee
BLAKE2b-256 checksum
How to use checksums
546cab3bb9c4342abc46130ae908fc5afab555e04528aecb70a191cea05a36ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Security for safecheck 0.5.3

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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