From 56bd7d74a2859cb89a2f769890941418b336dfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Bal=C3=A1=C5=BE?= Date: Thu, 19 Sep 2024 11:58:07 +0200 Subject: [PATCH 1/2] `dataclasses` plugin fix: arguments without a default cannot follow attributes with one --- mypy/plugins/dataclasses.py | 2 +- test-data/unit/check-dataclasses.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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" From 263cfaebee4d4b0e9af9732afc6068d43aa8ed3a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:02:35 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/plugins/dataclasses.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index aa7ca35250ee..8be68ba9dd38 100644 --- a/mypy/plugins/dataclasses.py +++ b/mypy/plugins/dataclasses.py @@ -734,7 +734,9 @@ 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 and not attr.kw_only) + 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: