-
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
PLC1901 false positives/incorrect response #3503
Comments
I think this is fixed by #3497. |
Only the second part. Linked PR does not touch the case where there is information about the variable type and the type is Union. |
I'm seeing the not:
That's the backward "not", but also, the original was: def ...(...) -> bool:
return name in self.env and self.env[name] != "" It's not valid to simply that without the incorrect I don't think you need variable type info; pylint doesn't do full type checking, you just can only simplify in cases were it's known to be a bool, via |
FWIW, Pylint treats these identically. Every example shared here is flagged by Pylint: def bug(query: str | None):
if query == "":
...
else:
...
def f() -> bool:
return name in self.env and self.env[name] != "" Yields: foo.py:1:0: C0114: Missing module docstring (missing-module-docstring)
foo.py:1:0: C0104: Disallowed name "foo" (disallowed-name)
foo.py:1:0: C0116: Missing function or method docstring (missing-function-docstring)
foo.py:2:7: C1901: Avoid comparisons to empty string (compare-to-empty-string)
foo.py:7:0: C0116: Missing function or method docstring (missing-function-docstring)
foo.py:7:0: C0103: Function name "f" doesn't conform to snake_case naming style (invalid-name)
foo.py:8:11: E0602: Undefined variable 'name' (undefined-variable)
foo.py:8:19: E0602: Undefined variable 'self' (undefined-variable)
foo.py:8:32: C1901: Avoid comparisons to empty string (compare-to-empty-string)
foo.py:8:32: E0602: Undefined variable 'self' (undefined-variable)
foo.py:8:41: E0602: Undefined variable 'name' (undefined-variable) |
PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey See: astral-sh/ruff#3503
PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey See: astral-sh/ruff#3503
PLC1901 `val != ""` can be simplified to `not val` as an empty string is falsey PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey The rule is broken and suggests incorrect fixes, see: astral-sh/ruff#3503 However, while the suggested fixes are incorrect, the intent is correct.
PLC1901 `val != ""` can be simplified to `not val` as an empty string is falsey PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey The rule is broken and suggests incorrect fixes, see: astral-sh/ruff#3503 However, while the suggested fixes are incorrect, the intent is correct.
PLC1901 `val != ""` can be simplified to `not val` as an empty string is falsey PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey The rule is broken and suggests incorrect fixes, see: astral-sh/ruff#3503 However, while the suggested fixes are incorrect, the intent is correct.
PLC1901 `val != ""` can be simplified to `not val` as an empty string is falsey PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey The rule is broken and suggests incorrect fixes, see: astral-sh/ruff#3503 However, while the suggested fixes are incorrect, the intent is correct.
In |
PLC1901 `val != ""` can be simplified to `not val` as an empty string is falsey PLC1901 `stderr == ""` can be simplified to `stderr` as an empty string is falsey PLC1901 `stdout == ""` can be simplified to `stdout` as an empty string is falsey The rule is broken and suggests incorrect fixes, see: astral-sh/ruff#3503 However, while the suggested fixes are incorrect, the intent is correct.
Yes, but most of the time, you either have something that's always a string, or you have an interface that can produce some of those other values, and you should also check to make sure it's a string. For example, There are certain types of tests where this can't be simplified (like testing custom Above, I was only worried about the case where the value is not in a place where the bool is directly evaluated and can leak the value (and/or is itself not a boolean context unless inside |
Ahh, it's working now (0.256!). Pylint and Ruff are both happy. I think the Edit, wait a minute, forgot I rewrote it... Nope, restoring the original, I still get:
The message is correct, though, so that's great. I'm not sure how to get pylint to flag this error. |
Ahh, empty string comparisons is an optional checker in pylint. Okay, I'm happy, I'd want Ruff'd Pylint checker to behave like pylint. Though if it was a hair smarter and could tell boolean context (assert, if, bool()) from places that could see the result, that'd be an improvement, but I think that could be proposed to pylint first. :) |
If I go to https://play.ruff.rs and in
Is there a syntax you would recommend or just |
You can use |
Just a bit of cleanup. See astral-sh/ruff#3503. Signed-off-by: Henry Schreiner <[email protected]>
Ah yeah, sorry, I should've mentioned that this is behind a Pylint extension. I'm honestly a little tempted to remove this rule given the number of false positives 🙈 But I try to defer to upstream plugins, and so look to Pylint as a guide, and... it has the same behavior. |
code:
this should not be flagged as
""
is different from None.Additionally, ruff provides the following error:
This is inaccurate, it should be simplified to
not query
(if it wasn't possible for query to be None).possible related issue but not the same: GH-3494
The text was updated successfully, but these errors were encountered: