New submission from Zoran Simic <zsi...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue38871>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to