Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive using operator 'AND' while checking keys on dict() #83330

Closed
leonardogalani mannequin opened this issue Dec 28, 2019 · 4 comments
Closed

False positive using operator 'AND' while checking keys on dict() #83330

leonardogalani mannequin opened this issue Dec 28, 2019 · 4 comments
Labels

Comments

@leonardogalani
Copy link
Mannequin

leonardogalani mannequin commented Dec 28, 2019

BPO 39149
Nosy @ronaldoussoren, @ned-deily, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-12-28.19:42:13.136>
created_at = <Date 2019-12-28.18:31:50.418>
labels = ['OS-mac', '3.7', 'invalid']
title = "False positive using operator 'AND' while checking keys on dict()"
updated_at = <Date 2019-12-28.20:31:46.370>
user = 'https://bugs.python.org/leonardogalani'

bugs.python.org fields:

activity = <Date 2019-12-28.20:31:46.370>
actor = 'leonardogalani'
assignee = 'none'
closed = True
closed_date = <Date 2019-12-28.19:42:13.136>
closer = 'serhiy.storchaka'
components = ['macOS']
creation = <Date 2019-12-28.18:31:50.418>
creator = 'leonardogalani'
dependencies = []
files = []
hgrepos = []
issue_num = 39149
keywords = []
message_count = 4.0
messages = ['358954', '358956', '358964', '358965']
nosy_count = 4.0
nosy_names = ['ronaldoussoren', 'ned.deily', 'xtreak', 'leonardogalani']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue39149'
versions = ['Python 3.7']

@leonardogalani
Copy link
Mannequin Author

leonardogalani mannequin commented Dec 28, 2019

using Python 3.7.6 (default, Dec 27 2019, 09:51:07) @ macOs

dict = { 'a': 1, 'b': 2, 'c': 3 }

if you `if 'a' and 'b' and 'c' in dict: print('ok')` you will get a True, since everything is true.

if you `if 'a' and 'g' and 'c' in dict: print('ok')` you also get a True because the last statement is True but the mid statement is false.

To avoid this false positive, you need to be explicit:
if 'a' in dict and 'g' in dict and 'c' in dict: print('ok') you will get a false.

@leonardogalani leonardogalani mannequin added 3.7 (EOL) end of life OS-mac labels Dec 28, 2019
@tirkarthi
Copy link
Member

It's intended as non-empty strings evaluate to True so you with 'a' and 'b' and 'c' in dict you are essentially evaluating 'a' and 'b' and ('c' in dict) with brackets precedence i.e. True and True and True . On the other hand 'a' and 'g' and 'c' in dict it's the same with 'g' evaluated to True. I guess you want to check all the keys are present where all is more readable. Some more answers here: https://stackoverflow.com/questions/1285911/how-do-i-check-that-multiple-keys-are-in-a-dict-in-a-single-pass

>>> all(char in dict for char in ['a', 'b', 'c'])
True
>>> all(char in dict for char in ['a', 'b', 'g'])
False

@ned-deily
Copy link
Member

P.S. You should also read the "Operator precedence" section for expressions in the Python Language Reference manual which explains that comparison operators bind tighter than Boolean AND operators:

https://docs.python.org/3/reference/expressions.html#operator-precedence

@leonardogalani
Copy link
Mannequin Author

leonardogalani mannequin commented Dec 28, 2019

I would totally agree if it wasn't for this:

>>> 'a' and 'b' and 'g' in dict
False

The last evaluation is False, making the whole statement False.

but thanks for the documentation link and stackoverflow suggestion.
it sure does make the code more readable.

Happy new year!

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants