Skip to content

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

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

Fix untyped decorator #3867

wants to merge 1 commit into from

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented May 21, 2025

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:

  • Fix untyped decorator error for @strawberry.field and @strawberry.federation.field by specifying return type as StrawberryField instead of Any.

Enhancements:

  • Simplify field function parameter defaults to untyped literals to improve type inference.
  • Update mypy test runner to treat empty output as success.

Documentation:

  • Document the typing fix in RELEASE.md to explain the untyped decorator issue and its resolution.

Tests:

  • Add type-checking tests for fields with arguments to validate mypy and pyright behavior.

Copy link
Contributor

sourcery-ai bot commented May 21, 2025

Reviewer's Guide

Adjusts 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: strawberry.federation.field Decorator Signature Update

classDiagram
    class StrawberryField {
        <<Type>>
    }
    class Field_strawberry_federation {
        <<Function>>
        # strawberry.federation.field()
        +field(resolver: Any, name: Optional[str], ..., external: bool, ..., graphql_type: Optional[Any]) : StrawberryField
    }
    Field_strawberry_federation ..> StrawberryField : returns
Loading

File-Level Changes

Change Details Files
Field decorator overloads now return StrawberryField instead of Any
  • Updated return type in overload signature for strawberry.types.field.field
  • Updated return type in overload signature for strawberry.federation.field.field
strawberry/types/field.py
strawberry/federation/field.py
Simplified parameter types in field decorator implementation
  • Replaced explicit Optional[...] annotations with default None values
  • Removed Literal type annotation for init parameter
  • Collapsed defaults for directives and extensions to direct tuple/list values
strawberry/types/field.py
Improved mypy runner to handle empty output
  • Assert zero return code when no mypy output is produced
  • Return an empty result list on empty output
tests/typecheckers/utils/mypy.py
Added new type-checking tests for field decorators
  • Created tests covering both strawberry.field and federation.field
  • Enforced Pyright and mypy checks via fixtures
tests/typecheckers/test_fields_with_arguments.py
Documented the patch release in release notes
  • Added RELEASE.md entry describing the patch and error reproduction
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


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

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 return StrawberryField instead of Any
  • 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
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 adding a more descriptive title for this release, e.g. 'Fix mypy typing issues with @strawberry.field decorator arguments'

@botberry
Copy link
Member

Apollo Federation Subgraph Compatibility Results

Federation 1 Support Federation 2 Support
_service🟢
@key (single)🟢
@key (multi)🟢
@key (composite)🟢
repeatable @key🟢
@requires🟢
@provides🟢
federated tracing🔲
@link🟢
@shareable🟢
@tag🟢
@override🟢
@inaccessible🟢
@composeDirective🟢
@interfaceObject🟢

Learn more:

@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes a long standing typing issue where mypy would
return the following error:

main:14: error: Untyped decorator makes function "e" untyped  [misc] (diff)

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:

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

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

Copy link

codecov bot commented May 21, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 95.00%. Comparing base (1568133) to head (0f0f5b6).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

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.

Copy link

codspeed-hq bot commented May 21, 2025

CodSpeed Performance Report

Merging #3867 will not alter performance

Comparing fix/mypy-field-typing (0f0f5b6) with main (1568133)

Summary

✅ 21 untouched benchmarks

@@ -475,7 +475,7 @@ def field(
directives: Optional[Sequence[object]] = (),
extensions: Optional[list[FieldExtension]] = None,
graphql_type: Optional[Any] = None,
) -> Any: ...
Copy link
Member

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 :(

Copy link
Member

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

Copy link
Member Author

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 🤷‍♂️

Copy link
Member Author

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: https://pyright-play.net/?pythonVersion=3.12&strict=true&code=GYJw9gtgBA%2BjwFcAuCQFM5QJYQA5hCSgEMA7UsJYpLMUgZwChGd9CoATa4gYwBti9emiaNQkKDzB8%2BaHjTr0AdMQBGPbHgJEAggHdi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-nBIBfc5xTyL5uVeSWG%2BCtvlNVdQE81%2B9LW3McWEoFhtQUm2iQmU1wqtXFVKrCyBqToH%2BPDp7NFRTUGYvQTi9FxSZWlOJYQ0licyGZpSJ8Vofo0LYCGEQXAPRxW8WwPQKACBhAcAItYpUCIsDuD9EQFyVNeIQDsGgKAeg-S%2BmHSAKA-pS09JpDMT4paViXibXYcdw7JCKF2AgZ5swx0UCQEoKA5kiClp3fWxt46wCSD9GQdwIRqifDINYmEl65i0DHdQZdA6ICQECLu1C5aHYKOAZQ01BIZjsmYEW3MtCzq52ZYXE091SWcv4D1VooxhEpCmRqkxhKR7bDsLgZAbdEYiVWY3GAdheJPMqcspRpGVEbP1djTVayYX22SYi9JcbUXVCQYEIUBElW0ZRkB1oLSs7QfaYwuDxcKmcvQ1ATDBrmNkfw4RgTxGKYqdIJR3g1HBMkZVXstVhisPSqNSAyNbHeUcddiilqCCePe34zRgz1K07OtXGGMTh5%2BZbB2HsBghwQAQE5QQS8IAYAWLFpcWeDgoAAGs9AwDoHwR8UXdIxannFqdHBpF3B4FgHpIB6CXASNJ7pQdvBdPpUHM4ZwpPVYQx0JDgkHZ4ihfJxTTHsMitreR0gBHCYuc03MijDQqNEcUa57uDGTNKZ6%2BG1jSTrMxqRVx%2Bz2S%2BOTeVW5w6HkcQ1neZWTze4QPVDAx8NIFhWj9B0GMKAtIAAaOhHBKjGLSKAB4s7lbZaSzkYIACKARL4zHpESW8Oofv3RywkdZci6CXGeCuwGsxrz%2BgHQ2horxmDVEPFDkuZc4cY2GIjuoXaV3kiIJjkA2PN5QEpzAanMA5gACYyvUmAEB28Z3qipHQCgEApAoCPAABLUiWI8ZgQA

Copy link
Member

@bellini666 bellini666 Jun 8, 2025

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

Copy link
Member Author

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 🤔

Copy link
Member

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

Copy link
Member Author

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

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.

@strawberry.field(args...) decorator gives 'untyped decorator makes function .. untyped'
5 participants