Skip to content
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

E1129 false positive with match #8272

Closed
dfioravanti opened this issue Feb 13, 2023 · 2 comments
Closed

E1129 false positive with match #8272

dfioravanti opened this issue Feb 13, 2023 · 2 comments
Labels
Duplicate 🐫 Duplicate of an already existing issue Match case

Comments

@dfioravanti
Copy link

dfioravanti commented Feb 13, 2023

Bug description

If one uses a match statement inside a context manager pylint complains that there is a 'NoneType' context manager. The following is the smallest code I managed to cook up that reproduce the problem

from contextlib import contextmanager
from enum import Enum, auto
from typing import ContextManager, Any


class Cases(Enum):
    CASE1 = auto()
    CASE2 = auto()


@contextmanager
def context_manager_with_if(whatever: Any, case: Cases) -> ContextManager[Any]:
    if case == Cases.CASE1:
        print(1)
        yield whatever
    elif case == Cases.CASE2:
        print(2)
        yield whatever
    else:
        raise ValueError("Wrong case")


@contextmanager
def context_manager_with_match(whatever: Any, case: Cases) -> ContextManager[Any]:
    match case:
        case Cases.CASE1:
            print(1)
            yield whatever
        case Cases.CASE2:
            print(2)
            yield whatever
        case _:
            raise ValueError("Wrong case")


if __name__ == "__main__":

    WHATEVER = 23
    with context_manager_with_if(WHATEVER, case=Cases.CASE1) as context:
        print(context)
    with context_manager_with_match(WHATEVER, case=Cases.CASE1) as context:
        print(context)

Configuration

No response

Command used

pylint tmp/example.py

Pylint output

************* Module example
tmp/example.py:41:4: E1129: Context manager 'NoneType' doesn't implement __enter__ and __exit__. (not-context-manager)

Expected behavior

To me that looks like a valid context manager. It should not raise anything

Pylint version

pylint 2.16.1
astroid 2.14.2
Python 3.11.1 (main, Dec 23 2022, 09:28:24) [Clang 14.0.0 (clang-1400.0.29.202)]

OS / Environment

MacOS 13.2 and Debian 11, it happens both inside and outside docker

Additional dependencies

No response

@dfioravanti dfioravanti added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 13, 2023
@dfioravanti dfioravanti changed the title E1129 false positive with match E1129 false positive with match Feb 13, 2023
@dfioravanti dfioravanti changed the title E1129 false positive with match E1129 false positive with match Feb 13, 2023
@jacobtylerwalls
Copy link
Member

Thanks for the report. pylint's inference engine astroid doesn't understand the returns from functions using match case. See:

>>> import astroid
>>> code = """
... def a(case):
...   match case:
...     case 1:
...       return 42
... 
... aa = a(1) #@
... """
>>> assign_node = astroid.extract_node(code)
>>> assign_name_node = assign_node.targets[0]
>>> assign_name_node.inferred()
[<Const.NoneType l.None at 0x10d477c90>]

Closing as duplicate of #5288 (and re-titling it)

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2023
@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 13, 2023
@dfioravanti
Copy link
Author

Ah interesting, good to know, thanks for the explanation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue Match case
Projects
None yet
Development

No branches or pull requests

3 participants