PEP 585 + PEP 604 backports.
Project description
modern_types
__modern_types__
aims to provide PEP 585 + PEP 604 backports for Python <=3.10 deferred type evaluation backward compatibility.
Hence, the targeted Python versions are 3.8 and 3.9.
What does it do?
Prevents type errors in evaluating PEP 585 and PEP 604 type annotations for Python 3.8 and 3.9, which might happen in pydantic models for example.
from __future__ import annotations
from collections import defaultdict
from pprint import pprint
from typing import get_type_hints
import __modern_types__
class Foo:
a: dict[str, int]
b: list[int]
c: set[int]
d: tuple[int, ...] | None
e: frozenset[int]
f: defaultdict[str, int]
pprint(get_type_hints(Foo, globals(), locals()))
gives:
{"a": typing.Dict[str, int],
"b": typing.List[int],
"c": typing.Set[int],
"d": typing.Union[typing.Tuple[int, ...], None],
"e": typing.FrozenSet[int],
"f": typing.DefaultDict[str, int]}
How to use?
Simply import __modern_types__
in your code, and it will override the default global namespace of typing.get_type_hints
.
And now you can use modern types everywhere in your code and the following replacements will be applied without overwriting your parameters:
Old type | New type | Without __modern_types__ , works on Python version... |
With __modern_types__ , works on Python version... |
Backports PEP |
---|---|---|---|---|
dict[KT, VT] |
typing.Dict[str, int] |
>=3.9 | >=3.8 | PEP 585 |
list[VT] |
typing.List[int] |
>=3.9 | >=3.8 | PEP 585 |
set[int] |
typing.Set[int] |
>=3.9 | >=3.8 | PEP 585 |
tuple[int, ...] |
typing.Tuple[int, ...] |
>=3.9 | >=3.8 | PEP 585 |
frozenset[int] |
typing.FrozenSet[int] |
>=3.9 | >=3.8 | PEP 585 |
collections.defaultdict[int] |
typing.DefaultDict[int] |
>=3.9 | >=3.8 | PEP 585 |
X | Y |
typing.Union[X, Y] |
>=3.10 | >=3.8 | PEP 604 |
[!Note] Some optional replacements will also be performed if possible, according to those listed in the
__modern_types__.ext
source code.
__modern_types__
additionally makes sure that generic aliases above are instantiable, which isn't the default behavior.
Use case
Keep your codebase up-to-date by speeding up migrating to modern types, even if you support Python versions >=3.8,<=3.10.
Stop using deprecated typing.Dict
, typing.List
, typing.Set
, typing.Tuple
, typing.FrozenSet
and typing.DefaultDict
!
Importing __modern_types__
will make all typing.get_type_hints
-dependent parts of your application, including pydantic models, work with PEP 585 and PEP 604.
Can __modern_types__
be used in production?
Yes. It won't break the existing code despite the monkeypatch. The library simply overrides the default global namespace of typing.get_type_hints
and tries to re-assign name-to-object references of un-subscriptable and un-orable types in relevant modules.
If some keys that __modern_types__
would supply to typing.get_type_hints
global namespace are present, they are used instead of the __modern_types__
ones.
Installation
If you want to…
…use this tool in your project 💻
You might simply install it with pip:
pip install modern-types
If you use Poetry, then run:
poetry add modern-types
…contribute to modern_types 🚀
[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.
-
Fork the modern_types repository on GitHub.
-
Install Poetry.
Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them. You might use pipx to install it globally (recommended):pipx install poetry
If you encounter any problems, refer to the official documentation for the most up-to-date installation instructions.
Be sure to have Python 3.8 installed—if you use pyenv, simply run:
pyenv install 3.8
-
Clone your fork locally and install dependencies.
git clone https://github.com/your-username/modern_types path/to/modern_types cd path/to/modern_types poetry env use $(cat .python-version) poetry install
Next up, simply activate the virtual environment and install pre-commit hooks:
poetry shell pre-commit install --hook-type pre-commit --hook-type pre-push
For more information on how to contribute, check out CONTRIBUTING.md.
Always happy to accept contributions! ❤️
Legal info
© Copyright by Bartosz Sławecki (@bswck).
This software is licensed under the terms of MIT License.
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
File details
Details for the file modern_types-1.0.0.tar.gz
.
File metadata
- Download URL: modern_types-1.0.0.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c599aa90e037ec1fc174efe04f821af140f0b9df8c0d984bf82b015a05470c0 |
|
MD5 | e267d3b63d9e40bfa3eb06cf914d0f2c |
|
BLAKE2b-256 | aa8c9654a88bf30ced070c7b9ca095ab33cab1d7ac3dcaf9a3283a5e81a04c5a |
File details
Details for the file modern_types-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: modern_types-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d834187f45f33e0413d528c1f527ac55b46a6637cfe2cfc787480ee2c840614 |
|
MD5 | b0644ec7fe87a2e41d2033fdaaba7ac2 |
|
BLAKE2b-256 | 2603f46fd11e6504b274d98acbaa004bc44210164cb5847793db9afa273a1ff9 |