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

Support relative imports in AddImportsVisitor. #585

Merged
merged 8 commits into from
Jan 8, 2022
Merged

Support relative imports in AddImportsVisitor. #585

merged 8 commits into from
Jan 8, 2022

Conversation

martindemello
Copy link
Contributor

  • Adds an Import dataclass to represent a single imported object
  • Refactors AddImportsVisitor to pass around Import objects
  • Separates out the main logic in get_absolute_module_for_import so that
    it can be used to resolve relative module names outside of a cst.Import
    node
  • Resolves relative module names in AddImportsVisitor if we have a
    current module name set.

Fixes #578

* Adds an Import dataclass to represent a single imported object
* Refactors AddImportsVisitor to pass around Import objects
* Separates out the main logic in get_absolute_module_for_import so that
  it can be used to resolve relative module names outside of a cst.Import
  node
* Resolves relative module names in AddImportsVisitor if we have a
  current module name set.

Fixes #578
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 6, 2022
Copy link
Member

@zsol zsol left a comment

Choose a reason for hiding this comment

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

Overall nice!

How about a test case with a from .. import E as foo style import?

libcst/codemod/visitors/_imports.py Outdated Show resolved Hide resolved
libcst/codemod/visitors/_imports.py Outdated Show resolved Hide resolved
libcst/codemod/visitors/_imports.py Show resolved Hide resolved
libcst/codemod/visitors/tests/test_add_imports.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jan 7, 2022

Codecov Report

Merging #585 (79d7acd) into main (3578f2f) will decrease coverage by 0.00%.
The diff coverage is 98.83%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #585      +/-   ##
==========================================
- Coverage   94.73%   94.72%   -0.01%     
==========================================
  Files         241      242       +1     
  Lines       24660    24772     +112     
==========================================
+ Hits        23362    23466     +104     
- Misses       1298     1306       +8     
Impacted Files Coverage Δ
libcst/helpers/__init__.py 100.00% <ø> (ø)
libcst/codemod/visitors/_imports.py 96.42% <96.42%> (ø)
libcst/codemod/visitors/__init__.py 100.00% <100.00%> (ø)
libcst/codemod/visitors/_add_imports.py 95.45% <100.00%> (+0.08%) ⬆️
libcst/codemod/visitors/_apply_type_annotations.py 93.99% <100.00%> (+0.01%) ⬆️
libcst/codemod/visitors/tests/test_add_imports.py 100.00% <100.00%> (ø)
libcst/helpers/_statement.py 100.00% <100.00%> (ø)
libcst/codegen/generate.py 28.75% <0.00%> (-1.92%) ⬇️
libcst/_nodes/statement.py 95.13% <0.00%> (-0.06%) ⬇️
libcst/matchers/__init__.py 100.00% <0.00%> (ø)
... and 2 more

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 3578f2f...79d7acd. Read the comment docs.

mod = self
# `import ..a` -> `from .. import a`
if mod.relative and mod.obj is None:
mod = replace(mod, module_name="", obj=mod.module_name)
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately replace's first argument is also called obj, so this won't work (as shown by the unit tests)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

huh. any idea why the local tests passed? (perhaps because i only ran them on 3.9?)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah this only seems to be a problem with 3.6 because we use the dataclasses package from pypi, where obj is not a positional-only argument.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@zsol zsol left a comment

Choose a reason for hiding this comment

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

Nice, thanks! I'll merge if CI is green.

@zsol zsol merged commit 8652974 into Instagram:main Jan 8, 2022
@martindemello martindemello deleted the add-imports branch January 10, 2022 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support relative imports in AddImportsVisitor
4 participants