typing-derive


Nametyping-derive JSON
Version 1.2.0 PyPI version JSON
download
home_pagehttps://github.com/asottile/typing-derive
Summaryderive types from other types to make it easier to type code!
upload_time2025-09-06 18:09:11
maintainerNone
docs_urlNone
authorAnthony Sottile
requires_python>=3.12
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![build status](https://github.com/asottile/typing-derive/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/typing-derive/actions/workflows/main.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/typing-derive/main.svg)](https://results.pre-commit.ci/latest/github/asottile/typing-derive/main)

typing-derive
=============

derive types from other types to make it easier to type code!

## Installation

```bash
pip install typing-derive
```

## usage

add as a mypy plugin

```ini
[mypy]
plugins = typing_derive.plugin
```

### `typing_derive.impl.typeddict_from_func`

create a usable `TypedDict` from some callable.  useful if you need to
dynamically build up `**kwargs` to call a function

```python
from typing_derive.impl import typeddict_from_func

def f(x: int, y: str) -> None: ...

TD = typeddict_from_func('TD', f)

x: TD = {
    'x': 1,
    'y': 'hello hello',
}

f(**x)
```

### `typing_derive.impl.typeof`

create a type alias for the type of a function / variable.

one use might be to pass functions of matching signatures as objects

```python
def f(x: int, y: str) -> None: ...

F = typeof('F', f)

def g(func: F) -> None:
    func(x=1, y='two')

def h(x: int, y: str) -> None: ...
def j(x: str, y: str) -> None: ...
def k(x1: int, y: str) -> None: ...

g(f)  # ok
g(h)  # ok
g(j)  # error: `x` is `str` not `int`
g(k)  # error: `x1` mismatches `x`
```

it also just works on normal variables too -- though I haven't come up with a
use for this yet

```python
x = 5

X = typeof('X', x)  # effectively `type X = int`

y: X = 6  # ok
z: X = 'no'  # not ok!
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/asottile/typing-derive",
    "name": "typing-derive",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": null,
    "author": "Anthony Sottile",
    "author_email": "asottile@umich.edu",
    "download_url": "https://files.pythonhosted.org/packages/06/7f/47c7d462780020d79a082b0aa2b7fa6709c31d780bc19708b2e2dbccc81a/typing_derive-1.2.0.tar.gz",
    "platform": null,
    "description": "[![build status](https://github.com/asottile/typing-derive/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/typing-derive/actions/workflows/main.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/typing-derive/main.svg)](https://results.pre-commit.ci/latest/github/asottile/typing-derive/main)\n\ntyping-derive\n=============\n\nderive types from other types to make it easier to type code!\n\n## Installation\n\n```bash\npip install typing-derive\n```\n\n## usage\n\nadd as a mypy plugin\n\n```ini\n[mypy]\nplugins = typing_derive.plugin\n```\n\n### `typing_derive.impl.typeddict_from_func`\n\ncreate a usable `TypedDict` from some callable.  useful if you need to\ndynamically build up `**kwargs` to call a function\n\n```python\nfrom typing_derive.impl import typeddict_from_func\n\ndef f(x: int, y: str) -> None: ...\n\nTD = typeddict_from_func('TD', f)\n\nx: TD = {\n    'x': 1,\n    'y': 'hello hello',\n}\n\nf(**x)\n```\n\n### `typing_derive.impl.typeof`\n\ncreate a type alias for the type of a function / variable.\n\none use might be to pass functions of matching signatures as objects\n\n```python\ndef f(x: int, y: str) -> None: ...\n\nF = typeof('F', f)\n\ndef g(func: F) -> None:\n    func(x=1, y='two')\n\ndef h(x: int, y: str) -> None: ...\ndef j(x: str, y: str) -> None: ...\ndef k(x1: int, y: str) -> None: ...\n\ng(f)  # ok\ng(h)  # ok\ng(j)  # error: `x` is `str` not `int`\ng(k)  # error: `x1` mismatches `x`\n```\n\nit also just works on normal variables too -- though I haven't come up with a\nuse for this yet\n\n```python\nx = 5\n\nX = typeof('X', x)  # effectively `type X = int`\n\ny: X = 6  # ok\nz: X = 'no'  # not ok!\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "derive types from other types to make it easier to type code!",
    "version": "1.2.0",
    "project_urls": {
        "Homepage": "https://github.com/asottile/typing-derive"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "fc10c905fd82674e8feaffb9f4a88e340fe199033d4b7c8e102825b791620be2",
                "md5": "9441026698abe2e0f24b70564c0afb2c",
                "sha256": "ff02cff476410ffdecbd86358ab1b046be721825217bb1aa73df8b1e141c0351"
            },
            "downloads": -1,
            "filename": "typing_derive-1.2.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9441026698abe2e0f24b70564c0afb2c",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.12",
            "size": 4921,
            "upload_time": "2025-09-06T18:09:09",
            "upload_time_iso_8601": "2025-09-06T18:09:09.182530Z",
            "url": "https://files.pythonhosted.org/packages/fc/10/c905fd82674e8feaffb9f4a88e340fe199033d4b7c8e102825b791620be2/typing_derive-1.2.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "067f47c7d462780020d79a082b0aa2b7fa6709c31d780bc19708b2e2dbccc81a",
                "md5": "2c37251c2cb797acd060ba061295e113",
                "sha256": "a1a752561e436e9b8c59df9b29b02911a2705b825e7137371b0e5f36b3954fc6"
            },
            "downloads": -1,
            "filename": "typing_derive-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "2c37251c2cb797acd060ba061295e113",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 4436,
            "upload_time": "2025-09-06T18:09:11",
            "upload_time_iso_8601": "2025-09-06T18:09:11.151964Z",
            "url": "https://files.pythonhosted.org/packages/06/7f/47c7d462780020d79a082b0aa2b7fa6709c31d780bc19708b2e2dbccc81a/typing_derive-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-06 18:09:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "asottile",
    "github_project": "typing-derive",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "typing-derive"
}
        
Elapsed time: 2.73152s