asottile / pyupgrade

A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.
MIT License
3.62k stars 183 forks source link

unwrap exec/eval literals #486

Open graingert opened 3 years ago

graingert commented 3 years ago

A common pattern in 2/3 compatible codebases is to wrap new Python 3 syntax with exec or eval with a string literal.

Examples can be seen six.reraise, various bits of twisted and tulip:

This also applies to 3.6-3.10 codebases for example positional only arguments:

if sys.version_info >= (3, 8):
    exec(
        """def fancyStuff(fn, \, ham, spam): ...
        """
    )
else:
    def fancyStuff(args, ham, spam):
        fn, *args = args
        ... 
asottile commented 3 years ago

I think this would be really difficult to do in the general case, but certainly possible