Skip to main content

future-typing

Tests codecov pypi versions license

Use generic type hints and new union syntax | with python 3.6+

If you just want to use new annotations for type checkers like mypy, then do not use this library and simply add from __future__ import annotations. But if you want to use those annotations at runtime, then you may be at the right place!

This library exposes:

  • transform_annotation, which will transform list[str|int|float] into

    • typing.List[typing.Union[str, int, float]] for python 3.6 to 3.8
    • list[typing.Union[str, int, float]] for python 3.9 (since generic types are natively supported)
  • a custom source code encoding future_typing that you can use to trigger the transformation at interpretation time

  • a CLI to see the transformed source

Installation

    pip install future_typing

Codec

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

CLI

$ future_typing pika.py
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')

Metadata

Release files for future-typing 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for future-typing 0.4.1
File Size Uploaded Provenance
future_typing-0.4.1.tar.gz 6.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for future-typing 0.4.1
File Interpreter ABI Platform
future_typing-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / future_typing-0.4.1.tar.gz

Download URL future_typing-0.4.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
65fdc5034a95db212790fee5e977fb0a2df8deb60dccf3bac17d6d2b1a9bbacd
BLAKE2b-256 checksum
How to use checksums
bc14374fc27007ada5eb5eb3ff2f2620146c9e3397112575344a26f0d60cc14d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.7

Release files / future_typing-0.4.1-py3-none-any.whl

Download URL future_typing-0.4.1-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af19e06b7fe6fdfb8d83c265a2688cba9ac74b0a6a2dac14c268910ecd14ac8d
BLAKE2b-256 checksum
How to use checksums
d30cdfd01088cb3ad4b1c2a1ebc46839dc3254f2241ba324e87d80145a38a000
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.7

Security for future-typing 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page