bpo-34776: Fix dataclasses to support __future__ "annotations" mode#9518
Conversation
gvanrossum
left a comment
There was a problem hiding this comment.
I wonder if the fix shouldn't apply to all callers of _create_fn()? I know __init__ is the only one that (currently) has annotations, but in general I wish the generated functions looked more like handwritten ones to various introspection tools.
Sorry, something went wrong.
I think there's no harm in doing that. I've updated the PR. It's a bit more invasive now, but the benefit is that we always use the same mechanism (a closure) to inject objects into generated code. |
Sorry, something went wrong.
|
BTW this looks like a relatively safe change for 3.7.1, but it's Eric's call. |
Sorry, something went wrong.
gvanrossum
left a comment
There was a problem hiding this comment.
I don't follow the logic in dataclasses.py well enough to approve this, though I see nothing wrong other than a missing trailing comma. @ericvsmith will have to have the last word.
Sorry, something went wrong.
|
I'll try and look at this today. Thanks, @1st1 and @gvanrossum for the code and reviews. |
Sorry, something went wrong.
|
According to https://discuss.python.org/t/3-7-1-and-3-6-7-release-update/184 there's still couple of days to get this into 3.7.1. @ericvsmith do you have time for a review? |
Sorry, something went wrong.
|
FWIW we can just merge the 39006fb commit to 3.7 which does the trick in the most minimal way. |
Sorry, something went wrong.
|
Unfortunately I'm out of town until 2018-10-10 with limited internet access until then. I won't be able to do a decent review until then. |
Sorry, something went wrong.
|
Hi Eric, Python 3.7.2rc1 has been released but this PR is still not merged. Is it possible to somehow unblock it? |
Sorry, something went wrong.
|
I apologize for being tied up with a client on end-of-year work. I don't see how this could get in to 3.7.2, but I'm planning on looking at it in January. |
Sorry, something went wrong.
|
It looks like https://bugs.python.org/issue37948 will be also fixed by this PR. |
Sorry, something went wrong.
ilevkivskyi
left a comment
There was a problem hiding this comment.
FWIW the fix looks good to me. It would be great to add few more comments and a test for plain string annotations and/or https://bugs.python.org/issue37948
Sorry, something went wrong.
It won't, I just tested with https://github.com/1st1/cpython/tree/fix_dc_introspect |
Sorry, something went wrong.
@a-recknagel : Could you post your test code and the results? Thanks. |
Sorry, something went wrong.
|
@ericvsmith Sure. python was built from e38de43, and I ran >>> import dataclasses
>>> import typing
>>> A = dataclasses.make_dataclass('A', ['var'])
>>> typing.get_type_hints(A)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/arne/pythons/python_dc_fix/lib/python3.8/typing.py", line 972, in get_type_hints
value = _eval_type(value, base_globals, localns)
File "/home/arne/pythons/python_dc_fix/lib/python3.8/typing.py", line 259, in _eval_type
return t._evaluate(globalns, localns)
File "/home/arne/pythons/python_dc_fix/lib/python3.8/typing.py", line 463, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'typing' is not definedWhich is the same behavior as on the current master. |
Sorry, something went wrong.
|
@ambv: Please replace |
Sorry, something went wrong.
…ythonGH-9518) (cherry picked from commit d219cc4) Co-authored-by: Yury Selivanov <yury@magic.io>
…ythonGH-9518) (cherry picked from commit d219cc4) Co-authored-by: Yury Selivanov <yury@magic.io>
https://bugs.python.org/issue34776