Skip to main content

Backport for type hinting generics in standard collections and union types as X | Y

Project description

future-typing

Tests codecov pypi versions license

Backport for type hinting generics in standard collections and union types as X | Y

(greatly inspired by future-annotations!)

Installation

    pip install future_typing

Usage

Just add this custom source code encoding at the top of your file

# -*- coding: future_typing -*-

and then use | and list[str], dict[str, int], ... as if you were using python 3.10

# -*- coding: future_typing -*-
from typing import Literal


class C:
    @staticmethod
    def g(t: tuple[int, ...]) -> tuple[int, ...]:
        return t


def f(a: list[str | int] | dict[str, str], b: Literal['pika'] | None = None) -> type[C]:
    x: set[str | int] = set(a)
    y: frozenset[str] = frozenset(['y1', 'y2'])
    t: tuple[int, ...] = (1, 2)
    print(f'it works! a: {a!r}, b: {b!r}')
    return C


f(['a', 'b', 1], 'pika')
$ python3.8 pika.py
it works! a: ['a', 'b'], b: 'pika'

$ mypy pika.py
Success: no issues found in 1 source file

See transformed source

$ python3.8 -m future_typing pika.py
# -*- coding: utf-8 -*-
import typing as typing___
from typing import Literal


class C :
    @staticmethod
    def g (t :typing___.Tuple [int ,...])->typing___.Tuple [int ,...]:
        return t


def f (a :typing___.Union [typing___.List [typing___.Union [str ,int ]],typing___.Dict [str ,str ]],b :typing___.Union [Literal ['pika'],None ]=None )->typing___.Type [C ]:
    x :typing___.Set [typing___.Union [str ,int ]]=set (a )
    y :typing___.FrozenSet [str ]=frozenset (['y1','y2'])
    t :typing___.Tuple [int ,...]=(1 ,2 )
    print (f'it works! a: {a!r}, b: {b!r}')
    return C


f (['a','b',1 ],'pika')

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

future_typing-0.3.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

future_typing-0.3.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file future_typing-0.3.1.tar.gz.

File metadata

  • Download URL: future_typing-0.3.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0rc1+

File hashes

Hashes for future_typing-0.3.1.tar.gz
Algorithm Hash digest
SHA256 42fa7002f42b8edd6c6178144d55b992c272bca9e2ac193bc34c44caa31e5d20
MD5 c00e63b4e4204c803756db73ce37917b
BLAKE2b-256 65896881ed20aaeecff2fac2b7ea8ba9a014bcbe4a2cb803243abfd4284c178d

See more details on using hashes here.

File details

Details for the file future_typing-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: future_typing-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0rc1+

File hashes

Hashes for future_typing-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 74c84c2b6b9d3fc2bb6a5f79a26f9adf657b0cdb3ccae5e17e42aaea6c932b77
MD5 67d0e4b77b4778eae6949aea3c1fd835
BLAKE2b-256 d249053b9ddbca33cae8bc730f132ff53abcb13345ea8b8cca3d0515e19dc7f3

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