Example of " remove unused imports" can have bad side effects in python?
Asked Answered
I

5

7

I'm looking for an example of how removing unused imports can lead to breaking otherwise valid programs. My question was inspired looking at this issue: https://github.com/psf/black/issues/86

Could someone please point me to an example?

Inesinescapable answered 30/7, 2019 at 16:17 Comment(0)
D
8

When you import a module in Python, you execute all the code in that file (and all of its imports).

By convention only this code is side-effectless and usually just provides classes and such, but there's no real reason this has to be true. A poorly written module could perform necessary steps to function correctly as part of the code triggered during import.

For example it's entirely possible to have code like:

# my_db/init.py
    from my_db.core import MyDB

    MyDB.do_very_important_setup()


# my_db/other.py
    import my_db.init
    from my_db.core import MyDB

    MyDB.do_some_work_assuming_setup_has_occurred()

Is this horrible? Yes. Should you do it? No.

Can you guarantee nobody else has?

Declaration answered 30/7, 2019 at 16:32 Comment(0)
B
2

When a module first gets imported, all the code within that module is run. Convention and good practice dictates that the module shouldn't make any changes to the rest of the environment, but this isn't always the case (for example, a module could change a couple of variables in sys for the entire program, when it's first imported).

It is possible that someone could import such a module purely for its side effects, and then never explicitly refer to it in the rest of their code. A linting/formatting tool like black would then detect it as unused, and remove it, and suddenly the side-effects it was having are no longer in effect, and the program breaks as a result.

I can't come up with any specific examples at this time, but I know I've seen at least one before, and I was personally working on such a module until I realized it was a poor idea and stopped.

Brooklet answered 30/7, 2019 at 16:29 Comment(0)
E
0

Consider you have following structure:

- root
   |--folder1
         |-- __init__.py
         |-- server.py
   |--main.py

And following content:

# main.py
import folder1 as f
print("I am doing stuff but don't need f")


# __init__.py
from folder1 import server


#  server.py
# Start a server, or do something else you need

When you remove the folder1 import in main.py it would stop doing the thins in __init__.py and server.py. I would definetly consider that as bad code, but it is possible.

Expeditionary answered 30/7, 2019 at 16:32 Comment(0)
G
0

I don't know if this answers your question, but if you remove the unused import, the first 2 icons are not displayed anymore.

https://github.com/theonlycodingbear/unused-imports-problem.git

Gasket answered 18/8, 2019 at 20:20 Comment(0)
H
0

To give a way that this could cause problems without necessarily being bad practice - You could be importing the function into a file for ease of use in something eternal (like a command line call).

For example, with the following structure:

- my_api
    |--init.py
    |--main.py

# main.py
def get_app():
    # Do Server Stuff


# __init__.py
from my_api.main import get_app as run

Then if you wanted to run it using uvicorn for example, you could do:

uvicorn my_api:run --reload 

Instead of:

uvicorn my_api:main.get_app --reload 

Removing the unused import there would also remove your external shortcut.

I've seen some packages where people do something similar to organize or group imports together as well; however, not really sure if that's technically bad practice, but I don't really like it. It usually just seems to add to confusion.

Historiographer answered 5/9 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.