Skip to main content

Use the latest type annotation features in older versions of Python.

Project description

https://github.com/brunonicko/tippo/workflows/MyPy/badge.svg https://github.com/brunonicko/tippo/workflows/Lint/badge.svg https://github.com/brunonicko/tippo/workflows/Tests/badge.svg https://readthedocs.org/projects/tippo/badge/?version=stable https://img.shields.io/github/license/brunonicko/tippo?color=light-green https://static.pepy.tech/personalized-badge/tippo?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads https://img.shields.io/pypi/pyversions/tippo?color=light-green&style=flat

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tippo-4.1.1.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

tippo-4.1.1-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

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

Hashes for tippo-4.1.1.tar.gz
Algorithm Hash digest
SHA256 1267825bccaa4c38d285d04b8f89ad82686cd34dece22a0f4b40000c959de78a
MD5 54ccfdae5f489e8758f7ac7b0c4054bb
BLAKE2b-256 49ecb9fbe6569b331db291de44f81a9c66d0a696fbc8f65723bc5b32e4687276

See more details on using hashes here.

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

Hashes for tippo-4.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e770b15c471c39720c49a7fd8cd1109466ff91a338302582cbcc5ec35a772e32
MD5 5cac4af5969c9d6b0c6b7891f4d256c4
BLAKE2b-256 395332425bf32e40046a2b9c97e2aa11fcbe6037bcab5bb3190b845ae3d56853

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page