python / python/mypy

dict_keys is identified as hashable

Open
#14,674 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy seems to think that dict_keys ({}.keys()) are hashable, which they are not.

To Reproduce

def f(a: Hashable) -> int:
    return 1
f({}.keys())  # does not raise an issue

Expected Behavior

Since dict-keys are not hashable (hash({}.keys()) fails and isinstance({}.keys(), Hashable) == False) I would expect mypy to throw an error in the above example.

Actual Behavior

Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 0.982
  • Mypy command-line flags: /
  • Mypy configuration options from mypy.ini (and other config files): /
  • Python version used: 3.10.8

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproducer with mypy and tracing how dict_keys is checked against Hashable. Done means mypy reports an error for f({}.keys()) and a regression test covers the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.