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

False positive: Type objects (including type[cls]) are incompatible with Hashable #17730

Closed
aatle opened this issue Sep 2, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@aatle
Copy link
Contributor

aatle commented Sep 2, 2024

Bug Report
In python, classes themselves, like int, are hashable.
A variable annotated with just type is correctly recognized as an instance of collections.abc.Hashable; however, a variable annotated with type[int], Type, or Type[int] is wrongly considered incompatible with Hashable.

To Reproduce

from collections.abc import Hashable
from typing import Type, reveal_type

a: type = int
b: type[int] = int
c: Type = int

x: Hashable = a
y: Hashable = b
z: Hashable = c

reveal_type(a)
reveal_type(b)
reveal_type(c)

Expected Behavior

No error, and consistent among x, y, and z.

Actual Behavior
Both y and z give an error, but not x.

main.py:9: error: Incompatible types in assignment (expression has type "type[int]", variable has type "Hashable")  [assignment]
main.py:10: error: Incompatible types in assignment (expression has type "type[Any]", variable has type "Hashable")  [assignment]
main.py:12: note: Revealed type is "builtins.type"
main.py:13: note: Revealed type is "type[builtins.int]"
main.py:14: note: Revealed type is "type[Any]"

Note: type and Type are considered different: the problem lies with Type.
Your Environment

  • Mypy version used: 1.11.2
  • Mypy command-line flags: default playground
  • Python version used: 3.12
@aatle aatle added the bug mypy got something wrong label Sep 2, 2024
@chadrik
Copy link
Contributor

chadrik commented Oct 22, 2024

This duplicates #11470

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants