MyPy does not enforce Hashability

Open
#2,455 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python

Research direction

The report points to checker.py; start by tracing how mypy validates set elements and whether existing Protocol checks are handled there. Reproduce the Python 3 example and compare it with the expected rejection of Foo in a set. Done means the checker reports the unhashable class while preserving valid hashable cases.

Written by the indexing model from the issue text.

Description

feature priority-1-normal

For example, the following code is illegal in Python 3 (implementing __eq__ shadows your __hash__ implementation with None in Python 3)

class Foo(object):
  def __init__(self, x):
    self.x = x
  def __eq__(self, other):
    if not isinstance(other, Foo):
      return NotImplemented
    return self.x == other.x

set_of_foo = {Foo(1), Foo(2)}

Poking around the code, AFAICT mypy doesn't do any Hashable checks. I don't see any other Protocols being checked in checker.py. Is there any existing tickets around supporting Protocols in mypy?

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

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.

More from python/mypy

All issues in python/mypy

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.