From 5d16973994c2ffc0df20683a81e30e8c42bdd20b Mon Sep 17 00:00:00 2001 From: Brij Date: Tue, 17 Feb 2026 18:41:41 -0500 Subject: [PATCH 1/6] default value check for descriptors --- Lib/dataclasses.py | 5 +++++ Lib/test/test_dataclasses/__init__.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 730ced7299865e..e133260e972c3e 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -831,6 +831,11 @@ def _get_field(cls, a_name, a_type, default_kw_only): # If the default value isn't derived from Field, then it's only a # normal default value. Convert it to a Field(). default = getattr(cls, a_name, MISSING) + # First checks if default is a descriptor, + # then checks if there is no default value returned by default.__get__. + if hasattr(default, "__get__") and default is cls.__dict__.get(a_name): + default = MISSING + if isinstance(default, Field): f = default else: diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 3b335429b98500..a0978e9ec7609d 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -4186,6 +4186,23 @@ class C: with self.assertRaisesRegex(TypeError, 'missing 1 required positional argument'): c = C() + def test_return_self_no_default_value(self): + class D: + def __get__(self, instance: Any, owner: object) -> Any: + if instance is None: + return self + return instance._x + + def __set__(self, instance: Any, value: int) -> None: + instance._x = value + + @dataclass + class C: + i: D = D() + + with self.assertRaisesRegex(TypeError, 'missing 1 required positional argument'): + c = C() + class TestStringAnnotations(unittest.TestCase): def test_classvar(self): # Some expressions recognized as ClassVar really aren't. But From a909cb3b19595bbea22e62ee1534c7c9faaabfe1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 23:50:42 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst diff --git a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst new file mode 100644 index 00000000000000..4907958ac645ca --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst @@ -0,0 +1,3 @@ +Previously descriptors that return themselves from calls to `__get__` were +treated as default values in a dataclass. Now, a `TypeError` is raised if no +initial value is provided to the dataclass constructor. From 1a35c15f59404c25ace9d034ab3e83ea44c0936c Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Tue, 17 Feb 2026 19:05:04 -0500 Subject: [PATCH 3/6] Fix formatting in NEWS entry for dataclass behavior --- .../Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst index 4907958ac645ca..f0942ce719d26f 100644 --- a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst +++ b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst @@ -1,3 +1,3 @@ -Previously descriptors that return themselves from calls to `__get__` were -treated as default values in a dataclass. Now, a `TypeError` is raised if no -initial value is provided to the dataclass constructor. +Previously descriptors that return themselves from calls to `__get__` were +treated as default values in a dataclass. Now, a `TypeError` is raised if no +initial value is provided to the dataclass constructor. From 97766b1a6548aa2c74cd09bb65bce596410ceb97 Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Tue, 17 Feb 2026 19:08:34 -0500 Subject: [PATCH 4/6] Fix formatting in NEWS entry for dataclass behavior --- .../Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst index f0942ce719d26f..bf8e24f3994c4a 100644 --- a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst +++ b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst @@ -1,3 +1,4 @@ -Previously descriptors that return themselves from calls to `__get__` were -treated as default values in a dataclass. Now, a `TypeError` is raised if no +Previously descriptors that return themselves from calls to ``__get__`` were +treated as default values in a dataclass. Now, a ``TypeError`` is raised if no initial value is provided to the dataclass constructor. + From 168a9b94f00f0ca6fc2324cba5d1682bc4a6be98 Mon Sep 17 00:00:00 2001 From: Brij <97006829+brijkapadia@users.noreply.github.com> Date: Sat, 18 Jul 2026 06:41:40 -0400 Subject: [PATCH 5/6] Update NEWS --- .../Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst index bf8e24f3994c4a..08911be699832d 100644 --- a/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst +++ b/Misc/NEWS.d/next/Library/2026-02-17-23-50-41.gh-issue-144749.sv3Ztr.rst @@ -1,4 +1,3 @@ -Previously descriptors that return themselves from calls to ``__get__`` were -treated as default values in a dataclass. Now, a ``TypeError`` is raised if no -initial value is provided to the dataclass constructor. - +Previously, descriptors that return themselves from calls to :meth:`~object.__get__` were +treated as default values in :mod:`dataclasses`. Now, a :exc:`TypeError` is raised if no +initial value is provided to the :mod:`dataclass ` constructor. From 3731ac4ec496a3ec7d70cd25dc4b1df1ae8d2e64 Mon Sep 17 00:00:00 2001 From: Brij <97006829+brijkapadia@users.noreply.github.com> Date: Mon, 3 Aug 2026 08:00:39 -0400 Subject: [PATCH 6/6] fix tests --- Lib/dataclasses.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index c35fca329aa17b..9ec9bce2cf9083 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -822,10 +822,12 @@ def _get_field(cls, a_name, a_type, default_kw_only): # If the default value isn't derived from Field, then it's only a # normal default value. Convert it to a Field(). default = getattr(cls, a_name, MISSING) - # First checks if default is a descriptor, - # then checks if there is no default value returned by default.__get__. - if hasattr(default, "__get__") and default is cls.__dict__.get(a_name): - default = MISSING + # Check if default is a data descriptor that returns itself when + # accessed at the class level, meaning it is not providing a + # default value. + if (hasattr(default, "__get__") and default is cls.__dict__.get(a_name) + and (hasattr(default, "__set__") or hasattr(default, "__delete__"))): + default = MISSING if isinstance(default, Field): f = default