Skip to content

Conversation

@deepak-singh
Copy link

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

Comment on lines +177 to +179
# Test nested Maybe
nested_maybe = Maybe(value=Maybe(value="nested"))
assert resolve_value(nested_maybe) == "nested"
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Suggested change
# Test nested Maybe
nested_maybe = Maybe(value=Maybe(value="nested"))
assert resolve_value(nested_maybe) == "nested"
# 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]

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

1 participant