-
-
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
'in' operator rejected with union right operand #4954
Comments
I think I've run into a similar problem, but given the error message, it's hard to tell. Here's the code sample:
the error I get (with mypy 0.620) is:
My guess is that the conditional makes mypy think that |
There must be something else, I just tried a complete version of your sample: from typing import List
class Person:
dn: List[str]
def exclude_disabled(employees: List[Person]) -> List[Person]:
"""Excludes disabled employees."""
return [employee for employee in employees if 'OU=zDisabled' not in employee.dn] and it type-checks cleanly on master. What is the type of |
Hi,
AFAICT this is a bug.
Here it is a minimal code snippets: from typing import Collection, Union
def function(a: Union[bool, Collection[str]], b: str) -> int:
if a is True:
return 1
if a is False:
return 2
if b in a:
return 3
return 4
This is a perfectly valid Python code, so mypy shouldn't complain.
Haven't tried
None Hope you can help. |
@ariciputi Your issue is different. Mypy doesn't understand that the |
@JukkaL : This is an interesting case. Did some digging. This works:
As in your example, adding the It seems like it has to do with the fact that the If we look in But, of course, if you define some dumb iterable, you can use
Simply adding a
I hesitate to make this change -- it seems like messing fundamentally with the protocol. I guess I'll push up a PR on Any direction would be helpful, I've just started trying to understand this codebase. |
Yeah, changing Would certainly appreciate guidance from someone more experienced with this code. |
I think this is also a case of this error (?), but is really odd because there's no need to upcast the value entries of the dict to
Results in
If a set literal rather than The workaround is to add an explicit type definition for |
@MrCreosote that's a different problem, basically that mypy infers |
This still reproduces. The weird part here to me isn't that we reject |
The |
I know that; what's weird to me is that mypy knows about that too, but only if the Iterable is not in a Union. (For completeness, |
Mypy generates a false positive for this fragment:
This should be easy to fix.
The text was updated successfully, but these errors were encountered: