Skip to main content

No project description provided

Project description

KeyOf

Mypy plugin for static type checking of TypedDict keys inspired by TypeScript's keyof type operator.

Requirements

  • python>=3.11
  • mypy>=1.0.1

Installation

pip install keyof

Mypy plugin

Add keyof.mypy_plugin to the list of plugins in your mypy config file (for example pyproject.toml)

[tool.mypy]
plugins = ["keyof.mypy_plugin"]

Features

  • KeyOf, RequiredKeyOf, and NotRequiredKeyOf types

  • ✅ Supports inheritance

  • ✅ Plays nicely with other types, e.g. KeyOf[Foo] | Literal["bar"]

  • ✅ Compatibility module for Pylance and Pyright

  • ✅ Zero dependencies

  • ❌ Generic TypeVar arguments

Usage

from typing import TypedDict

from keyof import KeyOf


class Data(TypedDict):
    version: int
    command: str


def get_data(data: Data, key: KeyOf[Data]) -> int | str:
    return data[key]


data = Data(version=1, command="foo")

get_data(data, "version")  # OK

get_data(data, "foo")
# mypy catches the error:
# error: Argument 2 to "get_data" has incompatible type "Literal['foo']"; expected "Literal['version', 'command']"

Usage with other type checkers

Since Pylance and Pyright don't support plugins and cannot correctly handle subclassing of Any (new in Python 3.11) there is compatibility module keyof.compat that exports the same types but they are only TypeAlias for Any.

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

keyof-0.4.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

keyof-0.4.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file keyof-0.4.0.tar.gz.

File metadata

  • Download URL: keyof-0.4.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Darwin/23.1.0

File hashes

Hashes for keyof-0.4.0.tar.gz
Algorithm Hash digest
SHA256 5419812608ca40a73f4cdd3b0c2da4aaab304a7ea8163e6311d7a66239faf05a
MD5 603c79f231b8f0f77d46b727036c98c5
BLAKE2b-256 00748326ea4919469e6381bad6392a1f50d198b22bdaff57ea451947183168f3

See more details on using hashes here.

File details

Details for the file keyof-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: keyof-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Darwin/23.1.0

File hashes

Hashes for keyof-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccdb780643b4fae999257c7afc3cdae7c525534e24a0617d04b251f86103cde8
MD5 d925d326fede2c864620d4b11ea33451
BLAKE2b-256 d22f8866baa7896d27356d48b369a48f4ba4f985489feaebb64132361e3788b9

See more details on using hashes here.

Supported by

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