Skip to content
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

bpo-39942:Fix TypeVar fails when missing __name__ #19616

Merged
merged 2 commits into from Apr 20, 2020

Conversation

hongweipeng
Copy link
Contributor

@hongweipeng hongweipeng commented Apr 20, 2020

Copy link
Contributor

@remilapeyre remilapeyre left a comment

Choose a reason for hiding this comment

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

Thanks for opening a pull request @hongweipeng, I tested the changes and it works well, I'm not sure if defaulting to __main__ is the correct behavior thought. If I'm correct both Enums and dataclasses had this issue and they are now just unpickable when __name__ cannot be determined.

if def_mod != 'typing':
self.__module__ = def_mod
try:
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it's safe to assume __name__ is __main__ when it's not defined here. I think other classes like enums make the class unpickable when __name__ cannot be determined.

Copy link
Member

Choose a reason for hiding this comment

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

I think '__main__' is fine. By the way a similar code is used by TypedDict and NamedTuple, so you can copy it here. Or maybe it would even make sense to factor this out in an internal helper function doing this:

    try:
        module = sys._getframe(1).f_globals.get('__name__', '__main__')
    except (AttributeError, ValueError):
        module = None

@@ -221,6 +221,13 @@ def test_bound_errors(self):
with self.assertRaises(TypeError):
TypeVar('X', str, float, bound=Employee)

def test_missing__name__(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

    def test_missing__name__(self):
        # See bpo-39942
        code = ("import typing\n"
                "T = typing.TypeVar('T')\n"
                )
        exec(code, {})

would be more like the other tests

@remilapeyre
Copy link
Contributor

This change will also require a NEWS entry (https://devguide.python.org/committing/#what-s-new-and-news-entries), you can add one by using https://blurb-it.herokuapp.com/

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@ilevkivskyi
Copy link
Member

Actually, it probably makes sense to backport this to 3.7 and 3.8 since this is technically a bug. Let me try if this still can be done automatically.

@miss-islington
Copy link
Contributor

Thanks @hongweipeng for the PR, and @ilevkivskyi for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Thanks @hongweipeng for the PR, and @ilevkivskyi for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 20, 2020
…19616)

https://bugs.python.org/issue39942
(cherry picked from commit a25a04f)

Co-authored-by: HongWeipeng <hongweichen8888@sina.com>
@bedevere-bot
Copy link

GH-19626 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 20, 2020
…19616)

https://bugs.python.org/issue39942
(cherry picked from commit a25a04f)

Co-authored-by: HongWeipeng <hongweichen8888@sina.com>
@bedevere-bot bedevere-bot removed the needs backport to 3.8 only security fixes label Apr 20, 2020
@bedevere-bot
Copy link

GH-19627 is a backport of this pull request to the 3.8 branch.

miss-islington added a commit that referenced this pull request Apr 20, 2020
https://bugs.python.org/issue39942
(cherry picked from commit a25a04f)

Co-authored-by: HongWeipeng <hongweichen8888@sina.com>
miss-islington added a commit that referenced this pull request Apr 20, 2020
https://bugs.python.org/issue39942
(cherry picked from commit a25a04f)

Co-authored-by: HongWeipeng <hongweichen8888@sina.com>
@hongweipeng hongweipeng deleted the issue39942 branch April 21, 2020 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants