Skip to content

Make ReservedParameterSpecification protocol in types/fields/resolver.py satisfy Hashable interface#4411

Merged
bellini666 merged 3 commits into
strawberry-graphql:mainfrom
jonathandung:main
May 14, 2026
Merged

Make ReservedParameterSpecification protocol in types/fields/resolver.py satisfy Hashable interface#4411
bellini666 merged 3 commits into
strawberry-graphql:mainfrom
jonathandung:main

Conversation

@jonathandung
Copy link
Copy Markdown
Contributor

@jonathandung jonathandung commented May 13, 2026

Description

Because instances of classes conforming to the above protocol are used as dictionary keys, they must be hashable.

Parent: python/typeshed#15754.

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Bug Fixes:

  • Make ReservedParameterSpecification protocol explicitly hashable so it can be safely used as a dictionary key.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 13, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Make the ReservedParameterSpecification protocol explicitly require a hash implementation so that conforming classes are hashable and can safely be used as dictionary keys.

File-Level Changes

Change Details Files
Require ReservedParameterSpecification implementors to be hashable via hash
  • Extend ReservedParameterSpecification protocol with a hash(self) -> int method requirement so instances can be used as dictionary keys
  • Ensure that any existing or future implementations of this protocol must provide a hash implementation or inherit one from a hashable base
strawberry/types/fields/resolver.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 13, 2026

Thanks for adding the RELEASE.md file!

Below is the changelog that will be used for the release.


The types.field.resolver.ReservedParameterSpecification protocol now requires classes to have __hash__ as well,
because instances of those classes are to be used as dictionary keys.

This release was contributed by @jonathandung in #4411

Additional contributors: @pre-commit-ci[bot]

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Since the intent is to enforce hashability, consider inheriting the protocol from collections.abc.Hashable instead of only declaring __hash__, which more clearly expresses the requirement and aligns with standard ABCs.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since the intent is to enforce hashability, consider inheriting the protocol from `collections.abc.Hashable` instead of only declaring `__hash__`, which more clearly expresses the requirement and aligns with standard ABCs.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 13, 2026

Greptile Summary

This PR adds an explicit __hash__(self) -> int: ... stub to the ReservedParameterSpecification Protocol, making it structurally compatible with the Hashable interface required by typeshed (tracked in python/typeshed#15754). The change is a single-line addition with no runtime impact.

  • All three concrete implementations (ReservedName, ReservedNameBoundParameter, ReservedType) are NamedTuple subclasses, which already derive __hash__ from tuple, so they satisfy the updated protocol without any modification.
  • This is a pure type-annotation improvement — instances of these classes are used as dict keys in the resolver machinery, and this change ensures static type checkers can verify that usage correctly.

Confidence Score: 5/5

Single-line stub addition to a Protocol with no runtime behaviour change; safe to merge.

The change only adds a method stub to a Protocol class, which has zero runtime effect. Every concrete implementation already provides __hash__ through NamedTuple inheritance, so no existing code breaks and no new code paths are introduced.

No files require special attention.

Important Files Changed

Filename Overview
strawberry/types/fields/resolver.py Adds __hash__ stub to ReservedParameterSpecification Protocol so it explicitly satisfies the Hashable interface; all concrete NamedTuple implementations already provide __hash__ automatically.

Class Diagram

%%{init: {'theme': 'neutral'}}%%
classDiagram
    class ReservedParameterSpecification {
        <<Protocol>>
        +find(parameters, resolver) Parameter | None
        +__hash__() int
    }
    class ReservedName {
        <<NamedTuple>>
        +name: str
        +find(parameters, resolver) Parameter | None
        +__hash__() int
    }
    class ReservedNameBoundParameter {
        <<NamedTuple>>
        +name: str
        +find(parameters, resolver) Parameter | None
        +__hash__() int
    }
    class ReservedType {
        <<NamedTuple>>
        +name: str | None
        +type: type
        +alias: str | None
        +find(parameters, resolver) Parameter | None
        +__hash__() int
    }

    ReservedParameterSpecification <|.. ReservedName : implements
    ReservedParameterSpecification <|.. ReservedNameBoundParameter : implements
    ReservedParameterSpecification <|.. ReservedType : implements
Loading

Reviews (1): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile

Copy link
Copy Markdown
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! :)

@bellini666 bellini666 merged commit 85cc076 into strawberry-graphql:main May 14, 2026
76 checks passed
@botberry
Copy link
Copy Markdown
Member

This PR was published as 0.315.5. Thank you for contributing!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 14, 2026

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing jonathandung:main (592620c) with main (395b2b3)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (a151fce) during the generation of this report, so 395b2b3 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants