Incompatible assignment using TypedDict in Generic class #12385
Copy link
Copy link
Closed
Labels
bugmypy got something wrongmypy got something wrongtopic-type-formTypeForm might fix thisTypeForm might fix thistopic-typed-dicttopic-usability
Description
# 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 = Bhttps://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.453secYour 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]
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-type-formTypeForm might fix thisTypeForm might fix thistopic-typed-dicttopic-usability
Activity
JelleZijlstra commentedon Mar 19, 2022
This is because TypedDict isn't compatible with
type[]. See #11644, #9003, #9773.Dreamsorcerer commentedon Apr 17, 2022
Another simple use case that fails from this:
The output is
Revealed type is "builtins.object*"If I try to annotate the argument with:
args1: D = t(D)Then I get the confusing error:
Incompatible types in assignment (expression has type "D", variable has type "D")erictraut commentedon Aug 13, 2023
This bug appears to have been fixed. I've confirmed that the error no longer occurs in mypy 1.5.