future-typing


Namefuture-typing JSON
Version 0.4.1 PyPI version JSON
download
home_pagehttps://github.com/PrettyWood/future-typing
SummaryUse generic type hints and new union syntax `|` with python 3.6+
upload_time2021-05-14 12:05:42
maintainer
docs_urlNone
authorEric Jolibois
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # future-typing
[![Tests](https://github.com/PrettyWood/future-typing/workflows/Tests/badge.svg)](https://github.com/PrettyWood/future-typing/actions)
[![codecov](https://codecov.io/gh/PrettyWood/future-typing/branch/main/graph/badge.svg)](https://codecov.io/gh/PrettyWood/future-typing)
[![pypi](https://img.shields.io/pypi/v/future-typing.svg)](https://pypi.python.org/pypi/future-typing)
[![versions](https://img.shields.io/pypi/pyversions/future-typing.svg)](https://github.com/PrettyWood/future-typing)
[![license](https://img.shields.io/github/license/PrettyWood/future-typing.svg)](https://github.com/PrettyWood/future-typing/blob/master/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

``` bash
    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`

```python
# -*- 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')
```

```console
$ python3.8 pika.py
it works! a: ['a', 'b'], b: 'pika'

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

## CLI
```console
$ 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')
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PrettyWood/future-typing",
    "name": "future-typing",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Eric Jolibois",
    "author_email": "em.jolibois@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bc/14/374fc27007ada5eb5eb3ff2f2620146c9e3397112575344a26f0d60cc14d/future_typing-0.4.1.tar.gz",
    "platform": "",
    "description": "# future-typing\n[![Tests](https://github.com/PrettyWood/future-typing/workflows/Tests/badge.svg)](https://github.com/PrettyWood/future-typing/actions)\n[![codecov](https://codecov.io/gh/PrettyWood/future-typing/branch/main/graph/badge.svg)](https://codecov.io/gh/PrettyWood/future-typing)\n[![pypi](https://img.shields.io/pypi/v/future-typing.svg)](https://pypi.python.org/pypi/future-typing)\n[![versions](https://img.shields.io/pypi/pyversions/future-typing.svg)](https://github.com/PrettyWood/future-typing)\n[![license](https://img.shields.io/github/license/PrettyWood/future-typing.svg)](https://github.com/PrettyWood/future-typing/blob/master/LICENSE)\n\nUse generic type hints and new union syntax `|` with python 3.6+\n\nIf you just want to use new annotations for type checkers like `mypy`, then do not use this library\nand simply add `from __future__ import annotations`.\nBut if you want to use those annotations at runtime, then you may be at the right place!\n\nThis library exposes:\n\n- `transform_annotation`, which will transform `list[str|int|float]` into\n  * `typing.List[typing.Union[str, int, float]]` for python 3.6 to 3.8\n  * `list[typing.Union[str, int, float]]` for python 3.9 (since generic types are natively supported)\n\n- a custom source code encoding `future_typing` that you can use to trigger the transformation at\n  interpretation time\n\n- a CLI to see the transformed source\n\n## Installation\n\n``` bash\n    pip install future_typing\n```\n\n## Codec\nJust add this custom source code encoding at the top of your file\n```\n# -*- coding: future_typing -*-\n```\n\nand then use `|` and `list[str]`, `dict[str, int]`, ... as if you were using `python 3.10`\n\n```python\n# -*- coding: future_typing -*-\nfrom typing import Literal\n\n\nclass C:\n    @staticmethod\n    def g(t: tuple[int, ...]) -> tuple[int, ...]:\n        return t\n\n\ndef f(a: list[str | int] | dict[str, str], b: Literal['pika'] | None = None) -> type[C]:\n    x: set[str | int] = set(a)\n    y: frozenset[str] = frozenset(['y1', 'y2'])\n    t: tuple[int, ...] = (1, 2)\n    print(f'it works! a: {a!r}, b: {b!r}')\n    return C\n\n\nf(['a', 'b', 1], 'pika')\n```\n\n```console\n$ python3.8 pika.py\nit works! a: ['a', 'b'], b: 'pika'\n\n$ mypy pika.py\nSuccess: no issues found in 1 source file\n```\n\n## CLI\n```console\n$ future_typing pika.py\nimport typing as typing___\nfrom typing import Literal\n\n\nclass C :\n    @staticmethod\n    def g (t :typing___.Tuple [int ,...])->typing___.Tuple [int ,...]:\n        return t\n\n\ndef f (a :typing___.Union [typing___.List [typing___.Union [str ,int ]],typing___.Dict [str ,str ]],b :typing___.Union [Literal ['pika'],None ]=None )->typing___.Type [C ]:\n    x :typing___.Set [typing___.Union [str ,int ]]=set (a )\n    y :typing___.FrozenSet [str ]=frozenset (['y1','y2'])\n    t :typing___.Tuple [int ,...]=(1 ,2 )\n    print (f'it works! a: {a!r}, b: {b!r}')\n    return C\n\n\nf (['a','b',1 ],'pika')\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Use generic type hints and new union syntax `|` with python 3.6+",
    "version": "0.4.1",
    "project_urls": {
        "Homepage": "https://github.com/PrettyWood/future-typing"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d30cdfd01088cb3ad4b1c2a1ebc46839dc3254f2241ba324e87d80145a38a000",
                "md5": "801b170e4cbb2b74bfbce8fe52c5f2af",
                "sha256": "af19e06b7fe6fdfb8d83c265a2688cba9ac74b0a6a2dac14c268910ecd14ac8d"
            },
            "downloads": -1,
            "filename": "future_typing-0.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "801b170e4cbb2b74bfbce8fe52c5f2af",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 7485,
            "upload_time": "2021-05-14T12:05:40",
            "upload_time_iso_8601": "2021-05-14T12:05:40.785275Z",
            "url": "https://files.pythonhosted.org/packages/d3/0c/dfd01088cb3ad4b1c2a1ebc46839dc3254f2241ba324e87d80145a38a000/future_typing-0.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc14374fc27007ada5eb5eb3ff2f2620146c9e3397112575344a26f0d60cc14d",
                "md5": "4027910d0760a30f6826ae4075f7e13e",
                "sha256": "65fdc5034a95db212790fee5e977fb0a2df8deb60dccf3bac17d6d2b1a9bbacd"
            },
            "downloads": -1,
            "filename": "future_typing-0.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "4027910d0760a30f6826ae4075f7e13e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 5958,
            "upload_time": "2021-05-14T12:05:42",
            "upload_time_iso_8601": "2021-05-14T12:05:42.310147Z",
            "url": "https://files.pythonhosted.org/packages/bc/14/374fc27007ada5eb5eb3ff2f2620146c9e3397112575344a26f0d60cc14d/future_typing-0.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-05-14 12:05:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PrettyWood",
    "github_project": "future-typing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "future-typing"
}
        
Elapsed time: 0.32928s