Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incompatible assignment using TypedDict in Generic class #12385

Description

@rayansostenes
# test.py
from typing import Generic, Type, TypedDict, TypeVar


_TA = TypeVar("_TA")
_TB = TypeVar("_TB")


class GenericBase(Generic[_TA, _TB]):
    type_a: Type[_TA]
    type_b: Type[_TB]


A = TypedDict("A", {})
B = TypedDict("B", {})


class GenericChild(GenericBase[A, B]):
    type_a = A
    type_b = B

https://mypy-play.net/?mypy=latest&python=3.10&gist=6223ebcaa996d172199c2ba03b3b677f

I think I found a bug when using TypedDict as a generic argument, unless I'm missing something the code above should type check, and it does on pyright, but mypy returns a weird error message.

$ mypy test.py               
test.py:21: error: Incompatible types in assignment (expression has type "Type[A]", base class "GenericBase" defined the type as "Type[A]")
test.py:22: error: Incompatible types in assignment (expression has type "Type[B]", base class "GenericBase" defined the type as "Type[B]")
Found 2 errors in 1 file (checked 1 source file)
$ npx pyright test.py 
No configuration file found.
Assuming Python platform Darwin
Searching for source files
Found 1 source file
0 errors, 0 warnings, 0 informations 
Completed in 0.453sec

Your Environment

  • Mypy version used: 0.941
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.9
  • Operating system and version: MacOS 12.1 (21C52) [Apple Silicon]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions