|
8 | 8 | import inspect |
9 | 9 | import unittest |
10 | 10 | from unittest.mock import Mock |
11 | | -from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar |
| 11 | +from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional |
12 | 12 | from collections import deque, OrderedDict, namedtuple |
13 | 13 | from functools import total_ordering |
14 | 14 |
|
@@ -1690,6 +1690,23 @@ def new_method(self): |
1690 | 1690 | c = Alias(10, 1.0) |
1691 | 1691 | self.assertEqual(c.new_method(), 1.0) |
1692 | 1692 |
|
| 1693 | + def test_generic_dynamic(self): |
| 1694 | + T = TypeVar('T') |
| 1695 | + |
| 1696 | + @dataclass |
| 1697 | + class Parent(Generic[T]): |
| 1698 | + x: T |
| 1699 | + Child = make_dataclass('Child', [('y', T), ('z', Optional[T], None)], |
| 1700 | + bases=(Parent[int], Generic[T]), namespace={'other': 42}) |
| 1701 | + self.assertIs(Child[int](1, 2).z, None) |
| 1702 | + self.assertEqual(Child[int](1, 2, 3).z, 3) |
| 1703 | + self.assertEqual(Child[int](1, 2, 3).other, 42) |
| 1704 | + # Check that type aliases work correctly. |
| 1705 | + Alias = Child[T] |
| 1706 | + self.assertEqual(Alias[int](1, 2).x, 1) |
| 1707 | + # Check MRO resolution. |
| 1708 | + self.assertEqual(Child.__mro__, (Child, Parent, Generic, object)) |
| 1709 | + |
1693 | 1710 | def test_helper_replace(self): |
1694 | 1711 | @dataclass(frozen=True) |
1695 | 1712 | class C: |
|
0 commit comments