python / python/mypy

Non-hashable tuple should not be accepted as `Hashable`

Open
#19,232 0 comments 0 reactions 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

I'd expect to get a type error when trying to pass a tuple to a Hashable bound if that tuple contains any non-hashable element type.

To Reproduce

from typing import Hashable, TypeVar

T = TypeVar("T", bound=Hashable)

def f(x: T) -> T:
    print(hash(x))
    return x
    
def usage1(x: tuple[dict[str, str], ...]):
    f(x)

def usage2(x: tuple[dict[str, str], int, int]):
    f(x)

def usage3(x: tuple[int, int, dict[str, str]]):
    f(x)

Expected Behavior

Type error in all f(x) lines, rejecting the non-hashable type.

Actual Behavior

The type check passes despite the unsound typing.

Your Environment

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

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 with the provided Python 3.12 reproduction using mypy 1.16.0, then inspect the type-checking path for Hashable bounds and tuple element types. Add regression coverage for usage1, usage2, and usage3; the work is done when all three calls are rejected.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.