Infer correct type from 'and' / 'or' expressions
ClosedPublic

Authored by nalvarez on May 19 2017, 2:19 AM.

Details

Summary

kdev-python was assuming boolean operator expressions like foo or bar always returned bool, but Python defines them to always return the value of either variable depending on whether the first one is false-like. Thus, someString or "bar" should be interpreted as a string, not a boolean.

This commit makes such boolean operator expressions have a type of unsure(type(foo), type(bar)) instead. I also added new tests for this new behavior, and to ensure the good old True or False is still interpreted as a bool.

Test Plan

Added new unit tests, and ensured old ones don't fail. Tested in the IDE with a simple .py file.

Diff Detail

Repository
R53 KDevelop: Python Support
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
nalvarez created this revision.May 19 2017, 2:19 AM
Restricted Application added a subscriber: kdevelop-devel. · View Herald TranscriptMay 19 2017, 2:19 AM

Forgot to add: fixes bug 379961.

flherne accepted this revision.May 19 2017, 1:24 PM
flherne added a subscriber: flherne.

This makes sense, I only have trivial things to mention. Compiling to test it now. Thanks!

duchain/expressionvisitor.cpp
736 ↗(On Diff #14682)

Nitpick - convention for these is either just v or operandVisitor (the latter usually when then there's more than one). I don't have a strong opinion.

duchain/tests/pyduchaintest.cpp
510 ↗(On Diff #14682)

Nitpick - you can use single quotes for the Python strings, instead of backslashed double-quotes. They're semantically identical.

This revision is now accepted and ready to land.May 19 2017, 1:24 PM
brauch accepted this revision.May 19 2017, 9:25 PM

Looks good, thanks! Useful change.

nalvarez marked 2 inline comments as done.May 20 2017, 12:54 AM
nalvarez added inline comments.
duchain/expressionvisitor.cpp
736 ↗(On Diff #14682)

Renamed, mainly for consistency with the other methods which use v.

duchain/tests/pyduchaintest.cpp
510 ↗(On Diff #14682)

Good point! Changed.

This revision was automatically updated to reflect the committed changes.
nalvarez marked 2 inline comments as done.