-
Notifications
You must be signed in to change notification settings - Fork 275
False positive dataclass-field-order for dataclass_transform wrapper with kw_only=True #3115
Copy link
Copy link
Closed
astral-sh/ruff
#24170Labels
dataclassesIssues relating to dataclasses and dataclass_transformIssues relating to dataclasses and dataclass_transformtyping semanticstyping-module features, spec compliance, etctyping-module features, spec compliance, etc
Milestone
Description
Summary
ty reports a false-positive dataclass-field-order error for a custom @dataclass_transform wrapper around dataclasses.dataclass when the wrapper is used as @define(kw_only=True).
In this case, the required field should be allowed after a field with a default, because kw_only=True makes the fields keyword-only. I would expect this code to produce no diagnostics.
Minimal repro:
import dataclasses
from collections.abc import Callable
from typing import Any, dataclass_transform, overload
@overload
@dataclass_transform(field_specifiers=(dataclasses.field,))
def define[T: type](maybe_cls: T, /, **kwargs) -> T: ...
@overload
@dataclass_transform(field_specifiers=(dataclasses.field,))
def define[T: type](maybe_cls: None = None, /, **kwargs) -> Callable[[T], T]: ...
def define(*args, **kwargs) -> Any:
return dataclasses.dataclass(*args, **kwargs)
@define(kw_only=True)
class A:
foo: int = dataclasses.field(default=0)
# ! error[dataclass-field-order]: Required field `bar` cannot be defined after fields with default values
bar: int = dataclasses.field()Command
ty check repro.pyRelevant settings
No special configuration is required to reproduce this issue.
Playground link
https://play.ty.dev/1bc71c1b-c147-496e-be5d-1803c86d32c4
Version
ty 0.0.24 (8762330 2026-03-19)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
dataclassesIssues relating to dataclasses and dataclass_transformIssues relating to dataclasses and dataclass_transformtyping semanticstyping-module features, spec compliance, etctyping-module features, spec compliance, etc