[Python-checkins] bpo-37868: Improve is_dataclass for instances. (GH-15325)

Miss Islington (bot) webhook-mailer at python.org
Tue Aug 20 02:02:00 EDT 2019


https://github.com/python/cpython/commit/02c1457a036c2af3e91beb952afdb66d9c806435
commit: 02c1457a036c2af3e91beb952afdb66d9c806435
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-08-19T23:01:55-07:00
summary:

bpo-37868: Improve is_dataclass for instances. (GH-15325)

(cherry picked from commit b0f4dab8735f692bcfedcf0fa9a25e238a554bab)

Co-authored-by: Eric V. Smith <ericvsmith at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 7725621e0fae..33e26460c74e 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -1011,13 +1011,14 @@ def fields(class_or_instance):
 
 def _is_dataclass_instance(obj):
     """Returns True if obj is an instance of a dataclass."""
-    return not isinstance(obj, type) and hasattr(obj, _FIELDS)
+    return hasattr(type(obj), _FIELDS)
 
 
 def is_dataclass(obj):
     """Returns True if obj is a dataclass or an instance of a
     dataclass."""
-    return hasattr(obj, _FIELDS)
+    cls = obj if isinstance(obj, type) else type(obj)
+    return hasattr(cls, _FIELDS)
 
 
 def asdict(obj, *, dict_factory=dict):
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index f36098c69339..99086e5f6d25 100755
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -1294,6 +1294,32 @@ class D:
         self.assertTrue(is_dataclass(d.d))
         self.assertFalse(is_dataclass(d.e))
 
+    def test_is_dataclass_when_getattr_always_returns(self):
+        # See bpo-37868.
+        class A:
+            def __getattr__(self, key):
+                return 0
+        self.assertFalse(is_dataclass(A))
+        a = A()
+
+        # Also test for an instance attribute.
+        class B:
+            pass
+        b = B()
+        b.__dataclass_fields__ = []
+
+        for obj in a, b:
+            with self.subTest(obj=obj):
+                self.assertFalse(is_dataclass(obj))
+
+                # Indirect tests for _is_dataclass_instance().
+                with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
+                    asdict(obj)
+                with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
+                    astuple(obj)
+                with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
+                    replace(obj, x=0)
+
     def test_helper_fields_with_class_instance(self):
         # Check that we can call fields() on either a class or instance,
         #  and get back the same thing.
diff --git a/Misc/NEWS.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst b/Misc/NEWS.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst
new file mode 100644
index 000000000000..7f342e1ee354
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst
@@ -0,0 +1,3 @@
+Fix dataclasses.is_dataclass when given an instance that never raises
+AttributeError in __getattr__.  That is, an object that returns something
+for __dataclass_fields__ even if it's not a dataclass.



More information about the Python-checkins mailing list