Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
code review
  • Loading branch information
hongweipeng committed Apr 20, 2020
commit 4387dfe2b66bb7955fc4fe2e3489ee727dbd2945
8 changes: 4 additions & 4 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ def test_bound_errors(self):
TypeVar('X', str, float, bound=Employee)

def test_missing__name__(self):

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

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

def test_no_bivariant(self):
Expand Down
6 changes: 3 additions & 3 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,10 @@ def __init__(self, name, *constraints, bound=None,
self.__bound__ = None
try:
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling

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

if def_mod != 'typing':
self.__module__ = def_mod
except (AttributeError, ValueError):
pass
def_mod = None
if def_mod != 'typing':
self.__module__ = def_mod

def __repr__(self):
if self.__covariant__:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Set "__main__" as the default module name when "__name__" is missing in
:class:`typing.TypeVar`. Patch by Weipeng Hong.