We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Bug Report
The redundant-expr gives a false positive if you conditionally read a variable for another conditional inside a loop.
redundant-expr
To Reproduce
def mypytest() -> int | None: trigger_segment = None violations = None for segment in [1, 2]: if segment == 2: violations = trigger_segment if trigger_segment else segment trigger_segment = segment return violations
The rule will trigger with error: If condition is always false [redundant-expr] on the if trigger_segment part of the ternary.
error: If condition is always false [redundant-expr]
if trigger_segment
Expected Behavior
Mypy should not report anything because clearly the if condition is not always false: running print(mypytest()) will print 1.
print(mypytest())
1
Actual Behavior
Your Environment
My configuration:
[tool.mypy] plugins = [ "pydantic.mypy" ] files = "src/**/*.py,tests/**/*.py" strict = true # These are additional checks not covered by strict: disallow_any_unimported = true enable_error_code = """\ ignore-without-code,\ truthy-bool,\ truthy-iterable,\ redundant-expr,\ redundant-self,\ unused-awaitable\ """ warn_unreachable = true
mypy 1.1.1 (compiled: yes) % python --version Python 3.11.1
The text was updated successfully, but these errors were encountered:
I suppose that trigger_segment has inferred type None. You could try the following workaround:
trigger_segment
None
trigger_segment: int | None = None
Sorry, something went wrong.
Fixed by #18433
No branches or pull requests
Bug Report
The
redundant-expr
gives a false positive if you conditionally read a variable for another conditional inside a loop.To Reproduce
The rule will trigger with
error: If condition is always false [redundant-expr]
on theif trigger_segment
part of the ternary.Expected Behavior
Mypy should not report anything because clearly the if condition is not always false: running
print(mypytest())
will print1
.Actual Behavior
The rule will trigger with
error: If condition is always false [redundant-expr]
on theif trigger_segment
part of the ternary.Your Environment
My configuration:
The text was updated successfully, but these errors were encountered: