File tree 2 files changed +20
-2
lines changed
Lib/test/test_dataclasses
2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -459,8 +459,8 @@ Module contents
459
459
460
460
.. function :: is_dataclass(obj)
461
461
462
- Return ``True `` if its parameter is a dataclass or an instance of one,
463
- otherwise return ``False ``.
462
+ Return ``True `` if its parameter is a dataclass (including subclasses of a
463
+ dataclass) or an instance of one, otherwise return ``False ``.
464
464
465
465
If you need to know if a class is an instance of a dataclass (and
466
466
not a dataclass itself), then add a further check for ``not
Original file line number Diff line number Diff line change @@ -1547,6 +1547,24 @@ class A(types.GenericAlias):
1547
1547
self .assertTrue (is_dataclass (type (a )))
1548
1548
self .assertTrue (is_dataclass (a ))
1549
1549
1550
+ def test_is_dataclass_inheritance (self ):
1551
+ @dataclass
1552
+ class X :
1553
+ y : int
1554
+
1555
+ class Z (X ):
1556
+ pass
1557
+
1558
+ self .assertTrue (is_dataclass (X ), "X should be a dataclass" )
1559
+ self .assertTrue (
1560
+ is_dataclass (Z ),
1561
+ "Z should be a dataclass because it inherits from X" ,
1562
+ )
1563
+ z_instance = Z (y = 5 )
1564
+ self .assertTrue (
1565
+ is_dataclass (z_instance ),
1566
+ "z_instance should be a dataclass because it is an instance of Z" ,
1567
+ )
1550
1568
1551
1569
def test_helper_fields_with_class_instance (self ):
1552
1570
# Check that we can call fields() on either a class or instance,
You can’t perform that action at this time.
0 commit comments