Skip to main content

Runtime reflection and validation of types and generics.

Project description

Test Coverage Stable Version Pre-release Version PyPI - Python Version PyPI Downloads

typing-utilities: Runtime reflection and validation of types and generics.

typing-utilities extends Python with the ability to check instances and types of generic types and unions introduced in the typing module.

Following is a small example of two of the most usable functions issubclass_typing and isinstance_typing, but a lot more is to be found in the API section further down...

Example:

from typing import Generic, TypeVar
from typingutils import issubclass_typing, isinstance_typing

T = TypeVar('T')

class Class1(Generic[T]):
    pass

class_type1 = Class1[str]
class_type2 = Class1[int]

issubclass_typing(class_type1, class_type2) # => False

# next line will fail
issubclass(class_type1, class_type2) # => TypeErrorr: Subscripted generics cannot be used with class and instance checks

class_inst1 = class_type1()
class_inst2 = class_type2()

isinstance_typing(class_inst1, class_type1) # => True
isinstance_typing(class_inst1, class_type2) # => False
isinstance_typing(class_inst2, class_type2) # => True
isinstance_typing(class_inst2, class_type1) # => False

# next line will fail
isinstance(class_inst1, class_type1) # => TypeError: Subscripted generics cannot be used with class and instance checks

Conventions

This project differs from Python and other projects in some aspects:

  • Generic subscripted types like list[str] are always a subclass of its base type list whereas the opposite is not true.
  • Any type is a subclass of type[Any].
  • type[Any] is not an instance of type[Any].
  • Builtin types and typing types are interchangeable, i.e. list[T] is interchangeable with typing.List[T] etc.

What's not included

Generic types

It's not the goal of this project to deliver generic types such as generically enforced lists and dicts.

Full documentation

Go to documentation

Other similar projects

There are other similar projects out there like typing-utils and runtype, and while typing-utils is outdated and pretty basic, runtype is very similar to typing-utilities when it comes to validation.

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

typing_utilities-0.0.3.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

typing_utilities-0.0.3-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file typing_utilities-0.0.3.tar.gz.

File metadata

  • Download URL: typing_utilities-0.0.3.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for typing_utilities-0.0.3.tar.gz
Algorithm Hash digest
SHA256 93129ea238160aa37563934a8cf4efc66bb363438735fdb99315c2f30a246505
MD5 dd8d7f5735a95d28420c6f9eedb0b5ab
BLAKE2b-256 7a524ea9c89fd50847e79b6b21e67cf94dcfc97f41ded00d3f476dab48753f01

See more details on using hashes here.

Provenance

The following attestation bundles were made for typing_utilities-0.0.3.tar.gz:

Publisher: python-publish.yml on apmadsen/typing-utilities

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

File details

Details for the file typing_utilities-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for typing_utilities-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2d96f1c2878be8966c86885190e2587d7f9e80c2cc700a51c7e992b0ba1b6bf7
MD5 f90784ac182e404ee419d8161dca5484
BLAKE2b-256 69ac9a79251a4e404362d7763673fdcfe0ed5708f0b8027083ac3168f2b6bf6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for typing_utilities-0.0.3-py3-none-any.whl:

Publisher: python-publish.yml on apmadsen/typing-utilities

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