-
-
Notifications
You must be signed in to change notification settings - Fork 577
Fix untyped decorator #3867
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
base: main
Are you sure you want to change the base?
Fix untyped decorator #3867
Conversation
Reviewer's GuideAdjusts type annotations for field decorators to return StrawberryField, streamlines parameter defaults in the decorator signature, enhances the mypy test utility, and adds new type-checking tests along with release notes. Class Diagram:
|
Change | Details | Files |
---|---|---|
Field decorator overloads now return StrawberryField instead of Any |
|
strawberry/types/field.py strawberry/federation/field.py |
Simplified parameter types in field decorator implementation |
|
strawberry/types/field.py |
Improved mypy runner to handle empty output |
|
tests/typecheckers/utils/mypy.py |
Added new type-checking tests for field decorators |
|
tests/typecheckers/test_fields_with_arguments.py |
Documented the patch release in release notes |
|
RELEASE.md |
Assessment against linked issues
Issue | Objective | Addressed | Explanation |
---|---|---|---|
#1929 | Fix the mypy error 'untyped decorator makes function .. untyped' when using @strawberry.field with arguments. |
✅ |
Possibly linked issues
- @strawberry.field(args...) decorator gives 'untyped decorator makes function .. untyped' #1929: The PR modifies type hints for @strawberry.field and adds a test to fix the mypy 'untyped decorator' error when arguments are used.
- @strawberry.field(args...) decorator gives 'untyped decorator makes function .. untyped' #1929: PR changes strawberry.field type hint and adds test to fix mypy untyped decorator error.
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
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR fixes a typing issue where @strawberry.field
decorator with arguments would cause mypy to raise 'untyped decorator' errors in strict mode. The changes include:
- Updated field function overloads in
strawberry/types/field.py
to returnStrawberryField
instead ofAny
- Added test file
tests/typecheckers/test_fields_with_arguments.py
to validate decorator typing with arguments - Modified
mypy.py
to handle empty output when type checking passes successfully - Updated federation field implementation to maintain consistent typing behavior
- Added release notes documenting the fix for the untyped decorator issue
5 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
@@ -0,0 +1,21 @@ | |||
release type: patch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider adding a more descriptive title for this release, e.g. 'Fix mypy typing issues with @strawberry.field decorator arguments'
Apollo Federation Subgraph Compatibility Results
Learn more: |
Thanks for adding the Here's a preview of the changelog: This release fixes a long standing typing issue where mypy would
When using the following code: import strawberry
@strawberry.type
class Query:
@strawberry.field(description="Get the last user")
def last_user_v2(self) -> str:
return "Hello" Here's the tweet text:
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3867 +/- ##
==========================================
- Coverage 95.01% 95.00% -0.02%
==========================================
Files 508 509 +1
Lines 33141 33153 +12
Branches 1721 1722 +1
==========================================
+ Hits 31490 31496 +6
- Misses 1370 1376 +6
Partials 281 281 🚀 New features to boost your workflow:
|
There was a problem hiding this 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!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
CodSpeed Performance ReportMerging #3867 will not alter performanceComparing Summary
|
@@ -475,7 +475,7 @@ def field( | |||
directives: Optional[Sequence[object]] = (), | |||
extensions: Optional[list[FieldExtension]] = None, | |||
graphql_type: Optional[Any] = None, | |||
) -> Any: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: Almost sure the reason we have Any
here is to allow this:
@strawberry.type
class Foo:
bar: int = strawberry.field(...)
Without Any
, this gives the issue we are seeing in the tests, "StrawberryField" is not assignable to "int"
Not sure if there's a way around that, though :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there's a way around that, though :(
Random showerthought: I feel like there should be a way, given that dataclasses use a similar notation and get away with it:
https://docs.python.org/3/library/dataclasses.html#dataclasses.field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I was taking a look at this, https://peps.python.org/pep-0681/ and it seems that they also use Any 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say we can close this PR =P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bellini666 the issue is still there though 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't even reproduce it properly
The original issue mentioned mypy (only a later comment mentions pyright). It looks like it reproduces in the mypy playground:
https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=ced03d8cb75457ab2ed0d89c2d82b23d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DoctorJohn can you open an issue on mypy's side? :D
Closes #1929
Thanks @aryaniyaps for the help!
Summary by Sourcery
Fix typing of field decorators to return StrawberryField and improve type checking support.
Bug Fixes:
Enhancements:
Documentation:
Tests: