diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 95ff39287bed61..5247d229acc240 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -228,6 +228,9 @@ class InitVar: def __init__(self, type): self.type = type + def __call__(self, *args, **kwargs): # required by typing.get_type_hints, see bpo-44799 + raise TypeError("InitVar object is not callable") + def __repr__(self): if isinstance(self.type, type): type_name = self.type.__name__ diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 8e645aeb4a7503..68caacada1ebd8 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -3831,5 +3831,19 @@ class A: c: int = 1 d: int + def test_lazy_init_var_get_type_hints(self): + @dataclass + class Foo: + a: "InitVar[int]" + + hints = get_type_hints(Foo) + + self.assertIsInstance(hints.get("a"), InitVar) + self.assertEqual(hints.get("a").type, int) + + def test_init_var_is_not_callable(self): + with self.assertRaisesRegex(TypeError, r"InitVar object is not callable"): + InitVar[int]() + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2021-08-02-16-21-59.bpo-44799.hXfxPO.rst b/Misc/NEWS.d/next/Library/2021-08-02-16-21-59.bpo-44799.hXfxPO.rst new file mode 100644 index 00000000000000..67971226acedef --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-08-02-16-21-59.bpo-44799.hXfxPO.rst @@ -0,0 +1,2 @@ +Fix issue when ``dataclass.InitVar`` can not be evaluated using +``typing.get_type_hints``. Patch provided by Yurii Karabas.