On 1/31/20 10:58 PM, Andrew Barnert via Python-ideas wrote:

When refactoring code to rename something, there is no way to tell whether a string—or a substring in a larger string—that matches the name should be changed. But a nameof expression is unambiguously a reference to the name, and therefore the tool knows that it needs to change.

Thinking about this, I have a question, IS there existing a refactoring program that can do the rest of the job in Python well. I am not sure there can be.

Take an example refactoring problem: We have a logging module to save diagnostic information about program operation. The logging class currently has a member log that we want to change to log_file.

In a statically type language, this is fairly easy, scan through the source code, find all references to the symbol log, see if they refer to the logging module or something else (which is easily solvable in a statically typed language, as the compiler needs to do this) and if they refer to the logging module, change the reference. The refactoring program just needs about the same smarts as the compiler.

We can't easily do this in Python. In general, we come to the sequence foo.log, in order to determine if we want to change this, we need to know what foo will be every time it gets there. To determine this we need to back track to figure every path of execution to here.

In fact, the transformation may not be possible (or may require other changes beyond what we want) as there may be a usage that handles many different types, all that happen to have a log member, so to change one class, we need to change them all (this can also happen in C++ templates which also can use duck typing). This lack of local type information makes the refactoring MUCH harder, and as you make the program smart enough to work on handling this, including strings might not be that hard. My guess is that any such refactoring is going to need to ask the programmer about a number of cases of the usage that the program can't determine, and thus can include strings that match too.

--
Richard Damon
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TJLFZANF54K7JZM6NBQC3JI7GCCUKKPV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to