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

Unpack list type when looking up namespaces #3388

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

Conversation

jeich
Copy link

@jeich jeich commented Feb 19, 2024

Description

The namespace resolver does not check for lists, meaning a type definition of List[Annotated[MyType, strawberry.lazy("my.module")]] throws strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError

Types of Changes

  • 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).

@jeich jeich marked this pull request as ready for review February 19, 2024 19:53
@botberry
Copy link
Member

botberry commented Feb 19, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Fixes an issue where lazy annotations raised an error when used together with a List

Here's the tweet text:

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

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

Copy link

codecov bot commented Feb 19, 2024

Codecov Report

Merging #3388 (7b88a53) into main (be9a2d5) will decrease coverage by 1.08%.
Report is 4 commits behind head on main.
The diff coverage is 80.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3388      +/-   ##
==========================================
- Coverage   96.41%   95.33%   -1.08%     
==========================================
  Files         498      500       +2     
  Lines       31133    31174      +41     
  Branches     3814     3813       -1     
==========================================
- Hits        30016    29719     -297     
- Misses        911     1246     +335     
- Partials      206      209       +3     

Copy link

@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.

PR Type: Bug fix

PR Summary: This pull request addresses a specific issue where using lazy annotations with lists in type definitions caused an UnresolvedFieldTypeError. It introduces changes to the namespace resolution logic to properly handle list types, ensuring that types like List[Annotated[MyType, strawberry.lazy("my.module")]] are correctly resolved without errors.

Decision: Comment

📝 Type: 'Bug fix' - not supported yet.
  • Sourcery currently only approves 'Typo fix' PRs.
✅ Issue addressed: this change correctly addresses the issue or implements the desired feature.
No details provided.
✅ Small diff: the diff is small enough to approve with confidence.
No details provided.

General suggestions:

  • Consider verifying the type of expr_slice before passing it to _get_namespace_from_ast to ensure robustness against unexpected input types.
  • The use of cast without utilizing its result seems unnecessary. If it's meant for type checking, consider applying it in a way that influences the program or removing it if it's redundant.
  • Overall, the changes are well-targeted and address the issue effectively. It's recommended to ensure that all edge cases and potential type mismatches are handled to prevent any unforeseen errors.
strawberry/utils/typing.py: Consolidate condition checks for "list", "List", and "Annotated" to simplify and reduce code complexity.

While reviewing the proposed changes, I noticed an opportunity to simplify the added complexity by consolidating similar conditions. The new code introduces separate branches for checking instances of ast.Subscript and ast.Name, specifically for "list" and "List", which closely mirrors the existing structure for handling "Annotated".

To enhance maintainability and readability, consider merging these conditions into a single block. This approach not only reduces repetition but also keeps the codebase concise, making it easier for future developers to navigate and understand. Here's a suggested revision that consolidates the handling of these cases:

elif isinstance(expr, ast.Subscript) and isinstance(expr.value, ast.Name):
    if expr.value.id in {"list", "List", "Annotated"}:
        expr_slice = expr.slice
        extra.update(_get_namespace_from_ast(expr_slice, globalns, localns))

This modification aims to streamline the logic by grouping similar conditions, potentially reducing the cognitive load required to grasp the code's intent and making the overall structure more elegant.

Thanks for using Sourcery. We offer it for free for open source projects and would be very grateful if you could help us grow. If you like it, would you consider sharing Sourcery on your favourite social media? ✨

Share Sourcery

Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

and expr.value.id in {"list", "List"}
):
expr_slice = expr.slice
cast(ast.Tuple, expr_slice)
Copy link

Choose a reason for hiding this comment

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

issue (llm): The use of cast here does not alter the program's behavior but is intended for type checking. However, without assigning its result or using it in a type hint, this line has no effect. Consider either removing this line if it's unnecessary or using the cast result if it was intended for type checking.

):
expr_slice = expr.slice
cast(ast.Tuple, expr_slice)
extra.update(_get_namespace_from_ast(expr_slice, globalns, localns))
Copy link

Choose a reason for hiding this comment

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

suggestion (llm): This update operation assumes that expr_slice will always be a valid input for _get_namespace_from_ast. Given that expr_slice comes from parsing AST nodes, it might be prudent to ensure that expr_slice is of a type that _get_namespace_from_ast can handle, or to handle potential exceptions that could arise from unexpected types.

Copy link

codspeed-hq bot commented Feb 19, 2024

CodSpeed Performance Report

Merging #3388 will not alter performance

Comparing jeich:fix-list-annotation (7b88a53) with main (be9a2d5)

Summary

✅ 12 untouched benchmarks

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.

Nice! Can you add a test? 😊

@jeich
Copy link
Author

jeich commented Feb 19, 2024

Nice! Can you add a test? 😊

There are some good tests on this PR for this scenario, should I add the commits to this PR?

@patrick91
Copy link
Member

@jeich yes please 😊

@jeich
Copy link
Author

jeich commented Feb 20, 2024

Added tests from @ddoroshev

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.

None yet

4 participants