PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.45k stars 574 forks source link

Feature request: Remove superfluous `import a` statements when there is already `import a.b` #2285

Open e-gebes opened 4 weeks ago

e-gebes commented 4 weeks ago

When there is already import a.b then import a is implicitly executed and a is added as a name. Hence, it does not make sense to have both like:

import logging  # this is superfluous
import logging.handlers

This is sufficient, isort should remove the superfluous import

import logging.handlers

Reference:

https://docs.python.org/3/reference/simple_stmts.html#the-import-statement ("Examples") https://docs.python.org/3/reference/import.html#the-module-cache

Note: if there is an import alias, only the last name is bound, hence in that case nothing can be removed:

import logging  # not superfluous
import logging.handlers as log_handlers