Skip to main content

Like `typing._eval_type`, but lets older Python versions use newer typing features.

Project description

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

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

eval_type_backport-0.4.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

eval_type_backport-0.4.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file eval_type_backport-0.4.0.tar.gz.

File metadata

  • Download URL: eval_type_backport-0.4.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for eval_type_backport-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8397d25e6524c2e67b9576bb0636be27dea2192017711220c534ec2de921e9b0
MD5 f25c80c6bb957217fd446215daffd4a4
BLAKE2b-256 1c15273a4baf8248d6d76220723c3caf039d283774b31a7c46ba686120145b76

See more details on using hashes here.

File details

Details for the file eval_type_backport-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for eval_type_backport-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad5e2a8db71b6696a56eafb938b0f5a337d3217f256b8e158b469422b4772b20
MD5 b9aef23076f9142830560ddb79d8b1de
BLAKE2b-256 50a7bb99bf5e6f78736ddb53480f2c3ff3702ffe2196a7c5e1661c03081d398e

See more details on using hashes here.

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