rope
rope copied to clipboard
Statically infer possible values of a variable
For autoimport to be able to handle __all__
properly, rope would need to be able to statically analyze the module to infer all the possible values that a variable/list may contain:
if foo():
var2 = 'foo'
else:
var2 = 'bar'
another_list = ['abc', 'def'] if baz() else ['ghi', var2]
__all__ = ['name1', f'{var2}_blah'] + another_list
The expected capability is that doing an infer_possible_values(module, '__all__')
should return: ['name1', 'foo_blah', 'bar_blah', 'abc', 'def', 'ghi', 'foo', 'bar']
.
Being able to do value inference would also be useful for autocompletion of, for example, dictionary keys or getattr/setattr/hasattr()
.
Describe the solution you'd like
It is unlikely that value inference could be made to work in all cases using just static analysis, but we can support a number of common constructs.
Describe alternatives you've considered
If we don't have possible values inference, it means that autoimport
would have to never remove any apparently unused imports if a module contains an __all__
.
Additional context
This ticket was split off from #374, specifically the FIXME in _get_all_star_list().
Hazard is that in more complicated scenarios, there could be infinite or a very large number of possible values, which may massively increase the time to do an analysis. So, we'll likely need have a way to limit the depth of the analysis.