Skip to main content

Type checking in runtime without stupid games

Project description

logo

Downloads Downloads Coverage Status Lines of code Hits-of-Code Test-Package Python versions PyPI version Checked with mypy Ruff DeepWiki

Python type checking tools are usually very complex. In this case, we have thrown out almost all the places where there is a lot of complexity, and left only the most obvious and necessary things for runtime.

Table of contents

Why?

It's been a long time since static type checking tools like mypy for Python have been available, and they've become very complex. The typing system has also become noticeably more complicated, providing us with more and more new types of annotations, new syntax and other tools. It seems that Python devs procrastinate endlessly, postponing all the really important CPyhton improvements in order to add more garbage to typing.

A separate difficulty arises for those who try to use type annotations in runtime. Many data types make sense only in the context of static validation, and there is no way to verify these aspects in runtime. And some checks, although theoretically possible, would be extremely expensive. For example, to verify the validity of annotation List[int] in relation to a list, you would need to go through all its objects linearly to make sure that none of them violates the contract from the annotation.

So, why do we need this package? There is only one function where you can pass a type or a type annotation + a specific value, and you will find out if one corresponds to the other. That's it! You can use this feature as a support when creating runtime type checking tools, however, we do not offer these tools here. You decide for yourself whether to wrap this function in syntactic sugar like decorators with automatic type checking.

Also, we are not trying to cover the whole chasm of semantics that, for example, mypy can track. Our approach is to make type checking as stupid as possible. This is the only way to avoid the stupid typing games that complex tools impose on us.

What exactly does this library support:

  • The basis of everything is the simplest type checking via isinstance. If you don't use any special types from typing, expect direct type matching.
  • Union support. You can combine the two types through a logical OR.
  • Checking the Optional type and None as an annotation.

And that's what's not here:

  • Supports types with complex semantics from the typing module.
  • Checking the contents of collections. Collections are checked only for the base type.
  • Support for string annotations.

If you need more complex semantics, use static validation tools. If you need strange and expensive runtime checks that try to confuse static semantics by adding thousands of exceptions, use other runtime tools. Use this library if you need a MINIMUM.

Installation

You can install simtypes using pip:

pip install simtypes

You can also quickly try out this and other packages without having to install using instld.

Usage

Import the check function:

from simtypes import check

And pass there 2 arguments, a type or type annotation + value:

print(check(int, 1))
#> True
print(check(str, 1))
#> False

↑ As you can see, the function returns True or False, depending on whether the value matches its annotation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simtypes-0.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simtypes-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file simtypes-0.0.1.tar.gz.

File metadata

  • Download URL: simtypes-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simtypes-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2f11b97c4fe9d03f251eb883fd46d63eaedd488fb429dcd79dea16eca9be7fca
MD5 82c6b7df0445935474b6e1333e7dfcc2
BLAKE2b-256 f8022443a0bfdf0fcb3d9a058fdbc357f697d86dfa935ea83d5dd774a8d2a7ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for simtypes-0.0.1.tar.gz:

Publisher: release.yml on pomponchik/simtypes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simtypes-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: simtypes-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simtypes-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b871ec5ceaef3051e90386d6d4843864a4ced45f9857620306f2addd6211fc6
MD5 040c6a7a4b3acac52b370f6445ef0e7f
BLAKE2b-256 51080d85f8d5933f1b741bace6dea98cbef0bff2b0c520ea73bf11b2ae7522e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for simtypes-0.0.1-py3-none-any.whl:

Publisher: release.yml on pomponchik/simtypes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page