Skip to content

Incorrect typing for the type decorator #3790

Open
@Corentin-Bravo

Description

@Corentin-Bravo

Describe the Bug

The type function is decorated with

@dataclass_transform(
    order_default=True, kw_only_default=True, field_specifiers=(field, StrawberryField)
)

Therefore mypy treats classes decorated with type as being dataclasses with ordering functions.

In particular, defining __gt__ on such a class will be treated as an error by mypy.

However, type (that is the underlying _wrap_dataclass function) do not do anything to ensure the dataclass actually has order functions defined.

I see multiple solutions:

  • Removing the order_default=True part of the dataclass_transform decorating type
  • Enforcing the order=True in _wrap_dataclass
  • Allowing the caller to pass dataclass kwargs (as per my previous issue)

System Information

  • Operating system: Ubuntu 24.04
  • Strawberry version (if applicable): 0.256.1

Additional Context

Code samples to be clear on the issue

@strawberry.type
class MyClass:
    attr: str
 
k = MyClass(attr="abc")
j = MyClass(attr="def")
j > k # TypeError: '<' not supported between instances of 'MyClass' and 'MyClass'
@strawberry.type
class MyClass:
    attr: str

    def __gt__(self, other):
        return self.attr > other.attr
 
k = MyClass(attr="abc")
j = MyClass(attr="def")
j > k # True

# When running mypy
error: You may not have a custom "__gt__" method when "order" is True  [misc]

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions