python / python/cpython

Alias that is generic over a ParamSpec is not hashable when using a list as parameters_expression

Open
#124,496 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-typing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

This is slightly related to #124445, but a different issue, maybe it can be solved at the same time?

Consider the following code where [T] is a parameters_expression according to PEP 612. Similar to using Callable or using Generic[P] or Protocol[P] I would expect the following code to work and receive a hashable variable:

from typing import TypeVar
from collections.abc import Callable
type X[**P] = Callable[P, int]
T = TypeVar("T")
hash(X[[T]])
# ~~~~^^^^^^^^
# TypeError: unhashable type: 'list'

Cause:

The problem is that X[[T]].__args__ == ([T],) which cannot be hashed. I think the list should either be turned into a tuple. Either a nested one as its done for Generic and Protocol or joined like for Callable[P, str][[int]].__args__.

Discussion:

I've opened a parallel topic on discuss

CPython versions tested on:

3.13.0.rc2

Operating systems tested on:

Linux

EDIT: A thought, should this be changed for whole class in general?

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 reproducer in the issue and compare the ParamSpec alias behavior with the referenced Generic, Protocol, and Callable cases. Read PEP 612, issue #124445, and the linked discussion to resolve whether list parameters should become nested tuples or be joined. Done means hash(X[[T]]) works without an unhashable list while preserving the intended arguments.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.