-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Improve message for 'true-false-comparison' (E712) #8164
Comments
This feels a little verbose for that message and I feel like many users will not understand the difference between the singleton value and truthiness without reading documentation. Ideally the documentation covers these cases with additional detail? |
I do agree that users should read the documentation, which does cover these cases in detail. However, if this error occurs, it's most likely that the user who wrote it didn't really read the documentation (because if they would, why would they use comparison to True/False with Regarding verbosity, users who don't understand this difference (and haven't read the documentation) might make the assumption that one or the other makes no difference in code and is merely of stylish nature.
Most clear difference can be seen with comparison to False, if Besides, PEP8 labels it as worse than using Here's a similar discussion: pylint-dev/pylint#2527 |
I completely agree with this issue and came here to file a similar one. Here's a long discussion about this exact topic. Generally, |
I followed the thread you linked, and I must admit that I don't understand, in the cases where you want to check in an object is the singleton |
I think you have to look at the context in which this is usually done. It might be something more like this:
Maybe not the prettiest example (should probably take things common here), but the general idea is that switching on types is a totally separate thing from switching on values. Usually you switch on type because a parameter is a union of types each of which has different behavior. The switch then chooses between the behaviors. After you switch on types, then you can separately switch on values. Separating these things conceptually makes code easier to understand. Usually when people try to do these things together, they either only intended to switch on values (so they should use truthiness or a comparison for non-Boolean values), or their code is (in my opinion) unnecessarily complicated. Also, it can be hard for type checkers to deduce types when you use
Switching on the type first makes it obvious to the reader and the type checker. |
Thanks! Whilst I better understand the motivation and utility of the pattern now, I think it might be too opinionated to affect a recommendation from a default rule. I prefer Regarding the message, might |
Ah, I see type checkers have advanced. I still think that the general pattern of switching on types should be separated from switching on values. When you do |
An
(Interestingly, when I also find it much less clear; this is my first time seeing that pattern. I do like your general idea of separating type and value comparisons though.
I like this more than the first suggested message because it does not rely on the user understanding singletons. Perhaps: To check for truthiness, use I also think it would be entirely reasonable to drop the section about checking for identity entirely from the rule message. I agree that checking for identity is rare and if you're using it you should know what you're doing. I don't see a strong justification for suggesting it in every violation. In that case, perhaps: Instead of comparing directly to |
Yup! Thought I am running using a development built of mypy, so it might not work in the latest official release.
There are a few occasions where a value being |
Thanks for this (: I agree that the first message I recommended would be somewhat confusing or complex for most. This message is much easier to read and is in accordance to PEP8. I also think it's reasonable to omit the message for checking identity, as it is usually really never the case that someone wants this behaviour. |
Pull request welcome here :) |
Using Ruff version
0.1.1
, for code like this:if a == True
ruff returns the following error message:E712 Comparison to 'True' should be 'cond is True' or 'if cond'
On the page describing the error, E712, it states that in PEP8 it's recommended to compare using
if cond is True
.While that applies for singletons such as None, later in the PEP8, it's stated that for True/False the comparison should be like this:
![image](https://private-user-images.githubusercontent.com/120791574/277653059-3afe0f2b-c65d-4644-abba-11228cad874c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk3MjE4OTksIm5iZiI6MTczOTcyMTU5OSwicGF0aCI6Ii8xMjA3OTE1NzQvMjc3NjUzMDU5LTNhZmUwZjJiLWM2NWQtNDY0NC1hYmJhLTExMjI4Y2FkODc0Yy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjE2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxNlQxNTU5NTlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mMjBkOGQ0OWJjMjU3ZDgwYTU1YmY4YWYyNmE3NTA4YmIwNTdhNjdlNGJiN2E3Yjk2NjE3YmU1Njk2MzYyYTEyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.pQ_8TAwRUYAGzZD3zyOEH-eYBfEFNFN76gOOnV-U2DE)
Using Pylint, the error message looks like this and is more clear on what's meant:
Comparison 'a ==True' should be 'a is True' if checking for the singleton value True, or 'a' if testing for truthiness.
The text was updated successfully, but these errors were encountered: