[issue34978] check type of object in fix_dict.py in 2to3

Pranav Devarakonda report at bugs.python.org
Mon Oct 15 14:08:33 EDT 2018


Pranav Devarakonda <devarakondapranav at yahoo.com> added the comment:

Thank you very much for pointing out some helpful things,  Karthikeyan. 

>True if 1 in list(a.keys()) if type(a) == dict else a.keys() else False

True that the fixer would return a syntax error in this case. I missed adding a pair of parenthesis to the whole if statement. I mean

True if 1 in (list(a.keys()) if type(a) == dict else  a.keys()) else False

is valid code. So adding parenthesis would solve the problem as I did in the updated patch I uploaded.

I don't see this updated fixer returning errors in other cases like nested if conditions or list comprehensions, since the fixer would convert the respective function calls of dict objects into separate statements, would not interfere with other if statements(or any other statements for that matter) and set the order of precedence appropriately.

Please do point out if there any other cases I missed. 

I know this is a less pythonic but is more accurate :) Thanks.

----------
Added file: https://bugs.python.org/file47871/fix_dict.py

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


More information about the Python-bugs-list mailing list