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

False positive not-an-iterable in match case #5845

Closed
Pierre-Sassoulas opened this issue Feb 27, 2022 · 1 comment
Closed

False positive not-an-iterable in match case #5845

Pierre-Sassoulas opened this issue Feb 27, 2022 · 1 comment
Labels
Duplicate 🐫 Duplicate of an already existing issue False Positive 🦟 A message is emitted but nothing is wrong with the code Match case python 3.10

Comments

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Feb 27, 2022

Bug description

I have 2 functions that do the same thing: recursively iterate an object and return all the string values. I would prefer to use iter2, with the match case, as it is easier on the eye. They both run correctly but pylint reports errors in the second function.

def iter1(object_):
    if isinstance(object_, str):
        yield object_
    if isinstance(object_, list):
        for item in object_:
            yield from iter1(item)
    elif isinstance(object_, dict):
        for value in object_.values():
            yield from iter1(value)
    return

def iter2(object_):
    match object_:
        case str():
            yield object_
        case list():
            for item in object_:
                yield from iter2(item)
        case dict():
            for value in object_.values():
                yield from iter2(value)
        case _:
            return

aaa = {"a": "abc", "b": "123", "c": ["def", "ghi", "jkl"], "d": {"e": "mno", "f": ["pqr", "stu"]}}
print(list(iter1(aaa)))
print(list(iter2(aaa)))

See original question on stackoverflow

Command used

pylint a.py

Pylint output

pylint reports these errors in the function iter2.

Non-iterable value iter2(item) is used in an iterating context pylint (not-an-iterable)

Expected behavior

No error.

Pylint version

pylint 2.12.2
astroid 2.9.3
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)]
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.10 labels Feb 27, 2022
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jul 6, 2022
Manitary added a commit to Manitary/advent-of-code that referenced this issue Dec 10, 2022
@jacobtylerwalls
Copy link
Member

Duplicate of #5288

@jacobtylerwalls jacobtylerwalls marked this as a duplicate of #5288 Feb 13, 2023
@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 PR This issue is accepted, sufficiently specified and now needs an implementation labels Feb 13, 2023
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 False Positive 🦟 A message is emitted but nothing is wrong with the code Match case python 3.10
Projects
None yet
Development

No branches or pull requests

3 participants