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

Add the possibility to add import and respect the import sorting. #835

Closed
JonathanPlasse opened this issue Nov 20, 2022 · 5 comments · Fixed by #3787
Closed

Add the possibility to add import and respect the import sorting. #835

JonathanPlasse opened this issue Nov 20, 2022 · 5 comments · Fixed by #3787
Assignees
Labels
core Related to core functionality

Comments

@JonathanPlasse
Copy link
Contributor

@JonathanPlasse
Copy link
Contributor Author

@charliermarsh, does #875 facilitate the implementation of this issue?

@charliermarsh charliermarsh added internal An internal refactor or improvement good first issue Good for newcomers core Related to core functionality and removed enhancement internal An internal refactor or improvement labels Dec 31, 2022
@charliermarsh
Copy link
Member

This would be good to tackle. It has now come up a few times now.

I think the idea would be to track the imports that need to be present for a given code modification, and then do a pass at the end to "add" those imports. It would somehow have to be connected to the autofix API too, to ensure that we only add the import if we end up applying the fix.

You can see how LibCST handles the problem here.

@charliermarsh
Copy link
Member

There are a few sub-problems:

  • How do we determine where the imports should go? It's probably not necessary to put the imports in the "right" spot, since we can rely on the next autofix iteration fixing up the order. But they should go in the first "block".
  • How do we only add imports we don't already have?
  • ...

@charliermarsh
Copy link
Member

I am returning to this.

@charliermarsh
Copy link
Member

Getting close...

diff --git a/foo.py b/foo.py
index 0647ee33b..696a9e873 100644
--- a/foo.py
+++ b/foo.py
@@ -1,3 +1,4 @@
+import sys
 exit = 1

-quit(1)
+sys.exit(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to core functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants