diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-01-06 17:41:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-06 17:41:53 (GMT) |
commit | e7ba013d870012157f695ead7e3645c2828a7fc5 (patch) | |
tree | da2f4f57fc040b4587544423090bc6082a2636f7 /Lib/test | |
parent | 3cd7c6e6eb43dbd7d7180503265772a67953e682 (diff) | |
download | cpython-e7ba013d870012157f695ead7e3645c2828a7fc5.zip cpython-e7ba013d870012157f695ead7e3645c2828a7fc5.tar.gz cpython-e7ba013d870012157f695ead7e3645c2828a7fc5.tar.bz2 |
bpo-32499: Add dataclasses.is_dataclass(obj), which returns True if obj is a dataclass or an instance of one. (#5113)
Diffstat (limited to 'Lib/test')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index ed69563..fca384d 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -1,6 +1,6 @@ from dataclasses import ( dataclass, field, FrozenInstanceError, fields, asdict, astuple, - make_dataclass, replace, InitVar, Field, MISSING + make_dataclass, replace, InitVar, Field, MISSING, is_dataclass, ) import pickle @@ -1365,27 +1365,32 @@ class TestCase(unittest.TestCase): self.assertIs(C().x, int) - def test_isdataclass(self): - # There is no isdataclass() helper any more, but the PEP - # describes how to write it, so make sure that works. Note - # that this version returns True for both classes and - # instances. - def isdataclass(obj): - try: - fields(obj) - return True - except TypeError: - return False + def test_is_dataclass(self): + class NotDataClass: + pass - self.assertFalse(isdataclass(0)) - self.assertFalse(isdataclass(int)) + self.assertFalse(is_dataclass(0)) + self.assertFalse(is_dataclass(int)) + self.assertFalse(is_dataclass(NotDataClass)) + self.assertFalse(is_dataclass(NotDataClass())) @dataclass class C: x: int - self.assertTrue(isdataclass(C)) - self.assertTrue(isdataclass(C(0))) + @dataclass + class D: + d: C + e: int + + c = C(10) + d = D(c, 4) + + self.assertTrue(is_dataclass(C)) + self.assertTrue(is_dataclass(c)) + self.assertFalse(is_dataclass(c.x)) + self.assertTrue(is_dataclass(d.d)) + self.assertFalse(is_dataclass(d.e)) def test_helper_fields_with_class_instance(self): # Check that we can call fields() on either a class or instance, |