Skip to content

Non-hashable tuple should not be accepted as Hashable #19232

Open
@bluenote10

Description

@bluenote10

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions