Skip to content

TypedDict: Argument 1 to "f" has incompatible type "test.C"; expected "C" #5593

@sk-

Description

@sk-

The following code fails to typecheck:

from typing import Any, Type, TypeVar
from mypy_extensions import TypedDict

_T = TypeVar('_T')

class C(TypedDict):
  x: str

class D:
  x: str

def gen_row() -> Any:
  return {'x': 1}

def foo(cls: Type[_T]) -> _T:
  return gen_row()

def print_c(c: C):
  print(c)

def print_d(d: D):
  print(d)

print_c(foo(C))
print_d(foo(D))
reveal_type(foo(C))
reveal_type(foo(D))

producing the following error:

$ mypy test.py
test.py:24: error: Argument 1 to "print_c" has incompatible type "test.C"; expected "C"
test.py:26: error: Revealed type is 'test.C*'
test.py:27: error: Revealed type is 'test.D*'

I think this is a bug related to TypedDict, as using any other class (like D in the sample code) works just fine.

Note also that one cannot tighten T with a bound=TypedDict, as that produces the error:
Invalid type "mypy_extensions.TypedDict".

  • What are the versions of mypy and Python you are using?
$ mypy --version
mypy 0.620
$ python --version
Python 3.6.5
  • What are the mypy flags you are using? (For example --strict-optional)
    mypy.ini is:
[mypy]
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
check_untyped_defs = True

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions