FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-39942:Fix `TypeVar` fails when missing `__name__` by hongweipeng · Pull Request #19616 · python/cpython · GitHub

/ cpython Public

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

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
hongweipeng:issue39942
Apr 20, 2020
Merged

bpo-39942:Fix TypeVar fails when missing __name__#19616
ilevkivskyi merged 2 commits into
python:masterfrom
hongweipeng:issue39942

Conversation

hongweipeng commented Apr 20, 2020
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

remilapeyre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Comment thread Lib/typing.py
if def_mod != 'typing':
self.__module__ = def_mod
try:
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Lib/test/test_typing.py
with self.assertRaises(TypeError):
TypeVar('X', str, float, bound=Employee)

def test_missing__name__(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
    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

Copy link
Copy Markdown
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/

ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good, thanks!

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
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>

Copy link
Copy Markdown

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>

Copy link
Copy Markdown

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 deleted the issue39942 branch April 21, 2020 01:10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

6 participants


Back | FazBrowse Home | New Git URL