-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add flag to prohibit equality checks between non-overlapping checks #6370
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
Conversation
In some sense that is a good thing, right, since we are doing a bad thing in those places? On the other hand, there are a lot of them, so... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really great! There are a couple questions worth thinking about though:
- I think we probably do want to allow promotions, since disallowing int/float comparisons seems a little aggressive.
- This disallows things like
assert foo is not None
which, while nominally ruled out by the type system, might be worth cutting some slack to. I dunno if there is a reasonable way to suppress it in asserts? Maybe the right thing is to allow spurious none checks, though that's kind of a bummer.
OK, also as discussed of tracker, let's start with allowing this asserts (otherwise even mypy codebase wouldn't be clean with this flag). |
@msullivan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One request for some restructuring in the new Type code, but otherwise this is great
@ilevkivskyi I think that |
This request came many times from internal users, since such comparisons caused actual troubles. One of the problems is that
As someone who used floats a lot, I think comparing floats with As a bottom line, I still think prohibiting promotions with this flag is a good idea. Potentially, we can add a note why this is unsafe. |
Thanks for the detailed explanation -- you convinced me. This is a great feature as it is now and will help catch many bugs. |
strict equality added in python/mypy#6370
Fixes #1271
The new per-file flag
--strict-equality
disables equality, identity, and container checks between non-overlapping types. In general the implementation is straightforward. Here are some corner cases I have found:Any
should be always safe.b'abc == 'abc'
should be an error.if x is not None: ...
, are special cased to not be errors ifx
has non-optional type.Optional[str]
andOptional[bytes]
should be considered non-overlapping for the purpose of this flag.__eq__()
(i.e. incompatible withobject.__eq__()
) I suppress the non-overlapping types error, if there is already an error originating from the method call check.Note that I updated
typing-full.pyi
so thatSequence
inherits fromContainer
(this is needed by some added tests, and also matches real stubs). This however caused necessary changes in a bunch of builtins fixtures.