-
Notifications
You must be signed in to change notification settings - Fork 180
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
"import as" can cause spurious "imported but unused" warnings #159
Comments
Original comment by bcs26 on Launchpad: Agreed that the warning targets the second "import". The warning itself, however, does seem to be caused by the presence of "import as". The following (effectively identical) code, for example, correctly does not trigger a warning: import selenium selenium.webdriver.Firefox("foo") Not sure I understand the comment about it not being a bug: an "unused" warning is generated, but both import lines are in fact essential (the code will raise an error if either is removed). |
Original comment by florent.x (@florentx?) on Launchpad: In the "import as" example, the name "selenium" is assigned in the global namespace but it is not used in the module. Only "se" is effectively used. There are different ways to work around this and avoid putting an unused name in the globals. For example: from selenium import webdriver
webdriver.Firefox("foo") Or another example: import selenium as se
__import__('selenium.webdriver')
se.webdriver.Firefox("foo") Or you keep the code as-is, and you just ignore this pyflakes message. |
Original comment by bcs26 on Launchpad: Ok, it honestly sounds like we just disagree on this. If you'd like, I'm happy to call this a feature request: it would be helpful if pyflakes did not generate a warning for this code, since the only alternative is adding code solely to suppress the warning. That said, while the name may be unused, the import operation is obviously essential. Using (As a perhaps-interesting aside, using "del selenium" to remove the name generates a different warning.) |
Original comment by florent.x (@florentx?) on Launchpad: I keep the issue open, since you don't like the alternative examples which were proposed: import selenium (or) from selenium import webdriver For the other remark, regarding the unassignment of globally imported objects, I opened bug 1162161 and attached a patch to it. |
Original comment by bcs26 on Launchpad: Sounds fair. Appreciated the discussion. On Sat, Mar 30, 2013 at 9:13 AM, Florent wrote:
|
Original comment by jayvdb (@jayvdb?) on Launchpad: It is possible, but not easy, for pyflakes to detect that IMO the two options provided in #159 (comment) are the correct "flake free" code, and IMO this is the most desirable: from selenium import webdriver If a more descriptive prefix is desirable, that could be: from selenium import webdriver as selenium_webdriver |
Original comment by bcs26 on Launchpad: Both of those alternatives are totally reasonable... but at least IMO they don't feel super great. For many larger packages (e.g. numpy) an "import numpy as np" style is extremely common, but you still need to import various submodules. If you apply the pattern above, you end up with "import numpy.modulea as np_modulea", "import numpy.random.moduleb as np_random_moduleb", etc. That obviously works but ends up creating two independent styles of usage. Not to mention the repetitive boilerplate in the actual imports. Ultimately it feels like working around an implementation limitation on the linter side. Fine if that's what it takes, better if it's unnecessary! |
the second import is purely for side-effects -- I believe pyflakes is correct to flag this as a programming error/smell |
Original report by bcs26 on Launchpad:
Pyflakes will generate a spurious "imported but unused" warning for the following code:
import selenium as se
import selenium.webdriver
se.webdriver.Firefox("foo")
Note that Pyflakes will, correctly, not generate such a warning for the following:
import selenium
import selenium.webdriver
se = selenium
se.webdriver.Firefox("foo")
The text was updated successfully, but these errors were encountered: