Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-44799: Fix InitVar resolving using get_type_hints #27553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this solve the problem at hand?

I'm unlikely to accept any changes to dataclasses and typing until PEP 649 is ruled on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception raised by _type_check. In case if object is callable this object won't raise exception.

cpython/Lib/typing.py

Lines 180 to 181 in e06ae75

if not callable(arg):
raise TypeError(f"{msg} Got {arg!r:.100}.")

I found that it was the easiest way to solve this issue. If you think it's a bad solution I am open to fix it in another way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just seems like it's papering over some actual problem.

In any event, I'm waiting on PEP 649, which if accepted makes this whole point moot.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will wait on a update regarding PEP 649.

raise TypeError("InitVar object is not callable")

def __repr__(self):
if isinstance(self.type, type):
type_name = self.type.__name__
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue when ``dataclass.InitVar`` can not be evaluated using
``typing.get_type_hints``. Patch provided by Yurii Karabas.