-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Strict equality not working between string and uuid #13632
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
Comments
It only works when there's no custom Any type can compare equal to anything ( However, AFAIC that method only compares equal to other UUIDs, so perhaps it should be excluded from typeshed? |
No, current typeshed consistently adds |
@sterliakov This is really unfortunate, because the code is special casing base types. That's why it works for strings and lists, as both the typings for strings and lists also have a custom declared I would argue against the decision of closing this issue, because If this is in fact is the intended behavior, could you please add a note to the documentation https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-strict-equality. |
I'm slightly against introducing more special cases for stdlib like this - mypy code is fairly non-trivial, and this issue can be solved at the stubs/typeshed level more efficiently in checker-agnostic fashion. Pyright suffers from exactly the same problem now. Perhaps you could raise this issue (slightly rephrasing to make it clear that custom |
typeshed's annotation seem correct to me: Line 61 in da50b5c
Annotating I'm against making changes to typeshed that cause the stubs to divert from the implementation just to satisfy a particular type checker's - fairly arbitrary - rules. |
I personally think that deleting |
It would make the maintenance situation worse for typeshed, as we'd have to manage another stubtest allowlist entry. It also introduces a lie, which is might have other unforseen consequences in the future – for example for non-typechecker related issues. So this is still a -1 for me. |
I would agree with this in general, but this case is less obvious. Both mypy and pyright (so an overwhelming majority, I believe, at least until red knot goes live) use the same heuristic: comparison of unrelated types emits a warning unless either side defines custom (not inherited from The benefits of such a change outweigh the inconvenience: I was also hit by |
We could change the annotations to the more correct: @overload
def __eq__(self, other: UUID) -> bool: ...
@overload
def __eq__(self, other: object) -> NotImplementedType: ... This is not only more correct, it would also allow type checkers to still implement the strict equality check – this time based on actual types, instead of faulty heuristics. I'm not sure any type checkers do this, at the moment. |
I'd be happy to see this implemented everywhere, consistently. If we apply this approach to all (as a small note, I'd prefer using |
Bug Report
--strict-equality
seems to only work in very reduced cases. In particular, it is not working when comparing strings and uuids.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&flags=show-error-codes%2Cstrict-equality&gist=6c8273316b52930967ad0f8062c57aa3
Expected Behavior
The comparison in line 10 should raise a comparison overlap error. Something like
Actual Behavior
Mypy does not flag the string/UUID comparison as incompatible
Your Environment
--strict-equality --show-error-codes
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: