Skip to content

Conversation

@deepak-singh
Copy link
Contributor

@deepak-singh deepak-singh commented Nov 1, 2025

Description

This PR adds support for the strawberry.Maybe type in the Django filter processing code, addressing issue #753.

Changes

  • Updated resolve_value() function in strawberry_django/filters.py to detect and handle strawberry.Maybe instances
  • Added comprehensive test coverage for Maybe type support in tests/filters/test_filters_v2.py

Testing

  • All existing tests pass
  • New tests for Maybe type pass
  • Tests gracefully skip if Maybe is not available in strawberry version

Notes

  • The implementation gracefully handles cases where strawberry.Maybe is not available in the installed version
  • Maybe values are recursively resolved to handle nested cases
  • Supports Maybe instances in lists

Closes #753

Summary by Sourcery

Enable filter processing to recognize and unwrap strawberry.Maybe values by extracting and resolving their .value, with added test coverage for various Maybe scenarios.

New Features:

  • Add support for strawberry.Maybe type in filter resolve_value to unwrap Maybe instances

Enhancements:

  • Recursively resolve nested Maybe values and handle lists of Maybe instances
  • Gracefully skip Maybe handling when strawberry.Maybe is unavailable

Tests:

  • Add tests for strawberry.Maybe handling including value present, None, nested, and list cases

- Updated resolve_value() to detect and handle Maybe instances
- Extract .value attribute from Maybe with recursive resolution
- Added comprehensive tests for Maybe support
- Gracefully handles cases where Maybe is not available

Closes strawberry-graphql#753
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Nov 1, 2025

Reviewer's Guide

Extend filter processing to support strawberry.Maybe types via recursive extraction in resolve_value and add corresponding test coverage ensuring compatibility across Strawberry versions.

File-Level Changes

Change Details Files
Add support for resolving strawberry.Maybe types in resolve_value
  • Wrap Maybe import in try/except to maintain compatibility
  • Detect Maybe instances and extract .value property
  • Recursively resolve nested Maybe values and return None for None values
strawberry_django/filters.py
Add test coverage for Maybe support in filter resolution
  • Skip tests if strawberry.Maybe is unavailable
  • Verify resolution of Maybe with value, None, enum, and GlobalID
  • Test list of Maybe instances and nested Maybe resolution
tests/filters/test_filters_v2.py

Assessment against linked issues

Issue Objective Addressed Explanation
#753 Add support for the strawberry.Maybe type in filter processing, specifically in the process_filter/resolve_value logic.
#753 Add tests to verify correct handling of strawberry.Maybe type, including extracting .value, handling None, nested Maybes, and lists.

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 - here's some feedback:

  • Consider moving the import of Maybe to module scope (or caching it) so you don’t re-try the import on every call to resolve_value.
  • You can simplify the Maybe handling by always calling resolve_value on value.value (since resolve_value(None) already returns None), removing the explicit if maybe_value is not None branch.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider moving the import of `Maybe` to module scope (or caching it) so you don’t re-try the import on every call to resolve_value.
- You can simplify the `Maybe` handling by always calling `resolve_value` on `value.value` (since `resolve_value(None)` already returns `None`), removing the explicit `if maybe_value is not None` branch.

## Individual Comments

### Comment 1
<location> `tests/filters/test_filters_v2.py:160-165` </location>
<code_context>
+    maybe_with_value = Maybe(value="test_string")
+    assert resolve_value(maybe_with_value) == "test_string"
+
+    # Test Maybe with None
+    maybe_none = Maybe(value=None)
+    assert resolve_value(maybe_none) is None
+
+    # Test Maybe with nested types
</code_context>

<issue_to_address>
**suggestion (testing):** Consider adding a test for Maybe with deeply nested None values.

Adding a test for Maybe(value=Maybe(value=None)) will help verify correct handling of nested None values.

```suggestion
    # Test Maybe with nested types
    maybe_enum = Maybe(value=Version.TWO)
    assert resolve_value(maybe_enum) == Version.TWO.value

    maybe_gid = Maybe(value=GlobalID("FruitNode", "42"))
    assert resolve_value(maybe_gid) == "42"

    # Test Maybe with deeply nested None value
    maybe_deep_none = Maybe(value=Maybe(value=None))
    assert resolve_value(maybe_deep_none) is None
```
</issue_to_address>

### Comment 2
<location> `tests/filters/test_filters_v2.py:177-179` </location>
<code_context>
+    maybe_gid = Maybe(value=GlobalID("FruitNode", "42"))
+    assert resolve_value(maybe_gid) == "42"
+
+    # Test Maybe in a list
+    maybe_list = [
+        Maybe(value=1),
+        Maybe(value="test"),
+        Maybe(value=None),
+        Maybe(value=Version.ONE),
+    ]
+    resolved_list = resolve_value(maybe_list)
+    assert resolved_list == [1, "test", None, Version.ONE.value]
+
+    # Test nested Maybe
</code_context>

<issue_to_address>
**suggestion (testing):** Consider adding a test for lists containing nested Maybes.

Add a test with a list containing nested Maybe instances, such as [Maybe(value=Maybe(value="foo")), Maybe(value=None)], to verify resolve_value correctly handles nested Maybes.

```suggestion
    # Test nested Maybe
    nested_maybe = Maybe(value=Maybe(value="nested"))
    assert resolve_value(nested_maybe) == "nested"

    # Test list containing nested Maybes
    nested_maybe_list = [
        Maybe(value=Maybe(value="foo")),
        Maybe(value=None),
    ]
    resolved_nested_list = resolve_value(nested_maybe_list)
    assert resolved_nested_list == ["foo", None]
```
</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.

- Updated resolve_value() to detect and handle Maybe instances
- Moved Maybe import to module scope for better performance
- Simplified Maybe handling by removing redundant None check
- Extract .value attribute from Maybe with recursive resolution
- Added comprehensive tests including nested Maybes and edge cases
- Gracefully handles cases where Maybe is not available

Closes strawberry-graphql#753
@bellini666
Copy link
Member

@deepak-singh, really sorry btw for taking so long to check this. Let me know if my comments make sense, and if you need help with the implementation somehow

@gitguardian
Copy link

gitguardian bot commented Dec 6, 2025

⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
22655414 Triggered Username Password 23d68e2 examples/ecommerce_app/tests/conftest.py View secret
22638889 Triggered Generic Password 23d68e2 examples/ecommerce_app/app/management/commands/populate_db.py View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@codecov
Copy link

codecov bot commented Dec 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.71%. Comparing base (9d6c098) to head (e2e018c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #805   +/-   ##
=======================================
  Coverage   89.70%   89.71%           
=======================================
  Files          45       45           
  Lines        4314     4318    +4     
=======================================
+ Hits         3870     3874    +4     
  Misses        444      444           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

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

I finished the PR here :)

@bellini666 bellini666 changed the title Add support for strawberry.Maybe type in filter processing feat: add support for strawberry.Maybe type in mutations and filter processing Dec 6, 2025
@bellini666 bellini666 merged commit dfb47b2 into strawberry-graphql:main Dec 6, 2025
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support the new strawberry.Maybe type

2 participants