Skip to content

False positive dataclass-field-order for dataclass_transform wrapper with kw_only=True #3115

@liblaf

Description

@liblaf

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    dataclassesIssues relating to dataclasses and dataclass_transformtyping semanticstyping-module features, spec compliance, etc

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions