isort icon indicating copy to clipboard operation
isort copied to clipboard

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

Open nh916 opened this issue 1 year ago • 2 comments

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:

  • Provide an option to treat # type: ignore comments as "anchored" to their original line or statement, preventing isort from moving them during the sorting process.
  • Ensure # type: ignore comments maintain their position relative to the import statement they are intended to modify.

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.

nh916 avatar Mar 05 '24 03:03 nh916

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).

stdedos avatar Jul 10 '24 10:07 stdedos