Skip to content

Why are the type parameters set, frozenset, dict and frozendict are generic in unbounded? #15751

Open

Description

@jonathandung

Excuse me if this question may come across as stupid, but these hashed containers should only have hashable items. However, in builtins.pyi, I see that their type variables do not have an upper bound of collections.abc.Hashable. How is that so?

Activity

  1. JelleZijlstra commented on May 10, 2026

    @JelleZijlstra
    Member

    Historically Hashable has been a bit of a problematic type, because hashability is sometimes hard to model (e.g., object is hashable but its subclasses may not be; there is __hash__ = None). Worth trying though and seeing what mypy-primer has to say.

  2. jonathandung commented on May 10, 2026

    @jonathandung
    ContributorAuthor

    Should I open a PR?

  3. sobolevn commented on May 10, 2026

    @sobolevn
    Member

    Moreover, this type is not really sound, because some subclasses of hashable types can be unhashable. Or even different instances can differ in hashability :(

  4. jonathandung commented on May 10, 2026

    @jonathandung
    ContributorAuthor

    True. It would also be exceedingly difficult to make such a big change to builtins.pyi, especially as it would likely involve updating collections.abc as well, and type checkers may have to rework some special casing. Overall, this approach may be infeasible.

  5. JelleZijlstra commented on May 10, 2026

    @JelleZijlstra
    Member

    It's worth at least trying and seeing what typeshed's CI has to say.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions