Use the latest type annotation features in older versions of Python.
Project description
Overview
Use the latest type annotation features in older versions of Python.
Motivation
When working with Python development for VFX pipelines we are often stuck with older Python versions for the runtime.
Tippo aims to bridge that gap since it allows us to use features such as static type checking (which could be performed by newer Python versions with support for MyPy during the testing phase) even though the code might be designed to run in Python 2.7, for example.
Installation
Tippo is available through pip:
pip install tippo
Usage
Instead of importing from typing and/or typing_extensions…
>>> try:
... from typing import TypeAlias
... except ImportError:
... from typing_extensions import TypeAlias
...
>>> try:
... from typing import final
... except ImportError:
... from typing_extensions import final
...
…just import directly from tippo:
>>> from tippo import TypeAlias, final
Generic Fixes
Tippo patches GenericMeta to fix known bugs for the Python 2.7 version of typing that were not addressed since it’s not officially supported anymore.
Generic class comparison:
>>> from tippo import Mapping
>>> assert Mapping[str, int] == Mapping[str, int] # passes
>>> assert not (Mapping[str, int] != Mapping[str, int]) # passes
Subclassing a generic class with a __weakref__ slot:
>>> from weakref import ref
>>> from tippo import Generic, TypeVar
>>> T = TypeVar("T")
>>> class MyGeneric(Generic[T]):
... __slots__ = ("__weakref__",)
...
>>> class SubClass(MyGeneric[T]): # does not error out
... __slots__ = ()
...
>>> instance = SubClass()
>>> instance_ref = ref(instance)
In order to maintain the same interface, GenericMeta points to type when imported from tippo in newer versions of Python.
Backports
Features from the latest versions of Python, such as TypeAlias, ClassVar, NewType, get_origin, and get_args.
>>> from tippo import Mapping, get_args, get_name
>>> mapping_type = Mapping[str, int]
>>> [get_name(a) for a in get_args(mapping_type)]
['str', 'int']
Commonly Used Protocols
Such as:
tippo.SupportsGetItem
tippo.SupportsGetSetItem
tippo.SupportsGetSetDeleteItem
tippo.SupportsKeysAndGetItem
>>> from tippo import SupportsGetItem
>>> class Foo(object):
... def __getitem__(self, item):
... # type: (str) -> int
... return 3
...
>>> def get_stuff(bar):
... # type: (SupportsGetItem[str, int]) -> int
... return bar["stuff"]
...
>>> assert get_stuff(Foo()) == 3 # passes static type checking
>>> assert get_stuff({"stuff": 3}) == 3 # passes static type checking
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 tippo-4.1.1.tar.gz
.
File metadata
- Download URL: tippo-4.1.1.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1267825bccaa4c38d285d04b8f89ad82686cd34dece22a0f4b40000c959de78a |
|
MD5 | 54ccfdae5f489e8758f7ac7b0c4054bb |
|
BLAKE2b-256 | 49ecb9fbe6569b331db291de44f81a9c66d0a696fbc8f65723bc5b32e4687276 |
File details
Details for the file tippo-4.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: tippo-4.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e770b15c471c39720c49a7fd8cd1109466ff91a338302582cbcc5ec35a772e32 |
|
MD5 | 5cac4af5969c9d6b0c6b7891f4d256c4 |
|
BLAKE2b-256 | 395332425bf32e40046a2b9c97e2aa11fcbe6037bcab5bb3190b845ae3d56853 |