Skip to content

Commit c68f41f

Browse files
correct identification of stdlib TypeVar objs (#412)
* correct identification of stdlib TypeVar objs In Python <= 3.7, the module attribute of TypeVar constructs is always set to typing and thus irrelevant to the true origin of the construct (stdlib, third-party, user-defined). cloudpickle used to cirumvent this issue in `_whichmodule` by exhaustively search for the construct in all imported modules. This would generate false positive in the case of stdlib TypeVar constructs such as AnyStr being used by imported third-party module. For such TypeVar construct, `_whichmodule` would occasionally flag them as belonging to the third party module. This resulted in a flaky test (`test_lookup_module_and_qualname_stdlib_typevar`) where `typing.AnyStr` was occasionally marked as defined in `_pytest.capture` instead of `typing`. Co-authored-by: Olivier Grisel <[email protected]>
1 parent fb83b83 commit c68f41f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Diff for: cloudpickle/cloudpickle.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,14 @@ def _whichmodule(obj, name):
136136
# Workaround bug in old Python versions: prior to Python 3.7,
137137
# T.__module__ would always be set to "typing" even when the TypeVar T
138138
# would be defined in a different module.
139-
#
140-
# For such older Python versions, we ignore the __module__ attribute of
141-
# TypeVar instances and instead exhaustively lookup those instances in
142-
# all currently imported modules.
143-
module_name = None
139+
if name is not None and getattr(typing, name, None) is obj:
140+
# Built-in TypeVar defined in typing such as AnyStr
141+
return 'typing'
142+
else:
143+
# User defined or third-party TypeVar: __module__ attribute is
144+
# irrelevant, thus trigger a exhaustive search for obj in all
145+
# modules.
146+
module_name = None
144147
else:
145148
module_name = getattr(obj, '__module__', None)
146149

0 commit comments

Comments
 (0)