Skip to main content

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

Project description

build status pre-commit.ci status

typing-derive

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

Installation

pip install typing-derive

usage

add as a mypy plugin

[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

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

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

x = 5

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

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

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

typing_derive-1.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typing_derive-1.2.0-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file typing_derive-1.2.0.tar.gz.

File metadata

  • Download URL: typing_derive-1.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for typing_derive-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a1a752561e436e9b8c59df9b29b02911a2705b825e7137371b0e5f36b3954fc6
MD5 2c37251c2cb797acd060ba061295e113
BLAKE2b-256 067f47c7d462780020d79a082b0aa2b7fa6709c31d780bc19708b2e2dbccc81a

See more details on using hashes here.

File details

Details for the file typing_derive-1.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for typing_derive-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ff02cff476410ffdecbd86358ab1b046be721825217bb1aa73df8b1e141c0351
MD5 9441026698abe2e0f24b70564c0afb2c
BLAKE2b-256 fc10c905fd82674e8feaffb9f4a88e340fe199033d4b7c8e102825b791620be2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page