Skip to main content

eval_type_backport

Build Status Coverage Status Supports Python versions 3.7+, including PyPy Anaconda's conda-forge channel

This package makes runtime typing inspection with the typing module possible with newer syntax in older Python versions. Specifically, this transforms X | Y into typing.Union[X, Y] and list[X] into typing.List[X] etc. (for all the types made generic in PEP 585) if the original syntax is not supported in the current Python version.

For users of Pydantic, merely having this package installed should make Pydantic models with newer syntax work in older Python versions.

Here's an example of how to use it directly:

import typing

from eval_type_backport import install_patch

install_patch()


class Foo:
    a: 'int | str'


print(typing.get_type_hints(Foo))

In Python 3.9, without the install_patch, the above code will raise TypeError: unsupported operand type(s) for |: 'type' and 'type'. With it, it prints {'a': typing.Union[int, str]} as expected.

install_patch monkey-patches the typing module so that things should 'just work' from wherever. To specifically use the backport where you need it instead of patching globally, you can use the eval_type_backport.eval_type_backport function directly instead of typing._eval_type. Yes, typing._eval_type is a protected function that you shouldn't normally be using. This package was written to support Pydantic, which uses it internally.

Install

From PyPI:

pip install eval-type-backport

or with Conda:

conda install -c conda-forge eval-type-backport

Release files for eval-type-backport 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for eval-type-backport 0.4.0
File Size Uploaded
eval_type_backport-0.4.0.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for eval-type-backport 0.4.0
File Interpreter ABI Platform
eval_type_backport-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / eval_type_backport-0.4.0.tar.gz

Download URL eval_type_backport-0.4.0.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8397d25e6524c2e67b9576bb0636be27dea2192017711220c534ec2de921e9b0
BLAKE2b-256 checksum
How to use checksums
1c15273a4baf8248d6d76220723c3caf039d283774b31a7c46ba686120145b76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release files / eval_type_backport-0.4.0-py3-none-any.whl

Download URL eval_type_backport-0.4.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad5e2a8db71b6696a56eafb938b0f5a337d3217f256b8e158b469422b4772b20
BLAKE2b-256 checksum
How to use checksums
50a7bb99bf5e6f78736ddb53480f2c3ff3702ffe2196a7c5e1661c03081d398e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page