Skip to content

mypy doesn't type-check membership equality, for example: 'foo' in bar_list  #1749

Closed
@cpdean

Description

@cpdean

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:

from typing import List


def is_it_in(things):
    # type: (List[int]) -> bool
    if "foo" in things:   # this will not fail, but it should
        return True
    else:
        return False


def add_a_thing(things):
    # type: (List[int]) -> None
    things.append("foo")  # this will fail expectedly

t = [1, 2, 3]
add_a_thing(t)
assert is_it_in(t)

Running it gives the following on master, mypy-lang-0.4.3.dev0:

mypy --py2 test.py
test.py: note: In function "add_a_thing":
test.py:14: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"

I know that there isn't anything wrong with going "foo" in things since the list could have anything in it, but mypy yells at append("foo"), so if adding things to a list are checked, maybe inspecting that list could be type-checked too?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions