Giter Club home page Giter Club logo

keyof's Introduction

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.

keyof's People

Contributors

viliamv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jdvor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.