[New-bugs-announce] [issue38871] lib2to3 generates invalid code with filter and ternary operator

Zoran Simic report at bugs.python.org
Wed Nov 20 18:12:36 EST 2019


New submission from Zoran Simic <zsimic at netflix.com>:

This code snippet exposes a small edge case in lib2to3, where syntactically invalid code is generated:

data = [1, 2, 3, 4, 5]
x = filter(lambda x: True if x > 2 else False, data)
print(x)


lib2to3 transforms 2nd line above to:
x = [x for x in data if True if x > 2 else False]


which is invalid (would be OK with parentheses after 1st 'if')

Admittedly, the original code here is more complex that it should be ('True if foo else False' being equivalent to just 'foo'), but walked into this in "the real world" and wanted to report it.

----------
components: 2to3 (2.x to 3.x conversion tool)
messages: 357112
nosy: Zoran Simic
priority: normal
severity: normal
status: open
title: lib2to3 generates invalid code with filter and ternary operator
type: behavior
versions: Python 3.7, Python 3.8

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


More information about the New-bugs-announce mailing list