strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Fix untyped decorator

Open patrick91 opened this issue 6 months ago • 5 comments

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.

patrick91 avatar May 21 '25 14:05 patrick91

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

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

  • #1929: The PR modifies type hints for @strawberry.field and adds a test to fix the mypy 'untyped decorator' error when arguments are used.
  • #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.

sourcery-ai[bot] avatar May 21 '25 14:05 sourcery-ai[bot]

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 avatar May 21 '25 14:05 botberry

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)

botberry avatar May 21 '25 14:05 botberry

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              
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar May 21 '25 14:05 codecov[bot]

CodSpeed Performance Report

Merging #3867 will not alter performance

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

Summary

✅ 21 untouched benchmarks

codspeed-hq[bot] avatar May 21 '25 14:05 codspeed-hq[bot]