Skip to content

Fix a false negative for unused-import #7621

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

Merged

Conversation

mbyrnepr2
Copy link
Member

@mbyrnepr2 mbyrnepr2 commented Oct 14, 2022

Fix a false negative for unused-import when linting multiple modules & one module uses an import in a type annotation & the same import is unused in a subsequent module.

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Description

Closes #4150

…les & one module uses an import in a type annotation & the same import is unused in a subsequent module.

Closes pylint-dev#4150
@mbyrnepr2 mbyrnepr2 self-assigned this Oct 14, 2022
@mbyrnepr2 mbyrnepr2 added the False Negative 🦋 No message is emitted but something is wrong with the code label Oct 14, 2022
@mbyrnepr2 mbyrnepr2 added this to the 2.15.5 milestone Oct 14, 2022
@mbyrnepr2 mbyrnepr2 added Checkers Related to a checker Bug 🪲 labels Oct 14, 2022
@coveralls
Copy link

coveralls commented Oct 14, 2022

Pull Request Test Coverage Report for Build 3351949749

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0003%) to 95.364%

Totals Coverage Status
Change from base Build 3348231752: 0.0003%
Covered Lines: 17178
Relevant Lines: 18013

💛 - Coveralls

@github-actions

This comment has been minimized.

@mbyrnepr2 mbyrnepr2 marked this pull request as ready for review October 14, 2022 09:05
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix !

@@ -1147,6 +1147,7 @@ def leave_module(self, node: nodes.Module) -> None:
return

self._check_imports(not_consumed)
self._type_annotation_names = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix itself is really simple 😄

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.15.5, 2.16.0 Oct 15, 2022
@Pierre-Sassoulas
Copy link
Member

Not sure about what to do regarding the release we're aiming. It's a bug but at the same time it was a false negative. Maybe 2.16.0 so we do not raise new messages in a patch. If there's an oversight on our side we don't want cascading patches backport ?

@mbyrnepr2
Copy link
Member Author

Not sure about what to do regarding the release we're aiming. It's a bug but at the same time it was a false negative. Maybe 2.16.0 so we do not raise new messages in a patch. If there's an oversight on our side we don't want cascading patches backport ?

Agreed. Happy to wait until 2.16.0.

@mbyrnepr2
Copy link
Member Author

About the primer comment.

I notice that the output for Django, Pandas & Sentry is similar to some other open MRs; it doesn't look like it is caused by this MR.
The black output looks relevant to this MR but, at the same time, it is saying that a new message is emitted while I also see that message emitted on main.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.15.5, 2.16.0 Oct 15, 2022
@github-actions

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

In black I think os is used here : https://github.com/psf/black/blob/f16333e78ba77ab29ab0b75e10891e6c65c6da7e/src/blib2to3/pgen2/pgen.py#L25. Feel like a false positive.

@mbyrnepr2
Copy link
Member Author

mbyrnepr2 commented Oct 15, 2022

In black I think os is used here : https://github.com/psf/black/blob/f16333e78ba77ab29ab0b75e10891e6c65c6da7e/src/blib2to3/pgen2/pgen.py#L25. Feel like a false positive.

I agree with you & I'm happy to dig into that one; but this false positive isn't created by the current MR.

The sample from the black primer will emit an unused-import on main also:

import os
from typing import Union
Path = Union[str, "os.PathLike[str]"]

@Pierre-Sassoulas
Copy link
Member

I agree with you & I'm happy to dig into that one; but this false positive isn't created by the current MR.

Ha, something is wrong with the primer then, sorry.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbyrnepr2 can you merge or rebase on main so we launch the new python 3.11 tests please ?

@Pierre-Sassoulas Pierre-Sassoulas merged commit dfd0e5a into pylint-dev:main Oct 29, 2022
@mbyrnepr2 mbyrnepr2 deleted the 4150_unused_import_false_negative branch October 29, 2022 15:46
@github-actions
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on black:
The following messages are now emitted:

  1. unused-import:
    Unused import os
    https://github.com/psf/black/blob/4bb6e4f64ab3820ab9fae6716cd59479d34b7edf/src/blib2to3/pgen2/pgen.py#L22

Effect on pytest:
The following messages are now emitted:

  1. inconsistent-return-statements:
    Either all return statements in a function should return an expression, or none of them should.
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/pytester.py#L301
  2. no-name-in-module:
    No name 'NOSE_SUPPORT_METHOD' in module '_pytest.deprecated'
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/python.py#L62
  3. no-name-in-module:
    No name 'PytestReturnNotNoneWarning' in module '_pytest.warning_types'
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/python.py#L81
  4. inconsistent-return-statements:
    Either all return statements in a function should return an expression, or none of them should.
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/python.py#L1085
  5. inconsistent-return-statements:
    Either all return statements in a function should return an expression, or none of them should.
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/fixtures.py#L130
  6. inconsistent-return-statements:
    Either all return statements in a function should return an expression, or none of them should.
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/python_api.py#L798
  7. no-name-in-module:
    No name 'NOSE_SUPPORT' in module '_pytest.deprecated'
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/nose.py#L5
  8. no-name-in-module:
    No name 'warn_explicit_for' in module '_pytest.warning_types'
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/config/__init__.py#L62
  9. no-member:
    Module '_pytest.deprecated' has no 'HOOK_LEGACY_MARKING' member
    https://github.com/pytest-dev/pytest/blob/646a46e5f4b1f1ae5a06dcbc91fcdebfc235a28a/src/_pytest/config/__init__.py#L369

This comment was generated for commit 295d0ce

@DanielNoord
Copy link
Collaborator

@mbyrnepr2 The message in black seems like a false positive?

@mbyrnepr2
Copy link
Member Author

It does seem that way, I agree @DanielNoord but we can also see the same happening on the main branch (before it was merged). Perhaps an issue with the primer?

@DanielNoord
Copy link
Collaborator

Probably! Went through my notifications of last week and noticed this message, but pytest is also broken in that comment so could very well be a fluke.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Checkers Related to a checker False Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type hints persist module usage between independent modules (unused-import)
5 participants