pylint
pylint copied to clipboard
features for refactoring non-idiomatic code with Python idioms
Current problem
Recently, a paper could automatically refactor non-idiomatic Python code with nine Python idioms: List/set/dict-comprehension, chain-comparison, truth-value-test, loop-else, star-in-function-call, assign-multiple-targets, for-multiple-targets. And they pulled some requests to GitHub projects and many project members like the feature. And some developers proposed issues for Pylint project such as #5800, a developer said “Would it be an idea for Pylint to output the suggested refactor to the user” in Pylint.
Therefore, I would like to try to integrate the functionality into Pylint, what do you think of the idea?
For example, for the list-comprehension, the paper could refactor the code from GitHub project,
input_files = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".pbxproj"):
input_files.append(path + file)
into
input_files = [path + file for (root, dirs, files) in os.walk(path)
for file in files if file.endswith('.pbxproj')]
. For another example, for the for-multiple-targets, the paper could refactor the code from GitHub project:
for items in params_grads:
assert items[0].name is not model.embed1.weight.name
assert items[0].name is not model.linear_1.weight.name
into
for (items_0, *items_len) in params_grads:
assert items_0.name is not model.embed1.weight.name
assert items_0.name is not model.linear_1.weight.name
Desired solution
If I could do this, currently I want to refer to the source code of Refactoring checker. Maybe I want to create a new class for each python idiom and register them in https://github.com/PyCQA/pylint/blob/main/pylint/checkers/refactoring/init.py. And then, I maybe refer to https://github.com/PyCQA/pylint/blob/055c1920140ebf36501d30307d5ab86965ae185f/pylint/checkers/refactoring/refactoring_checker.py to implement the details of code refactoring.
For example,
- adding msg for each refactoring
- checking code whether is refactorable
- giving suggestions with added msg like Refactoring checker, such as printing "RXXXX: we could refactor ... into ...".
How about the method or some suggestions?
Additional context
https://arxiv.org/abs/2207.05613
Thank you for creating the issue. This is something we often talk about as there's some message that give the replacement outright in text and auto fixing would make a lot of sense. But we did not have an issue for it yet.
Take for example the for_any_all checker, the expected code is clearly given in the message.
It was proposed that it would be better as a standalone command and project with a different release cycle than pylint, for simplicity. Even if most pylint contributors would probably be interested in participating.
There's some design to consider:
- Do we reuse the code of the checkers from pylint (maybe adding a hook to an autofix when using
add_message) ? This would be more suited to something integrated in pylint - Do we code it from scratch instead with a completely new design ? It would be more work at the beginning but less later on (?). We could refactor checkers without breaking the "pylint-autofix" (btw we need a name).
The organization we would create the repository in is also something we need to discuss. (Right now we do not have admin right in PyCQA and can't create a new repository among other issues).