No project description provided
Project description
KeyOf
Mypy plugin for static type checking of TypedDict keys
inspired by TypeScript's keyof
type operator.
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"]
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, "not_existing_key") # error
# Argument 2 to "get_data" has incompatible type "Literal['not_existing_key']"; expected "Literal['version', 'command']"
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.1.0.tar.gz
(3.8 kB
view hashes)
Built Distribution
keyof-0.1.0-py3-none-any.whl
(3.9 kB
view hashes)