PyCQA / PyCQA/isort

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

Open
#2,285 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7k
Forks
687
Avg merge
4h 56m
Merged PRs (30d)
2

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two referenced Python documentation sections and verify the binding behavior for plain and aliased imports. Then locate isort’s import-processing logic and relevant tests; done means removing only redundant plain parent imports while preserving imports needed for aliases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.