Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(imp) support aliases in get_specialized_type_var_map #3766

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexey-pelykh
Copy link
Contributor

@alexey-pelykh alexey-pelykh commented Feb 3, 2025

Description

Support aliases (TypeVar passthrough) in get_specialized_type_var_map:

def test_get_specialized_type_var_map_double_generic_passthrough():
    @strawberry.type
    class Foo(Generic[_T]): ...

    @strawberry.type
    class Bar(Foo[_K], Generic[_K]): ...

    @strawberry.type
    class Bin(Bar[int]): ...

    assert get_specialized_type_var_map(Bin) == {
        "_T": int,
        "_K": int,
    }

Without this change, errors like

KeyError: '_T'

will appear

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

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

Support TypeVar passthrough in get_specialized_type_var_map.

Enhancements:

  • Resolve type variables correctly when a generic type inherits from another generic type, passing the type variable through.

Tests:

  • Add a test case for TypeVar passthrough with double generics.

Copy link
Contributor

sourcery-ai bot commented Feb 3, 2025

Reviewer's Guide by Sourcery

This pull request introduces support for TypeVar passthrough in get_specialized_type_var_map. This allows the function to correctly resolve type variables when dealing with complex inheritance scenarios involving generic types.

Class diagram showing generic type inheritance example

classDiagram
    class Foo~T~{
        +Generic[T]
    }
    class Bar~K~{
        +Generic[K]
    }
    class Bin{
    }
    Foo <|-- Bar: Foo[K]
    Bar <|-- Bin: Bar[int]
    note for Bin "Resolves type variables:
_T = int
_K = int"
Loading

File-Level Changes

Change Details Files
Implemented TypeVar passthrough in get_specialized_type_var_map.
  • Modified the function to traverse the class hierarchy and collect type parameters.
  • Added logic to resolve type variables passed through generic aliases.
  • Updated the function to use an OrderedDict to preserve the order of type parameters.
  • Added a loop to resolve type variables that are aliases of other type variables.
strawberry/utils/inspect.py
Added a new test case to verify the TypeVar passthrough functionality.
  • Created a new test case test_get_specialized_type_var_map_double_generic_passthrough.
  • Added a new test case that uses a double generic passthrough to verify the fix.
tests/python_312/test_inspect.py
tests/utils/test_inspect.py
Added a release note for the new feature.
  • Added a release note to the RELEASE.md file.
RELEASE.md

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

Copy link
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 @alexey-pelykh - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding inline documentation for the type variable resolution loop to explain the algorithm for future maintainers.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

@alexey-pelykh alexey-pelykh force-pushed the imp/get_specialized_type_var_map-double-generic-passthrough branch from 76e2406 to f1d079e Compare February 3, 2025 11:08
@botberry
Copy link
Member

botberry commented Feb 3, 2025

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Support aliases (TypeVar passthrough) in get_specialized_type_var_map.

Here's the tweet text:

🆕 Release (next) is out! Thanks to Alexey Pelykh for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

@alexey-pelykh alexey-pelykh force-pushed the imp/get_specialized_type_var_map-double-generic-passthrough branch from f1d079e to 025e667 Compare February 3, 2025 11:10
@alexey-pelykh alexey-pelykh changed the title Imp/get specialized type var map double generic passthrough (imp) support aliases in get_specialized_type_var_map Feb 3, 2025
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

Attention: Patch coverage is 97.77778% with 1 line in your changes missing coverage. Please review.

Project coverage is 97.27%. Comparing base (7ba5928) to head (025e667).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3766   +/-   ##
=======================================
  Coverage   97.27%   97.27%           
=======================================
  Files         504      504           
  Lines       33480    33504   +24     
  Branches     5502     5512   +10     
=======================================
+ Hits        32566    32590   +24     
  Misses        703      703           
  Partials      211      211           

Copy link

codspeed-hq bot commented Feb 3, 2025

CodSpeed Performance Report

Merging #3766 will not alter performance

Comparing alexey-pelykh:imp/get_specialized_type_var_map-double-generic-passthrough (025e667) with main (407b14f)

Summary

✅ 21 untouched benchmarks

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

Successfully merging this pull request may close these issues.

2 participants