-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
--strict-equality should be more flexible #6607
Comments
I think that splitting the option may be a reasonable long-term goal, but I think that it would be too much complexity right now. In my opinion the main benefits for the feature are a) helping with Python 2 to 3 migration and b) catching some silly mistakes such as comparing against a function object (forgetting to call). For these goals, minimizing the false positive rate seems like the main concern, as long as it can still flag things like Here are more concrete ideas:
I'd love if we could get the false positive rate low enough to enable this by default. If we can pull that off, the usefulness of the feature would increase dramatically. |
Here is a possible short term plan we discussed with @JukkaL elsewhere:
This will trade some "false positives" for "false negatives", but it is probably OK for now. |
Good plan. |
Another thing that generates false positives is comparisons between two non-builtin classes. These can be valid due to multiple inheritance (technically there could be multiple inheritance from built-in classes, but that seems rare enough that we can ignore it). Example: class A: pass
class B: pass
class C(A, B): pass
def f(a: A, b: B) -> bool:
return a == b # Should be okay?
c = C()
print(f(c, c)) This causes two false positives in the mypy codebase, so it's not a theoretical concern. This probably mostly happens with ABC-like classes, but some of these might not be tagged as ABCs, so we may have to assume any user-defined class could be used like an ABC. |
These two places in mypy code are actually questionable IMO (because of #5159). I remember I spent some time understanding what is going there while working on Also there are several other places where mypy thinks that multiple inheritance doesn't exist, for example |
Yeah, the current behavior is pretty reasonable, unless it seems to generate many false positives. The mypy codebase isn't big enough to estimate the false positive rate anyway. |
I get |
We can potentially also special-case this, but I think this is lower priority than other cases listed above. |
per Dropbox Slack, adding this case regarding typing_extensions.Literal: |
Currently,
--strict-equality
is all or nothing. People typically want it for particular situations likestr
vsbytes
for Python 2 to 3 migration,int
vsfloat
for numeric code, etc. It looks like there are at least two options:--disallow-any-xxx
flags.--always-true
/--always-false
.I personally prefer the second option, since it is more flexible. Plus I could imagine a use case with user defined types: one might want to disable comparisons after migration from a legacy API.
The text was updated successfully, but these errors were encountered: