Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nh916 opened this issue Mar 5, 2024 · 0 comments

Comments

@nh916
Copy link

nh916 commented Mar 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant