pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.26k stars 1.12k forks source link

Incorrect unsupported-membership-test error on TypedDict #7978

Open tibbe opened 1 year ago

tibbe commented 1 year ago

Bug description

pylint incorrectly raises an error on the membership test below:

# pylint: disable=missing-docstring
from typing import Optional, TypedDict

class ADict(TypedDict):
    a: int

def f() -> None:
    x: Optional[ADict] = None
    while x is None or 'a' in x:
        print('test')

Configuration

[MASTER]
# https://github.com/samuelcolvin/pydantic/issues/1961
extension-pkg-whitelist=pydantic

[FORMAT]
# Maximum number of characters on a single line.
max-line-length=80

# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$

[MESSAGE CONTROL]
disable=duplicate-code,
        global-statement,
        invalid-name,  # We sometimes need to match boto3 API casing.
        missing-function-docstring,
        too-few-public-methods,
        too-many-arguments,
        too-many-branches,
        too-many-locals,
        too-many-return-statements,
        too-many-statements

Command used

pylint pylint_repro.py

Pylint output

************* Module pylint_repro
pylint_repro.py:11:30: E1135: Value 'x' doesn't support membership test (unsupported-membership-test)

------------------------------------------------------------------
Your code has been rated at 2.86/10 (previous run: 2.86/10, +0.00)

Expected behavior

No error as x is provably ADict when the membership test is made and ADict is a TypedDict, which supports membership tests.

Pylint version

pylint 2.13.9
astroid 2.11.7
Python 3.9.15 (main, Oct 11 2022, 21:39:54) 
[Clang 14.0.0 (clang-1400.0.29.102)]

OS / Environment

macOS 13.1 (22C65)

Additional dependencies

No response

tibbe commented 1 year ago

Duplicate of #3045.

DanielNoord commented 1 year ago

Knowing a little about our implementation this probably won't be fixed with #3045 so I'm reopening this as an actual issue. Thanks for the report!

doctorlard commented 1 month ago

I'm also getting this with Exception messages, e.g.

try:
    from somewhere import things
    # ... do stuff with things
except ImportError as e:
    if "No module named 'somewhere'" in e.msg:
        print("Tell user to create 'somewhere' from the template")