12

Suppose I have some code like

def get_x(d: dict) -> int:
    d["x"]

However, I want to tell mypy that d should only contain certain keys (only the "x" key for example). That way, if I make a mistake lower in the code trying to reference an invalid key of d, mypy will trigger an error.

My question is:

  1. Is this possible? Can mypy validate dictionary keys?
  2. If yes, how is this done? If no, is there a preferred workaround?
2

2 Answers 2

12

Ran into this problem too when trying to put some typing around AWS API Gateway/Lambda integration.

As pointed out by @Michael0x2a in the comments, TypedDict would seem to be the way to go, especially because it does not require any conversions yourself before passing into a function (a requirement for API responses, like you mentioned).

from mypy_extensions import TypedDict

DictWithOnlyX = TypedDict('DictWithOnlyX', {"x": str})

# error: Extra key 'y' for TypedDict "DictWithOnlyX@58"
dx1: DictWithOnlyX = { "y": 123 }

# error: Incompatible types (expression has type "int",
#        TypedDict item "x" has type "str")
dx2: DictWithOnlyX = { "x": 123 }

# GOOD!
dx3: DictWithOnlyX = { "x": "123" }

Should also note that, if dependencies are segregated between production and dev/test, then this makes mypy a production dependency.

1

There are a few ways and assuming that your data structure must only have the same attributes, but they don't use dicts.

Instead of passing in a dict, you can use a namedtuple. Using a namedtuple, you can treat it as an object, but no new fields can be added either.

from collections import namedtuple

A = namedtuple("A", ["x", "y", "z"])

def get_x(d: A) -> int:
    d.x

Another way is to create a class and use the __slot__ attribute. This also makes it so no new attributes can be accidentally inserted.

class A:
    __slot__ = ["x", "y", "z"]

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

Using the above methods, you must define the fields at the very beginning.

3
  • 1
    Context: I'm testing APIs and the APIs are returning JSON. I don't want to have to convert each JSON into a namedtuple. I want some type checking in terms of what the client-side methods return. Is there a way for me to narrow down the dict type? Feb 2, 2019 at 4:39
  • Nope. Don't think so. Python is an interpreted language. You can create a function that checks the existing keys with a set of keys and see if there are any that shouldn't be there.
    – drum
    Feb 2, 2019 at 15:17
  • 1
    I want to do this at the type level - I want mypy to find this mistake. I already have runtime checking but it’s not the same. Feb 2, 2019 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.