-
-
Notifications
You must be signed in to change notification settings - Fork 603
fix: fix Maybe with future annotations #4031
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
Conversation
Reviewer's GuideEnhance 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
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
d9bc9ce to
b5e4ae5
Compare
|
Thanks for adding the Here's a preview of the changelog: This release fixes the usage of Here's the tweet text: |
Apollo Federation Subgraph Compatibility Results
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.
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()instrawberry/types/maybe.pyto handle string annotations using regex pattern^(?:strawberry\.)?Maybe\[(.+)\]$ - Added comprehensive tests for both
Maybeandstrawberry.Maybevariants with future annotations - Removed
pytest-codspeeddev 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
autohaving 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
4 files reviewed, 1 comment
Codecov Report❌ Patch coverage is 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:
|
CodSpeed Performance ReportMerging #4031 will not alter performanceComparing Summary
|
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
DoctorJohn
left a comment
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.


Ideally we can try to find a better way of solving this in the future. We have a similar case for
autoandstrawberry.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 enoughFix #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:
Maybein string-based future annotations using regex matchingDocumentation:
Maybewith future annotationsTests:
Maybeused withfrom __future__ import annotations