Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions strawberry_django/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
from strawberry.types.unset import UnsetType
from typing_extensions import Self, assert_never, dataclass_transform, deprecated

# Try to import Maybe at module level for performance
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: no need to do that, we can bump the minimum strawberry version to be the one that introduced Maybe

try:
from strawberry import Maybe
except ImportError:
# Maybe type not available in this version of strawberry
Maybe = None # type: ignore[assignment, misc]

Check warning on line 34 in strawberry_django/filters.py

View workflow job for this annotation

GitHub Actions / Typing

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)

from strawberry_django.fields.filter_order import (
RESOLVE_VALUE_META,
WITH_NONE_META,
Expand Down Expand Up @@ -122,6 +129,12 @@
if isinstance(value, list):
return [resolve_value(v) for v in value]

# Handle strawberry.Maybe type if available
if Maybe is not None and isinstance(value, Maybe):

Check failure on line 133 in strawberry_django/filters.py

View workflow job for this annotation

GitHub Actions / Typing

Second argument to "isinstance" must be a class or tuple of classes   Generic type with type arguments not allowed for instance or class checks (reportArgumentType)

Check warning on line 133 in strawberry_django/filters.py

View workflow job for this annotation

GitHub Actions / Typing

Condition will always evaluate to True since the types "UnionType" and "None" have no overlap (reportUnnecessaryComparison)
# Extract .value from Maybe and recursively resolve it
# resolve_value(None) already returns None, so no need for explicit check
return resolve_value(getattr(value, "value", None))

if isinstance(value, relay.GlobalID):
return value.node_id

Expand Down
49 changes: 49 additions & 0 deletions tests/filters/test_filters_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,55 @@
assert resolve_value(value) == resolved


def test_resolve_value_maybe():
"""Test that strawberry.Maybe type is properly handled in resolve_value."""
try:
from strawberry import Maybe
except ImportError:
pytest.skip("strawberry.Maybe is not available in this version")
Comment on lines +147 to +150
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: ditto


# Test Maybe with a value
maybe_with_value = Maybe(value="test_string")

Check failure on line 153 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
assert resolve_value(maybe_with_value) == "test_string"
Comment on lines +153 to +154
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: I think all of those can be parametrized on pyright. Will make the implementation cleaner


# Test Maybe with None
maybe_none = Maybe(value=None)

Check failure on line 157 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
assert resolve_value(maybe_none) is None

# Test Maybe with nested types
maybe_enum = Maybe(value=Version.TWO)

Check failure on line 161 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
assert resolve_value(maybe_enum) == Version.TWO.value

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

Check failure on line 164 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
assert resolve_value(maybe_gid) == "42"

# Test Maybe with deeply nested None value
maybe_deep_none = Maybe(value=Maybe(value=None))

Check failure on line 168 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)

Check failure on line 168 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
assert resolve_value(maybe_deep_none) is None

# Test Maybe in a list
maybe_list = [
Maybe(value=1),

Check failure on line 173 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
Maybe(value="test"),

Check failure on line 174 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
Maybe(value=None),

Check failure on line 175 in tests/filters/test_filters_v2.py

View workflow job for this annotation

GitHub Actions / Typing

Object of type "UnionType" is not callable (reportCallIssue)
Maybe(value=Version.ONE),
]
resolved_list = resolve_value(maybe_list)
assert resolved_list == [1, "test", None, Version.ONE.value]

# Test nested Maybe
nested_maybe = Maybe(value=Maybe(value="nested"))
assert resolve_value(nested_maybe) == "nested"
Comment on lines +181 to +183
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]


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


def test_filter_field_missing_prefix():
with pytest.raises(
MissingFieldArgumentError, match=r".*\"prefix\".*\"field_method\".*"
Expand Down
Loading