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

fix relative import of mock module #554

Closed
wants to merge 6 commits into from
Closed

Conversation

vivster7
Copy link

Fixes #549

Description

from mock import mock is syntactically valid.
Without this patch, this transforms to from unittest.mock import mock (syntactically invalid)
With this patch, this line becomes from unittest import mock

Details

This patch looks for the identifier mock in the names part of the ImportFrom AST node. Then, it handles this case in one of two ways:

  1. len(names) == 1.
    a. rewrite the module: mock -> unittest
  2. len(names) > 1.
    a. rewrite the module: mock -> unittest.mock
    b. remove the identifier mock from the list of names.
    c. add import on a new line: from unittest import mock

@vivster7
Copy link
Author

Looks like I'm failing a coverage test (which is super neat!) -- will fixup and reopen.

@vivster7 vivster7 marked this pull request as draft October 11, 2021 22:54
@vivster7 vivster7 marked this pull request as ready for review October 12, 2021 02:21
@sadikkuzu
Copy link
Sponsor Contributor

@vivster7
For avoiding those auto-fixes, you can run pre-commit install in your local folder.

@asottile
Copy link
Owner

via #673

@asottile asottile closed this Jul 10, 2022
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.

Incorrect mock upgrade
3 participants