-
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
RUF038
Surprising fix for Literal[True, False]
in functions with overloads
#16129
Comments
Oh i am so sorry! I didn't read the doc for rule |
RUF038
Literal[True, False]
might not always be equal to bool
RUF038
Incorrect fix for Literal[True, False]
in functions with overloads
RUF038
Incorrect fix for Literal[True, False]
in functions with overloadsRUF038
Surprising fix for Literal[True, False]
in functions with overloads
Yes, this part is covered by the section you call out. However, it does make me wonder if we should skip providing a fix alltogether if a method is marked as |
It's not totally clear to me whether mypy is correct in rejecting the overloads after they've been autofixed by Ruff here. Overloads aren't currently clearly specified in the typing spec, but there is a draft PR to provide a detailed specification for them, which I think is nearly over the line now. The rendered version of the draft spec is here, and it describes how type checkers should perform "argument type expansion" for overloads, which for Whether mypy is right or wrong, though, is perhaps irrelevant here. We perhaps shouldn't be making changes that would cause your CI to fail, and here we clearly are, since mypy now emits more errors after Ruff's autofix than it did initially. So I'd be okay with ignoring any |
According to the draft spec for overload handling, mypy is in the wrong here by failing to expand Both the error in the initial code and the new error in the fixed code are examples of the exact same mypy limitation, the problem just occurs in a new location (inside the implementation of If mypy were fixed to perform expansion of In general (even not considering overloads specifically) anywhere a type checker fails to treat The fix safety docs do already mention this exact problem in the fix safety section, and even link to relevant mypy bugs. I think we could decline to autofix this in this particular overload case, as a concession to mypy, but I'm not sure how many real-world scenarios would be helped by that. An overload like this is quite likely already causing errors under mypy, because it's almost certain you'd be calling it somewhere with a value typed as |
Yeah I also noticed that -- I agree, I'm not sure what those docs are trying to say there |
I agree with everything you said, thank you for the writeup! |
Description
I stumbled upon some code of mine where it seems to me thatmypy
andruff
do not agree whetherLiteral[True, False] == bool
and ruleRUF038
suggest a fix which introduces a mypy error.Edit: I read the docs for the rule again and this general case seems to be mentioned in the "Fix safety" already. Maybe this example is different, but if you feel like it is already covered well enough, feel free to close the issue!
Consider this code containing two
@overload
s and illustrating perhaps two different problems:And ruff suggest fixing the annotations like this:
Resulting in:
And again
mypy
on the fixed code:The text was updated successfully, but these errors were encountered: