Refactor -> Move removes unused imports that are unrelated to the move, which seems like an unintended/unexpected side effect

Answered

Recently I noticed that entire blocks of import statements had disappeared from parts of my code. After a while of tracking it down, I realized it's because I moved a module once.

I could understand the idea of removing unused imports to the module that was moved, though I don't fully agree with there being no option to prevent it if you would rather it just refactors the unused imports. But in this case, I moved moduleA.py from package/ to package/subpackage/, and the import optimizer removed every single import related to package (not just to the module I moved) in a bunch of files that were in my Project dir but not even in package/

I don't think this is an obvious consequence of wanting to Move a file. Personally I can't say I agree with it, but reading up on the forums I got the feeling this is a feature and will stay this way. However, even if it must be there and there can't be an option to disable it - either on the Move command window or in the settings - I feel that there should be a warning that this will happen. It can cause a bit of a mess if you didn't intend for it to happen, and I don't see why a user would expect unrelated imports being modified on a Move command.

2
1 comment

Hi,
Thanks for reporting it.

I filed a ticket to our issue tracker https://youtrack.jetbrains.com/issue/PY-54707/Move-refactoring-shouldnt-remove-unused-imports

Please vote for it and feel free to comment.

0

Please sign in to leave a comment.