diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index edfc6840fc37..aa7ca35250ee 100644 --- a/mypy/plugins/dataclasses.py +++ b/mypy/plugins/dataclasses.py @@ -734,7 +734,7 @@ def collect_attributes(self) -> list[DataclassAttribute] | None: "Attributes without a default cannot follow attributes with one", context ) - found_default = found_default or (attr.has_default and attr.is_in_init) + found_default = found_default or (attr.has_default and attr.is_in_init and not attr.kw_only) if found_kw_sentinel and self._is_kw_only_type(attr.type): context = cls if attr.name in current_attr_names: diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index 0f726242b25b..9f0346869992 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -460,7 +460,7 @@ from dataclasses import dataclass, field, KW_ONLY class Application: _: KW_ONLY name: str = 'Unnamed' - rating: int = field(kw_only=False) # E: Attributes without a default cannot follow attributes with one + rating: int = field(kw_only=False) Application(name='name', rating=5) Application() # E: Missing positional argument "name" in call to "Application"