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] | dict[str, object], b: Literal['pika'] | None = None) -> type[C]:
x: set[str] = set(a)
y: frozenset[str] = frozenset(['y1', 'y2'])
t: tuple[int, int] = (1, 2)
print(f'it works! a: {a!r}, b: {b!r}')
return C
f(['a', 'b'], '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 future_typing.py 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 [str ],typing___.Dict [str ,str ]],b :typing___.Union [Literal ['pika'],None ]=None )->typing___.Type [C ]:
x :typing___.Set [str ]=set (a )
t :typing___.Tuple [int ,...]=(1 ,2 )
print (f'it works! a: {a!r}, b: {b!r}')
return C
f (['a','b'],'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.2.0.tar.gz
(5.3 kB
view details)
Built Distribution
File details
Details for the file future_typing-0.2.0.tar.gz
.
File metadata
- Download URL: future_typing-0.2.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb9b2ac99cbfa4a90057c233d2de2a6f7d3698cf0dbf28ecdc95c52a57ad89f7 |
|
MD5 | a4299218bba0e9423240592c72e1a48b |
|
BLAKE2b-256 | 2349cfafe04c4daad77ca0eebbb09ba3439d279a5c15daa0e43e2afc278c5b59 |
File details
Details for the file future_typing-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: future_typing-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 358c2864ab8a178abc8ddd68b834b6dc7a40845566eabec2a4963c6e314b4507 |
|
MD5 | 7197190611886f2e0da69ec8b60aef56 |
|
BLAKE2b-256 | f72b56a885821f8ee553678904e0f466bce79b5ac570971b4153ccce52a26b20 |