Skip to content

Conversation

@bellini666
Copy link
Member

@bellini666 bellini666 commented Oct 18, 2025

Ideally we can try to find a better way of solving this in the future. We have a similar case for auto and strawberry.auto, so we can solve both at the same time.

For now, especially considering we already do that for auto, I think it should be good enough

Fix #4004

Summary by Sourcery

Fix detection of Maybe annotations when using future annotations by matching string patterns, and add tests for this scenario

Bug Fixes:

  • Support recognition of Maybe in string-based future annotations using regex matching

Documentation:

  • Add release note for patch fixing Maybe with future annotations

Tests:

  • Add tests to verify schema generation for Maybe used with from __future__ import annotations

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 18, 2025

Reviewer's Guide

Enhance the detection of strawberry.Maybe annotations in modules using future imports by adding a regex-based matcher for string annotations, clean up dev dependencies, introduce tests covering these scenarios, and update release notes for a patch release.

File-Level Changes

Change Details Files
Add regex-based support for string-form Maybe annotations
  • Define _maybe_re to match ‘Maybe[...]’ patterns
  • Handle string annotations in _annotation_is_maybe by matching against _maybe_re
strawberry/types/maybe.py
Introduce tests for future and direct Maybe annotations
  • Add tests covering future-import string annotations
  • Add parallel tests for direct Maybe imports
tests/schema/test_maybe_future_annotations.py
Add patch release notes
  • Create RELEASE.md with release type and change summary
RELEASE.md

Assessment against linked issues

Issue Objective Addressed Explanation
#4004 Fix instantiation of input classes using strawberry.Maybe fields when 'from future import annotations' is present, so that Maybe fields do not require keyword arguments.
#4004 Ensure that queries and mutations using inputs with strawberry.Maybe fields work correctly when 'from future import annotations' is used.

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

@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 there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `RELEASE.md:3` </location>
<code_context>
+Release type: patch
+
+This release fixes the usage of `strawnerry.Maybe` inside modules using `from __future__ import annotations`
</code_context>

<issue_to_address>
**issue (typo):** Typo: 'strawnerry' should be 'strawberry'.

Update the release notes to use the correct spelling.
</issue_to_address>

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.

@botberry
Copy link
Member

botberry commented Oct 18, 2025

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes the usage of strawberry.Maybe inside modules using from __future__ import annotations

Here's the tweet text:

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

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

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

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.

Greptile Overview

Summary

Fixed Maybe type handling when using from __future__ import annotations by adding regex pattern matching for stringified annotations.

Key Changes:

  • Modified _annotation_is_maybe() in strawberry/types/maybe.py to handle string annotations using regex pattern ^(?:strawberry\.)?Maybe\[(.+)\]$
  • Added comprehensive tests for both Maybe and strawberry.Maybe variants with future annotations
  • Removed pytest-codspeed dev dependency

Why This Works:
When from __future__ import annotations is enabled, type annotations become strings at runtime. The function now checks if the annotation is a string and uses regex matching to detect Maybe patterns before evaluating. This prevents the _inject_maybe_default() function from failing to recognize Maybe fields, which was causing them to be treated as required keyword arguments.

Minor Issue:

  • Typo in RELEASE.md: "strawnerry" should be "strawberry"

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk after fixing the typo
  • The fix is simple, well-tested, and follows an existing pattern in the codebase (as mentioned in the PR description about auto having similar handling). The regex pattern is solid and handles edge cases correctly. Only issue is a typo in the release notes.
  • RELEASE.md needs typo correction before merge

Important Files Changed

File Analysis

Filename Score Overview
RELEASE.md 4/5 Added release notes with a typo in "strawberry"
strawberry/types/maybe.py 5/5 Added regex pattern matching to handle string annotations from future imports
tests/schema/test_maybe_future_annotations.py 5/5 Added comprehensive tests for both Maybe and strawberry.Maybe with future annotations

Sequence Diagram

sequenceDiagram
    participant User as User Code
    participant Decorator as @strawberry.input
    participant ProcessType as _process_type()
    participant InjectDefaults as _inject_maybe_default()
    participant AnnotationCheck as _annotation_is_maybe()
    participant Class as MyInput Class

    User->>Decorator: Define class with Maybe[str] field
    Note over User,Decorator: from __future__ import annotations

    Decorator->>ProcessType: Process class definition
    ProcessType->>InjectDefaults: Inject defaults for Maybe fields
    
    InjectDefaults->>AnnotationCheck: Check if annotation is Maybe
    
    alt Before Fix: Runtime annotation is string
        AnnotationCheck-->>InjectDefaults: False (string not recognized)
        Note over InjectDefaults: Field treated as required
        InjectDefaults-->>Class: No default injected
        Class-->>User: TypeError: missing required keyword argument
    end
    
    alt After Fix: String pattern matching
        AnnotationCheck->>AnnotationCheck: Check if isinstance(annotation, str)
        AnnotationCheck->>AnnotationCheck: Match regex: ^(?:strawberry\.)?Maybe\[(.+)\]$
        AnnotationCheck-->>InjectDefaults: True (pattern matched)
        InjectDefaults->>Class: setattr(cls, name, None)
        Class-->>User: MyInput() succeeds
    end
Loading

4 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@codecov
Copy link

codecov bot commented Oct 18, 2025

Codecov Report

❌ Patch coverage is 94.73684% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.41%. Comparing base (513c7cc) to head (0919b66).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4031   +/-   ##
=======================================
  Coverage   94.41%   94.41%           
=======================================
  Files         532      533    +1     
  Lines       34773    34811   +38     
  Branches     1828     1829    +1     
=======================================
+ Hits        32831    32867   +36     
- Misses       1647     1649    +2     
  Partials      295      295           
🚀 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.

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 18, 2025

CodSpeed Performance Report

Merging #4031 will not alter performance

Comparing fix-future-maybe (0919b66) with main (513c7cc)

Summary

✅ 26 untouched

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Copy link
Member

@DoctorJohn DoctorJohn left a comment

Choose a reason for hiding this comment

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

image

@patrick91 patrick91 merged commit 91038e9 into main Oct 18, 2025
83 checks passed
@patrick91 patrick91 deleted the fix-future-maybe branch October 18, 2025 20:29
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.

from __future__ import annotations incompatible with Maybe

5 participants