Backport for type hinting generics in standard collections and union types as X | Y
Project description
future-typing
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.4.0.tar.gz
(5.7 kB
view hashes)
Built Distribution
Close
Hashes for future_typing-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f0a1befa5dcc8f04db38bdc3a84286000f6ee293668e7a8b66b011f6add9662 |
|
MD5 | 1a523c43328328a5bbb538c41a6a697b |
|
BLAKE2b-256 | 9425db260363223333e37681f6766a2c932b1aec4d2252b8c955a9c00ab49a5b |