-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrongpendingIssues that may be closedIssues that may be closed
Description
Bug Report
A Container[str] is expected to accept any object, even though it is generic over str:
from collections.abc import Container
class StringContainer:
def __contains__(self, item: str) -> bool:
return True
container: Container[str] = StringContainer()$ mypy t.py
t.py:9: error: Incompatible types in assignment (expression has type "StringContainer", variable has type "Container[str]") [assignment]
t.py:9: note: Following member(s) of "StringContainer" have conflicts:
t.py:9: note: Expected:
t.py:9: note: def __contains__(self, object, /) -> bool
t.py:9: note: Got:
t.py:9: note: def __contains__(self, str, /) -> bool
Found 1 error in 1 file (checked 1 source file)This contradicts the error mypy gives when trying to use a Container[str] with any type other than str, if strict mode is enabled:
from collections.abc import Container
container: Container[str] = ()
if 42 in container:
pass$ mypy --strict r.py
r.py:6: error: Non-overlapping container check (element type: "int", container item type: "str") [comparison-overlap]
Found 1 error in 1 file (checked 1 source file)To Reproduce
See above.
Expected Behavior
I expect to be allowed to define my __contains__ method with the correct argument type.
Actual Behavior
See above.
Your Environment
- Mypy version used: mypy 1.19.1 (compiled: yes)
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: Python 3.14.2 (main, Dec 5 2025, 16:49:16) [Clang 17.0.0 (clang-1700.4.4.1)]
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongpendingIssues that may be closedIssues that may be closed