top | item 32562628

(no title)

evilsnoopi3 | 3 years ago

You can also use TypedDict if you would normally use a raw dictionary but want the type checking.

discuss

order

LtWorf|3 years ago

Yes, but you still need a module like typedload to do the runtime checking.

TypedDict performs no checking by itself at runtime.

    class A(TypedDict):
        a: int


    A(d=32)
    # Returns {'d': 32}
    typedload.load({'d': 32}, A)
    # TypedloadValueError: Value does not contain fields: {'a'} which are necessary for type A

ghostwriter|3 years ago

unlike fixed records, dictionaries access is impossible to optimise via JIT like PyPy