-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy doesn't type-check membership equality, for example: 'foo' in bar_list
#1749
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's not symmetric though. You could have a value whose type is a supertype of the list item type (or a union that includes the list item type), and while it's a definite violation to append that to the list, it's not necessarily wrong to check whether that's in the list. But if mypy believes that the membership test will always return False it would be okay to report that IMO. I think we may already have an issue about this? |
Yeah, something similar was discussed a while ago. Note that there's no syntax in PEP 484 that lets us specify the desired behavior for |
Yep, it was discussed in #1472, though that discussion went in other directions as well. I've had two confusing debugging experiences while working on mypy itself which would have been caught immediately by such a check, so I'm personally quite in favor of seeing something done here. |
I guess what I'm saying is that I don't like the idea of requiring |
I can see how the membership check doesn't necessarily violate the List[int] annotation, since you can still ask if anything is in a list, but how strict or non strict do we want to be here? I can see the value in enforcing it on the append method but I'm struggling to come up with a use case when we wouldn't want to enforce it for checking for an element in a list. if a list sometimes could have a different type then they could just annotate it with a Union, right? |
So if I have But here's a case using an overlap:
I don't think the check in foo() should trigger an error; having to make it "type-safe" by writing
doesn't help anybody. |
That's a good point, I agree with you. Sorry, went right for the code example and skipped over what you had said @gvanrossum . |
There's a similar issue with equality. Maybe mypy should warn about |
We now have a flag |
I'm not sure if this is a bug or a feature request, but given mypy will enforce types on the
append
operator, it feels incomplete it doesn't also support checking if an element is present in a list of a different type.For example:
Running it gives the following on master,
mypy-lang-0.4.3.dev0
:I know that there isn't anything wrong with going
"foo" in things
since the list could have anything in it, but mypy yells atappend("foo")
, so if adding things to a list are checked, maybe inspecting that list could be type-checked too?The text was updated successfully, but these errors were encountered: