Skip to content

fix: codegen generic types on selection #3925

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

franco-gondi
Copy link

@franco-gondi franco-gondi commented Jun 23, 2025

This pr fixes an issue where using the codegen with some queries where generic types are involved failed.

Description

Types of Changes

Generic type renaming was not being performed when there is a selection, it was being done only on selection set

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

Issues Fixed or Closed by This PR

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

Unify generic parent type naming and apply it during field selection codegen to fix generic type renaming issues

Bug Fixes:

  • Fix generic type renaming in selection and selection set code generation by using the new helper method

Enhancements:

  • Extract _parent_type_name helper to compute schema type names including generics

Copy link
Contributor

sourcery-ai bot commented Jun 23, 2025

Reviewer's Guide

This PR introduces a new _parent_type_name helper to centralize generic type name resolution and refactors both field selection functions to use it, ensuring that generic types are consistently and correctly renamed during code generation.

Class diagram for refactored generic type name resolution in codegen

classDiagram
    class QueryCodegen {
        _field_from_selection(selection, parent_type)
        _field_from_selection_set(selection, parent_type)
        _parent_type_name(parent_type) str
    }
    QueryCodegen : - _field_from_selection now uses _parent_type_name
    QueryCodegen : - _field_from_selection_set now uses _parent_type_name
    QueryCodegen : + _parent_type_name centralizes generic type name logic
    class StrawberryObjectDefinition {
        type_var_map
        name
    }
    QueryCodegen --> StrawberryObjectDefinition : uses
Loading

File-Level Changes

Change Details Files
Encapsulated generic parent type name resolution into a helper method
  • Added _parent_type_name to compute names for generic and non-generic types based on type_var_map
  • Updated _field_from_selection to use the new helper for field lookup instead of parent_type.name
  • Refactored _field_from_selection_set to replace inline generic naming logic with the helper call
strawberry/codegen/query_codegen.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

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Fixed codegen bug where generic type renaming was not being handled consistently in query processing. The fix extracts and standardizes type name handling logic across the codebase.

  • Extracted parent type name handling into new _parent_type_name method in strawberry/codegen/query_codegen.py to ensure consistent generic type handling
  • Fixed bug where generic type renaming was skipped for selections without selection sets
  • Improved code maintainability by reusing type name logic between _field_from_selection and _field_from_selection_set

1 file reviewed, 1 comment
Edit PR Review Bot Settings | Greptile

Comment on lines 903 to 917
def _parent_type_name(self, parent_type: StrawberryObjectDefinition) -> str:
# Check if the parent type is generic.
# This seems to be tracked by `strawberry` in the `type_var_map`
# If the type is generic, then the strawberry generated schema
# naming convention is <GenericType,...><ClassName>
# The implementation here assumes that the `type_var_map` is ordered,
# but insertion order is maintained in python3.6+ (for CPython) and
# guaranteed for all python implementations in python3.7+, so that
# should be pretty safe.
if parent_type.type_var_map:
return "".join(
c.__name__ # type: ignore[union-attr]
for c in parent_type.type_var_map.values()
) + parent_type.name
return parent_type.name
Copy link
Contributor

Choose a reason for hiding this comment

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

style: consider documenting the generic type handling behavior with a docstring, especially the ordering assumptions

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 @franco-gondi - I've reviewed your changes and they look great!


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.

@botberry
Copy link
Member

botberry commented Jun 23, 2025

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to franco-gondi for the PR 👏

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

Copy link
Member

@patrick91 patrick91 left a comment

Choose a reason for hiding this comment

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

Hi @franco-gondi!

this looks promising, can you add test that fails on main?

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