* imports are not handled well

Bug #1648647 reported by Ethan Furman
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pyflakes
Won't Fix
Undecided
Unassigned

Bug Description

Currently, no effort is made to check names from a * import. Attached is a basic patch that does.

It works by exec'ing the "from ... import *" and using a locals dictionary to avoid polluting the module namespace, then adding all the names found as additional bindings. checkDeadScopes is modified to not report unused names from the * import as unused.

A better solution is probably to recursively run pyflakes on the * imported module to avoid possible side effects, but that is beyond my available time.

Revision history for this message
Ethan Furman (ethan-stoneleaf) wrote :
Revision history for this message
Ethan Furman (ethan-stoneleaf) wrote :

Modified patch to fix the false positive of

... may be undefined, or defined from star imports: ...

Revision history for this message
Ethan Furman (ethan-stoneleaf) wrote :
Revision history for this message
asmeurer (asmeurer) wrote :

Patches should be submitted as GitHub pull requests, but a heads up that this is likely to get rejected, as imports can execute arbitrary code (and aside from that, this would slow down pyflakes by quite a bit).

Revision history for this message
Phil Frost (bitglue) wrote :

Indeed. The contribution is appreciated, but pyflakes by design does not execute any of the code being checked.

Changed in pyflakes:
status: New → Won't Fix
Revision history for this message
Ethan Furman (ethan-stoneleaf) wrote :

I did not expect to have the patch accepted as-is. It's mainly a blue print of the needed changes, with the actual acquisition of the * module globals a proof-of-concept.

Revision history for this message
Phil Frost (bitglue) wrote :

Please feel free to open a PR on GitHub, though be advised anything that imports or executes code under test will probably not be accepted. It's very slow, and can have undesirable side effects.

Revision history for this message
asmeurer (asmeurer) wrote :

Even a static check would have to make some assumptions about the state of the file system and sys.path when the import happens. That could mean weird errors where pyflakes passes or fails depending on what version of a dependency happens to be installed, but a more realistic problem is that you have to deal with egg .pth files and all that nonsense.

Which isn't to say I necessarily think it can't or shouldn't be done (with some loose assumptions). But I've always felt that import * is "bad" anyway and pyflakes bailing on it isn't a huge deal.

Exception: relative import * imports in __init__.py. I really wish those worked. Maybe limiting this to only relative imports would be acceptable? I know Phil has already stated in other issues that pyflakes reading code from other files in the same module would be acceptable for pyflakes.

Revision history for this message
Ethan Furman (ethan-stoneleaf) wrote :

No worries of pyflakes failing -- if it's unable to read the * module it falls back to the default behavior.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.