PyCQA / isort

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

Feature Request: Improved Handling of `# type: ignore` Comments to Preserve Mypy Compatibility #2244

Open nh916 opened 8 months ago

nh916 commented 8 months ago

Description:

When using isort in conjunction with mypy, the placement of # type: ignore comments on multi-line imports can lead to issues where mypy either doesn't recognize the ignore directive properly or isort inadvertently moves the comment in a way that changes its intended effect. This can result in mypy reporting errors for ignored imports.

Issue:

The specific issue arises with multi-line imports that include a # type: ignore comment to bypass mypy checks for a particular import statement. isort, when reformatting import statements, can move the # type: ignore comment in a way that changes its scope or applicability, potentially leading to mypy errors or failing to ignore the intended line.

Example

Before isort:

This comment correctly ignores the mypy issue with this package missing type hinting

from example.package import ( # type: ignore
    ModuleA as RenamedA,
    ModuleB,
)

After isort Formatting:

After isort formats the import, it moves the mypy ignore typing comment with it and mypy now flags this import again as missing type hinting because it doesn't know that I am trying to ignore the typing on this package

from example.package import (
    ModuleA as RenamedA,  # type: ignore
    ModuleB,
)

In the example above, the # type: ignore comment is intended to ignore the entire import statement. However, after isort processes this code, the comment moves to a position where it only ignores the ModuleA as RenamedA line, causing mypy to not recognize the ignore directive as intended for the entire import block.

Current Remedy:

I could try putting a blanket ignore # isort: skip on the line, but then I'd lose the nice isort formatting features that make the code beautiful and readable.

from example.package import (  # type: ignore # isort:skip
    ModuleA as RenamedA,
    ModuleB,
)

Requested Feature:

I would like to request a feature or configuration option in isort that provides more granular control over the placement of # type: ignore comments in multi-line imports. Specifically, it would be helpful to:

This feature would greatly enhance the interoperability of isort with mypy, especially in codebases that require specific type ignore directives for certain imports.

Thank you for considering this feature request. I believe it would address a common pain point for developers working with both isort and mypy in their projects.

stdedos commented 4 months ago

For https://github.com/pylint-dev/pylint-pytest/blob/1195f5c68ee4f6631fdfd1e0112309d691b8ec62/pylint_pytest/checkers/__init__.py#L12:

When writing

from pylint.interfaces import IAstroidChecker  # type: ignore[attr-defined] # It is defined in pylint~=2

the combination of black and isort format the file on-save between

        # black, 23.9.1: works
        from pylint.interfaces import (  # type: ignore[attr-defined] # It is defined in pylint~=2
            IAstroidChecker,
        )

and:

        # isort, 5.12.0 / 5.13.2: fails
        from pylint.interfaces import \
            IAstroidChecker  # type: ignore[attr-defined] # It is defined in pylint~=2

Moreover, they compete endlessly with each other (one's format is not acceptable by the other).