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

Deep validation checks

This project does not check the contents of objects like lists and dicts, which is why isinstance_typing([1, 2, 3], list[int]) returns false. The reason is, that while it's relatively easy to compare every item in a list to the type argument of list[str], other generic types are not as straight forward, thus it's better left to the programmer.

Generic types

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

API

Go to API 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.1.tar.gz (15.1 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.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typing_utilities-0.0.1.tar.gz
  • Upload date:
  • Size: 15.1 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.1.tar.gz
Algorithm Hash digest
SHA256 2144a36b410f8e9e9ae897498a8d1c320913590b36c0e7c1f9a8cf9cfeee651a
MD5 da933e620b14ba9d7e6690f76a6f6d08
BLAKE2b-256 3766a76db8f973647c9737960c2d5bd84fa4e0f3a7d8a2fcfb171893e4284501

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for typing_utilities-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d6160047494b5f0102f69f88ddfa7fc51047e96c4a68bf1a773d55a412ef892
MD5 f493e860e789007c72f03788604f29e3
BLAKE2b-256 38643144aece6c863f4f0215b12a4b5b11c644c7200697411dbccfb6a77183c2

See more details on using hashes here.

Provenance

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