-
-
Notifications
You must be signed in to change notification settings - Fork 33k
bpo-44747: Refactor usage of sys._getframe at typing module #27387
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
Conversation
@Fidget-Spinner Could you please review this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this mostly looks good. Could you please change your PR title to Refactor usage...
instead of Reduce usage...
? I thought you'd found some clever hack to avoid sys._getframe
outright at first 😉 .
BTW, after ae0a2b7, avoiding sys._getframe
could yield us very good performance gains. So there's some incentive to that, though I don't know how we can get rid of it.
Lib/typing.py
Outdated
if '.' in name: | ||
name = name.rpartition('.')[-1] | ||
self.__name__ = name | ||
self.__module__ = _callee(default='typing') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused. Why don't we need this anymore? I thought the new default is __main__
which is different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we should do it like it did at other classes:
def_mod = _callee()
if def_mod != 'typing':
self.__module__ = def_mod
This line was written by me and it can lead to pickling errors cause in case if name is not present it will set module name to typing
and typing
module can not contains this NewType
.
What is your opinion regarding that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow interesting! Are you able to to reproduce this with a small peice of code? If you can, we should backport this specific change (and you should open a separate PR for this, tied to the pickling/serialization issue). We can't backport the current PR entirely because it is refactoring, not bugfix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code to snippet to reproduce this issue:
import pickle
from typing import NewType
del __name__
MyNewType = NewType("MyNewType", int)
pickle.dumps(MyNewType)
Traceback (most recent call last):
File "...", line 9, in <module>
pickle.dumps(MyNewType)
^^^^^^^^^^^^^^^^^^^^^^^
_pickle.PicklingError: Can't pickle typing.MyNewType: attribute lookup MyNewType on typing failed
I am not sure that it's okay to delete __name__
attribute of module)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Please create a separate PR just for this and link to https://bugs.python.org/issue44353.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New issue has been created.
link to issue - https://bugs.python.org/issue44761
lint to PR - #27406
Misc/NEWS.d/next/Library/2021-07-27-12-06-19.bpo-44747.epUzZz.rst
Outdated
Show resolved
Hide resolved
Hmm, also I can't verify this from the devguide, but I had the impression that refactoring can usually |
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
LGTM. Lets wait for your other PR to land first. BTW minor nit and feel free to ignore it: (see #27387 (comment)) |
I find this goes somewhat outside of an acceptable refactor for backporting to 3.10 and 3.9 due to changes to |
@Fidget-Spinner @ambv How do you think |
The implementation of
In this case usage of |
Oh, by the way, it just appeared to me. The function is called wrong. callee - n. The person who is called by the caller (on the telephone) The function should be called |
Looks like we can explicitly set Code to show this behavior: ns = {}
exec("""
import typing
__name__ = None
T = typing.TypeVar("T")
P = typing.ParamSpec("P")
NT = typing.NewType("NT", int)
""", ns)
for o in (ns["T"], ns["P"], ns["NT"]):
print(o, o.__module__)
I don't know if it's valid case when someone explicitly set |
If the user explicitly sets The only time |
# Conflicts: # Lib/typing.py
@ambv: Please replace |
https://bugs.python.org/issue44747