Skip to main content

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.

Metadata

Release files for keyof 0.4.0

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

Source distribution (sdist)

Source distribution for keyof 0.4.0
File Size Uploaded
keyof-0.4.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keyof 0.4.0
File Interpreter ABI Platform
keyof-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / keyof-0.4.0.tar.gz

Download URL keyof-0.4.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5419812608ca40a73f4cdd3b0c2da4aaab304a7ea8163e6311d7a66239faf05a
BLAKE2b-256 checksum
How to use checksums
00748326ea4919469e6381bad6392a1f50d198b22bdaff57ea451947183168f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Darwin/23.1.0

Release files / keyof-0.4.0-py3-none-any.whl

Download URL keyof-0.4.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ccdb780643b4fae999257c7afc3cdae7c525534e24a0617d04b251f86103cde8
BLAKE2b-256 checksum
How to use checksums
d22f8866baa7896d27356d48b369a48f4ba4f985489feaebb64132361e3788b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Darwin/23.1.0

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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