autoflake icon indicating copy to clipboard operation
autoflake copied to clipboard

Expand *all* star imports?

Open fake-name opened this issue 7 years ago • 3 comments

I'm somewhat curious what the current limitations on star-import expansion are due to.

I haven't looked at the code in any depth, but it seems it'd be pretty straight-forward (if not simple) to fully qualify all components imported via star imports. Get the ast of the relevant code file, walk the tree, find items where the code-string doesn't reflect the actual path, and replace.

This, of course, does require building the AST of the source file, which is being processed, which seems more complex then what pyflakes does, so I'm not sure if this is out of scope for what autoflake is trying to do.

fake-name avatar Jan 24 '18 01:01 fake-name

Ok, this is slightly more complex then I'd hoped, because the name resolution happens at runtime, not parse-time (derp).

I think it should still be possible, at least for a limited subset of cases (read: no dynamic method lookup/stringization of functions, etc...).

I have a simple script that can walk the AST, and find start imports, and then use a NodeVisitor to walk the AST tree and find all calls (this should probably be extended to constants, too).

It's probably possible to build a list of the exposed functions/constants/etc in each star-imported import, and then use that to try to resolve out locals.

fake-name avatar Jan 24 '18 03:01 fake-name

@fake-name it may be better to use https://github.com/davidhalter/jedi

graingert avatar Apr 18 '19 09:04 graingert

@graingert - Why are you commenting on random 1+ year old issues?

Also, this is a refactoring tool, something that jedi specifically doesn't do (at least yet). How is that relevant.

fake-name avatar Apr 18 '19 19:04 fake-name