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

correct identification of stdlib TypeVar objs #412

Merged
merged 5 commits into from Mar 1, 2021

Conversation

pierreglaser
Copy link
Member

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 can generate false positives in the case of stdlib TypeVar constructs such as AnyStr being used by a imported third-party module. For such TypeVar constructs, _whichmodule would occasionally flag them as belonging to the said 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.

This PR fixes this bug by looking if the TypeVar construct is defined in typing before triggering a search in sys.modules.

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`.
@codecov
Copy link

codecov bot commented Feb 21, 2021

Codecov Report

Merging #412 (142f6ac) into master (cdc704d) will increase coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #412      +/-   ##
==========================================
+ Coverage   91.61%   91.70%   +0.08%     
==========================================
  Files           3        4       +1     
  Lines         656      663       +7     
  Branches      135      136       +1     
==========================================
+ Hits          601      608       +7     
  Misses         34       34              
  Partials       21       21              
Impacted Files Coverage Δ
cloudpickle/cloudpickle.py 86.76% <100.00%> (+0.07%) ⬆️
cloudpickle/__init__.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cdc704d...142f6ac. Read the comment docs.

@pierreglaser
Copy link
Member Author

cc @ogrisel if you want to take a quick look.

Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM besides question below:

cloudpickle/cloudpickle.py Outdated Show resolved Hide resolved
.github/workflows/testing.yml Outdated Show resolved Hide resolved
@ogrisel
Copy link
Contributor

ogrisel commented Feb 27, 2021

Also please document the fix in the changelog.

@ogrisel ogrisel merged commit c68f41f into cloudpipe:master Mar 1, 2021
@ogrisel
Copy link
Contributor

ogrisel commented Mar 1, 2021

Merged. Thanks @pierreglaser!

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

2 participants