Skip to content

in operator does not check type of element #4645

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

Closed
Wilfred opened this issue Feb 27, 2018 · 2 comments
Closed

in operator does not check type of element #4645

Wilfred opened this issue Feb 27, 2018 · 2 comments

Comments

@Wilfred
Copy link

Wilfred commented Feb 27, 2018

Given the following code:

from typing import List

def contains_foo(list_of_ints):
    # type: (List[int]) -> bool
    return 'foo' in list_of_ints

mypy --strict --py2 example.py does not complain that I'm looking for strings in a list of ints.

I don't think there are duplicate issues, but in is a tricky thing to search for. This comment in #4071 is possibly relevant, talking about narrowing.

@gvanrossum
Copy link
Member

The problem is that quite often people use this behavior of in (and ==) in working code, depending on the fact that e.g. None or some other value is not in a given list (or not equal to a given value). So "fixing" this would cause a lot of pain for people who have painstakingly annotated legacy codebases. (Like my employer.)

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 27, 2018

This is a duplicate of #1749.

@JukkaL JukkaL closed this as completed Feb 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants