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.
  • Annotations like typing.Literal and typing.Required are not considered types, but "annotated types", and are therefore not supported in type checks. New function resolve_annotation() can be used to resolve these to types before calling isinstance_typing().

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.1.1.tar.gz (18.5 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.1.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for typing_utilities-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f792bcdb24b20373019b71c6a83a617f1030f74c78d6269e4fc101cea945a655
MD5 91facee7cb0808d403c494e3fd436752
BLAKE2b-256 5e800393cc297945898d6a98887cbb5b7509dff3574d7c21a69050a15f08b06a

See more details on using hashes here.

Provenance

The following attestation bundles were made for typing_utilities-0.1.1.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.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typing_utilities-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3c3fb9e5b2b918f5a6c277a9cd6ba99a2ef4f0f818ed3d551a9afcc10b9c3e2
MD5 6410e4a396ef210b1bdc9fa4b3325601
BLAKE2b-256 bbead464ef5071c56630fd00f6b7340cbba019f45e7f359c042ff94d40a5f672

See more details on using hashes here.

Provenance

The following attestation bundles were made for typing_utilities-0.1.1-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