Runtime reflection and validation of types and generics.
Project description
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 typelistwhereas the opposite is not true. - Any type is a subclass of
type[Any]. type[Any]is not an instance oftype[Any].- Builtin types and
typingtypes are interchangeable, i.e.list[T]is interchangeable withtyping.List[T]etc. - Annotations like
typing.Literalandtyping.Requiredare not considered types, but "annotated types", and are therefore not supported in type checks. New functionresolve_annotation()can be used to resolve these to types before callingisinstance_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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f792bcdb24b20373019b71c6a83a617f1030f74c78d6269e4fc101cea945a655
|
|
| MD5 |
91facee7cb0808d403c494e3fd436752
|
|
| BLAKE2b-256 |
5e800393cc297945898d6a98887cbb5b7509dff3574d7c21a69050a15f08b06a
|
Provenance
The following attestation bundles were made for typing_utilities-0.1.1.tar.gz:
Publisher:
python-publish.yml on apmadsen/typing-utilities
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typing_utilities-0.1.1.tar.gz -
Subject digest:
f792bcdb24b20373019b71c6a83a617f1030f74c78d6269e4fc101cea945a655 - Sigstore transparency entry: 849987543
- Sigstore integration time:
-
Permalink:
apmadsen/typing-utilities@6b480041feba143294788b215235da7f9b122d1c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6b480041feba143294788b215235da7f9b122d1c -
Trigger Event:
release
-
Statement type:
File details
Details for the file typing_utilities-0.1.1-py3-none-any.whl.
File metadata
- Download URL: typing_utilities-0.1.1-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3c3fb9e5b2b918f5a6c277a9cd6ba99a2ef4f0f818ed3d551a9afcc10b9c3e2
|
|
| MD5 |
6410e4a396ef210b1bdc9fa4b3325601
|
|
| BLAKE2b-256 |
bbead464ef5071c56630fd00f6b7340cbba019f45e7f359c042ff94d40a5f672
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typing_utilities-0.1.1-py3-none-any.whl -
Subject digest:
a3c3fb9e5b2b918f5a6c277a9cd6ba99a2ef4f0f818ed3d551a9afcc10b9c3e2 - Sigstore transparency entry: 849987544
- Sigstore integration time:
-
Permalink:
apmadsen/typing-utilities@6b480041feba143294788b215235da7f9b122d1c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6b480041feba143294788b215235da7f9b122d1c -
Trigger Event:
release
-
Statement type: