[issue38871] lib2to3 generates invalid code with filter and ternary operator

Dong-hee Na report at bugs.python.org
Sat Nov 23 14:41:58 EST 2019


Dong-hee Na <donghee.na92 at gmail.com> added the comment:

Dear core developers,

I 'd like to discuss fixing this issue.
IMHO, There are 2 options to fix this issue.

1. Add parenthesize when creating ListComp.
The change will be as follow:
             new = ListComp(results.get("fp").clone(),
                            results.get("fp").clone(),
                            results.get("it").clone(),
-                           results.get("xp").clone())
+                           parenthesize(results.get("xp").clone()))

But generated codes are not pretty.

2. Transform the lambda function as same as a normal function.
The only needed is removing lambda specific logics on fixer.

The generated code will be like this in this case.
x = filter(lambda x: True if x > 2 else False, data)
x = list(filter(lambda x: True if x > 2 else False, data))

Personally, I prefer the 2nd option because it's more clear.
If the proposal is accepted, I 'd like to fix this issue. 

If there are any ideas, please let me know.
Thank you always!

----------
nosy: +benjamin.peterson

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38871>
_______________________________________


More information about the Python-bugs-list mailing list