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

Fix info by adding a custom __class_getitem__ #3419

Merged
merged 5 commits into from Mar 21, 2024

Conversation

patrick91
Copy link
Member

I should have tested this at runtime, woops 😅

@botberry
Copy link
Member

botberry commented Mar 21, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release properly allows passing one argument to the Info class.

This is now fully supported:

import strawberry

from typing import TypedDict


class Context(TypedDict):
    user_id: str


@strawberry.type
class Query:
    @strawberry.field
    def info(self, info: strawberry.Info[Context]) -> str:
        return info.context["user_id"]

Here's the tweet text:

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

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

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.

Hey @patrick91 - I've reviewed your changes and they look great!

General suggestions:

  • Consider simplifying the __class_getitem__ implementation to enhance code maintainability.
  • Adding a docstring to the __class_getitem__ method could improve readability and provide clarity on its purpose and usage.
  • Evaluate the use of Any as a default type for better type safety and consider documenting the rationale if it remains.
  • Including test cases for error conditions and edge cases, such as invoking strawberry.Info without any arguments, would strengthen the test suite.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 3 issues found
  • 🟡 Complexity: 1 issue found
  • 🟢 Docstrings: 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 to tell me if it was helpful.

Comment on lines 42 to 46
def __class_getitem__(cls, types: Union[type, Tuple[type, ...]]) -> Type[Info]:
if not isinstance(types, tuple):
types = (types, Any)

return super().__class_getitem__(types)
Copy link

Choose a reason for hiding this comment

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

suggestion (code_clarification): Consider adding a docstring for __class_getitem__.

Given the significance of this method in enabling a new way to pass arguments to the Info class, a docstring explaining its purpose, parameters, and return type would enhance code readability and maintainability.

Comment on lines 43 to 44
if not isinstance(types, tuple):
types = (types, Any)
Copy link

Choose a reason for hiding this comment

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

question (code_refinement): Ensure Any is the intended default type.

Using Any as a default type when types is not a tuple could lead to less type-safe code. If this is intentional, consider documenting the rationale; otherwise, evaluate if a more specific default type could be used.

@@ -38,6 +39,12 @@
_raw_info: GraphQLResolveInfo
_field: StrawberryField

def __class_getitem__(cls, types: Union[type, Tuple[type, ...]]) -> Type[Info]:
if not isinstance(types, tuple):
types = (types, Any)
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Consider the implications of modifying types in-place.

Modifying the types variable directly changes the method's input argument, which could lead to unexpected behavior if types is used later in the method. Although not currently the case, as a best practice, consider using a new variable for the modified tuple.

@@ -0,0 +1,15 @@
from typing import Any
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding a test case for error conditions.

It would be beneficial to include a test case that verifies the behavior when invalid types are passed to strawberry.Info. This could help ensure that the custom __class_getitem__ method handles error conditions gracefully.

@@ -0,0 +1,15 @@
from typing import Any
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add a test case for using strawberry.Info without any arguments.

Including a test case for strawberry.Info invoked without any arguments could verify that the default behavior aligns with expectations. This scenario seems to be missing and could be an important edge case.

@@ -0,0 +1,15 @@
from typing import Any
Copy link

Choose a reason for hiding this comment

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

praise (testing): Praise for covering both one and two argument use cases.

The inclusion of tests for both one and two argument scenarios for strawberry.Info is commendable. It ensures that the functionality works as expected in these common use cases.

strawberry/types/info.py Show resolved Hide resolved
Copy link

codecov bot commented Mar 21, 2024

Codecov Report

Merging #3419 (23639fc) into main (dbf74bb) will decrease coverage by 1.03%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3419      +/-   ##
==========================================
- Coverage   96.40%   95.37%   -1.03%     
==========================================
  Files         499      500       +1     
  Lines       31129    31145      +16     
  Branches     3815     3815              
==========================================
- Hits        30010    29705     -305     
- Misses        913     1232     +319     
- Partials      206      208       +2     

Copy link

codspeed-hq bot commented Mar 21, 2024

CodSpeed Performance Report

Merging #3419 will not alter performance

Comparing fix/actually-allow-only-one-param (23639fc) with main (dbf74bb)

Summary

✅ 13 untouched benchmarks

@patrick91 patrick91 merged commit 2914950 into main Mar 21, 2024
62 of 63 checks passed
@patrick91 patrick91 deleted the fix/actually-allow-only-one-param branch March 21, 2024 17:17
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

2 participants