Description
Knowing the type of the expressions in the AST is useful for IDEs (and IDE plugins) to support autocompletion, for static analyzers and for refactoring tools. As a matter of fact, both jedi
and pylint
have their own heuristics for type inference.
In my specific use case I would like to use the type information to write a safe refactoring tool. The idea is to be able to say that you want to refactor specific methods. For example one could want to refactor string.find
into string.index
, as:
pos = expr.find(x)
if pos >= 0:
# do something with pos
else:
# do something else
where expr
is any string expression, like 'string'
, string_var
, (var + 'foo')
, string_var.replace(' ', '-')
, etc.
into
try:
pos = var.index(x)
# do something with pos
except ValueError:
# do something else
To safely do this refactoring, one needs to be able to query the type of sub expressions, given their location in the source file. Otherwise, given that find
is a common name present in many different classes, the refactoring, would blindly be applied to all of them.
Note: this is the feature request version of issue #4713.