diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2019-08-20 01:40:28 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-20 01:40:28 -0400 |
commit | b0f4dab8735f692bcfedcf0fa9a25e238a554bab (patch) | |
tree | 572a7de0d867aca65321f7b0de04f4f5876005fa /Lib/test/test_dataclasses.py | |
parent | d3c8d735147ccdde1f9bf18ba481da67564837bf (diff) | |
download | cpython-b0f4dab8735f692bcfedcf0fa9a25e238a554bab.tar.gz cpython-b0f4dab8735f692bcfedcf0fa9a25e238a554bab.zip |
bpo-37868: Improve is_dataclass for instances. (GH-15325)
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index ea42904b003..037bf4c2214 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -1300,6 +1300,32 @@ class TestCase(unittest.TestCase): 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. |