You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
error: Right operand of "or" is never evaluated - although it is evaluated at some point #14120
Mypy is giving out error: Right operand of "or" is never evaluated on a statement that is evaluated. It is part of a for loop and it is not evaluated on the first pass of the loop, however it is evaluated on each consecutive pass of the for loop.
I understand why the right side is not evaluated on the first pass, but I feel that it should not error out on this code as it is evaluated at some point. Or at least the error message should be different.
changed the title [-]error: Right operand of "or" is never evaluated - although it is[/-][+]error: Right operand of "or" is never evaluated - although it is evaluated at some point[/+]on Nov 17, 2022
because otherwise mypy thinks the variable has the type None. But it's still weird that mypy didn't complain about the assignment within the loop if it thought that the type is None...
It seems like MyPy just doesn't want to warn about the inferred type being None at first, and then silently changing it to Union[Role, None] after the assignment, no matter which options I try to use on the playground. There should be at least a possibility of generating a "need type annotation" warning on the untyped local variable. From the documentation, it seems like --local-partial-types should cause a warning like this to be generated, but it doesn't do so in this case, for some reason.
@pcustic As a general tip for the future, if you want to avoid getting "weird" errors like this, always check if the variable involved in the error you're getting is properly typed.
Activity
[-]error: Right operand of "or" is never evaluated - although it is[/-][+]error: Right operand of "or" is never evaluated - although it is evaluated at some point[/+]tmke8 commentedon Nov 17, 2022
It works if you define the variable like this:
because otherwise mypy thinks the variable has the type
None. But it's still weird that mypy didn't complain about the assignment within the loop if it thought that the type isNone...pcustic commentedon Nov 17, 2022
Yes, you are correct, that works, thanks! I was sure I tried that but.. 🤔
Yeah, I would also say it should complain about that.
DevilXD commentedon Nov 24, 2022
It seems like MyPy just doesn't want to warn about the inferred type being
Noneat first, and then silently changing it toUnion[Role, None]after the assignment, no matter which options I try to use on the playground. There should be at least a possibility of generating a "need type annotation" warning on the untyped local variable. From the documentation, it seems like--local-partial-typesshould cause a warning like this to be generated, but it doesn't do so in this case, for some reason.@pcustic As a general tip for the future, if you want to avoid getting "weird" errors like this, always check if the variable involved in the error you're getting is properly typed.
A5rocks commentedon Jan 22, 2025
Fixed by #18433