KeyOf
Mypy plugin for static type checking of TypedDict keys
inspired by TypeScript's keyof type operator.
Requirements
python>=3.11mypy>=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, andNotRequiredKeyOftypes -
✅ Supports inheritance
-
✅ Plays nicely with other types, e.g.
KeyOf[Foo] | Literal["bar"] -
✅ Compatibility module for
PylanceandPyright -
✅ Zero dependencies
-
❌ Generic
TypeVararguments
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)
| File | Size | Uploaded | |
|---|---|---|---|
| keyof-0.4.0.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|