Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible assignment using TypedDict in Generic class #12385

Closed
rayansostenes opened this issue Mar 19, 2022 · 3 comments
Closed

Incompatible assignment using TypedDict in Generic class #12385

rayansostenes opened this issue Mar 19, 2022 · 3 comments
Labels
bug mypy got something wrong topic-type-form TypeForm might fix this topic-typed-dict topic-usability

Comments

@rayansostenes
Copy link

# 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]
@rayansostenes rayansostenes added the bug mypy got something wrong label Mar 19, 2022
@JelleZijlstra
Copy link
Member

This is because TypedDict isn't compatible with type[]. See #11644, #9003, #9773.

@Dreamsorcerer
Copy link
Contributor

Another simple use case that fails from this:

class D(TypedDict):
    a: int

T = TypeVar("T")
def t(t: Type[T]) -> T: ...

d = t(D)
reveal_type(d["a"])

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
Copy link

This bug appears to have been fixed. I've confirmed that the error no longer occurs in mypy 1.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-form TypeForm might fix this topic-typed-dict topic-usability
Projects
None yet
Development

No branches or pull requests

6 participants