"dict[str, Unknown]" is incompatible with my custom TypedDict

Viewed 74

Given this custom TypedDict TMyDict:

class TMyDict(TypedDict, total=False):
  prop_a: int
  prop_b: int

This is ok:

def get_my_dict_works() -> TMyDict:
  return {
    'prop_a': 0,
    'prop_b': 1
  }

But this don't:

def get_my_dict_fail() -> TMyDict:
  d= {
    'prop_a': 0,
    'prop_b': 1
  }
  return d

Error message is:

  Expression of type "dict[str, Unknown]" cannot be assigned to return type "TMyDict"
     "dict[str, Unknown]" is incompatible with "TMyDict"

And it works if I add the type annotation when assigning var:

def get_my_dict_fix() -> TMyDict:
  d: TMyDict = {
    'prop_a': 0,
    'prop_b': 1
  }
  return d

Why?

0 Answers
Related