Home > Software design >  Detect/update Python2-style code that is no longer needed
Detect/update Python2-style code that is no longer needed

Time:02-26

I maintain a moderately big open source project (~22000 lines of python code)

Because it has been in development for a long time and was originally written for Python 2 there is a lot of unnecessarily complex code. Code that could be simplified (and unified) by using modern Python 3 syntax (for example meta-classes, which have significantly simpler syntax nowadays).

Is there some established way to do this? At least detect python2-isms (maybe using pylint and some specific settings?), but ideally auto-correct them.

If possible, I want the code to refrain from "Python <3.6-isms" as well.

CodePudding user response:

Yes - 2to3 and pyupgrade.

You can top things off with flynt for f-string conversion.

From my shell history:

pyupgrade --py36-plus $(git ls-files '*.py')
flynt $(git ls-files '*.py')

CodePudding user response:

I do not think there are some established ways to convert between -isms, as it depends a lot on the use case and such.

There are some scripts to convert Python2 code to Python3, like 2to3, but it does not consider everything you want to do.

  • Related