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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

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