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

Always report imports shadowed by another import #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jayvdb
Copy link
Member

@jayvdb jayvdb commented Nov 15, 2015

Previously only unused imports redefined by another
imports were reported as RedefinedWhileUnused.

A new ImportShadowedByImport message now always
reports the re-import except when an import in the
doctest scope shadows an import in the module scope.

Previously only unused imports redefined by another
imports were reported as RedefinedWhileUnused.

A new ImportShadowedByImport message now always
reports the re-import except when an import in the
doctest scope shadows an import in the module scope.
''',
m.RedefinedWhileUnused, m.RedefinedWhileUnused,
m.UnusedImport, m.UnusedImport)
''', *(m.ImportShadowedByImport, m.RedefinedWhileUnused,
Copy link
Member

Choose a reason for hiding this comment

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

What's the use of the *(...) construction here?

Copy link
Member Author

Choose a reason for hiding this comment

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

To simplify neater indentation.

Copy link
Member

Choose a reason for hiding this comment

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

Eh, this isn't exactly clear or obvious. I understand why, but I'm not sure I agree with it.

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries; I can rework it when I create a narrower patch after #54 has been approved.

@jayvdb
Copy link
Member Author

jayvdb commented Nov 15, 2015

Yea, I was going to note that this and #44 should not both be merged without amendments, but since you require fast-forward merges only that note isnt necessary.

@bitglue
Copy link
Member

bitglue commented Dec 3, 2015

Sorry I haven't been keeping up with the PRs lately. I'm busy with work stuff this week but I'll try to take a look at these next week. Alternately, I'm fine with any of these changes being merged if anyone else reviews them first.

@ambv
Copy link
Member

ambv commented Mar 14, 2016

Similarly to #55, I would use this but it doesn't belong to pyflakes on the basis of not reporting an actual error. A superfluous import is absolutely harmless and a local, alternative import is useful (even if constitutes questionable style).

I do agree unused imports break the "errors only" rule, too, but this is historical and it seems to me it would open a Pandora box of other warning-type suggestions if I let this one in. It really seems we need a flake8-likely-bugs plugin that sits between what pyflakes tests for (hard errors) and what pep8 does (pure style suggestions).

@bitglue
Copy link
Member

bitglue commented Mar 15, 2016

Superfluous imports aren't harmless: they have (sometimes significant) performance consequences and create edges in the dependency graph that are unnecessary which can lead to dependency cycles that cause imports to fail in confusing and mysterious ways.

In fact this very problem was the reason pyflakes was written in the first place.

I think you miss the point of pyflakes. It is not to catch "hard errors". That's what unit tests are for. It's to catch "soft errors" which would not be caught by a unit test, but only actual errors, not maybe errors.

There is never any reason to import something and not use it, with one exception: importing stuff into init.py or similar to make an ubermodule that consolidates the namespaces of multiple submodules. And this is why we have __all__.

@asmeurer
Copy link
Contributor

There are use-cases (like when a module import doesn't exist until you import some other thing, like https://twitter.com/asmeurer/status/468514695154921472), but they are very rare, and I think generally can be worked around. I agree the unused imports, while they seem harmless, are actually useful, in the same way that unused variables are useful. If you imported something and don't use it, that often indicates that you forgot something (not as often as importing a variable and not using it, but often enough). I've also had pyflakes tell me that I'm not using an external module any more, meaning my code had an unnecessary unused dependency from an unused import. And unused imports are trivial to fix, and I doubt anyone will argue against doing so for some style reason (contrast this with some pep8 warnings, some of which I strongly disagree with). And FWIW I disagree that you should have to define __all__ in __init__.py for pyflakes to not warn about unused imports/names.

@ambv
Copy link
Member

ambv commented Mar 15, 2016

@bitglue, by a superfluous import I mean the exact case covered by this pull request, e.g. an import done within the function scope, when a module-level import has already been made. These have almost zero runtime penalty as they're reusing modules already present in sys.modules.

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

Successfully merging this pull request may close these issues.

5 participants