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.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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5419812608ca40a73f4cdd3b0c2da4aaab304a7ea8163e6311d7a66239faf05a
|
|
| MD5 |
603c79f231b8f0f77d46b727036c98c5
|
|
| BLAKE2b-256 |
00748326ea4919469e6381bad6392a1f50d198b22bdaff57ea451947183168f3
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccdb780643b4fae999257c7afc3cdae7c525534e24a0617d04b251f86103cde8
|
|
| MD5 |
d925d326fede2c864620d4b11ea33451
|
|
| BLAKE2b-256 |
d22f8866baa7896d27356d48b369a48f4ba4f985489feaebb64132361e3788b9
|