Skip to content

Mypy allows calling in keyword on Iterable #16532

@tgavoille

Description

@tgavoille

Bug Report

Mypy doesn't raise an error when checking if an element is in an Iterable while it does when directly using __contains__.

To Reproduce

from typing import Any, Iterable

def iterable_contains(iterable: Iterable[Any], element: Any):
    if element in iterable:  # No error but I thingk it should raise
        print(element)
    if iterable.__contains__(element):  # Error, expected
        print(element)

Expected Behavior

Mypy should raise an error on the first if statement

Actual Behavior

But it doesn't

Your Environment

Found this with earlier python and mypy versions but example is reproducible with :

  • Mypy version used: 1.7.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions