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 with operator+ and lists (no-member) #4826

Closed
hemberger opened this issue Aug 10, 2021 · 2 comments · Fixed by #5696
Closed

False positive with operator+ and lists (no-member) #4826

hemberger opened this issue Aug 10, 2021 · 2 comments · Fixed by #5696
Assignees
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Minor 💅 Polishing pylint is always nice Needs astroid update Needs an astroid update (probably a release too) before being mergable

Comments

@hemberger
Copy link
Contributor

hemberger commented Aug 10, 2021

Bug description

When using operator+ to merge two lists (in the context of a class method?), pylint incorrectly thinks that the objects in the combined list are also of type list.

The simplest example I can construct is the following:

# pylint: disable=C0103,C0114,C0115,C0116,R0903

class A:
    def __init__(self):
        self.Alist = []
        self.data = []

    def run(self):
        for a in [self] + self.Alist:
            for _ in a.data:  # # <-- causes "no-member" warning
                pass

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:12:21: E1101: Instance of 'list' has no 'data' member (no-member)

Expected behavior

I would expect there to be no warning, since both self and self.Alist are (nominally) of type A, which does have a member called data.

If instead of using [self] + self.Alist, any of the following will not result in a warning:

  • for a in [self]
  • for a in self.Alist

Pylint version

pylint 2.9.6
astroid 2.6.5
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0]

OS / Environment

CentOS 8.4

Additional dependencies

No response

@hemberger hemberger added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 10, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening this issue. I'm classifying as minor since

class A:
    def __init__(self):
        self.Alist = [A()]
        self.data = []

    def run(self):
        for a in [self] + self.Alist:
            for _ in a.data:  # # <-- causes "no-member" warning
                pass

A().run()

Gives no pylint warning and putting a string instead of A() gives a proper pylint warning.

@Pierre-Sassoulas Pierre-Sassoulas added Minor 💅 Polishing pylint is always nice and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 12, 2021
@DanielNoord
Copy link
Collaborator

This is probably an issue in astroid.
I wrote the following code to show different behaviour in similar code:

import astroid

module = astroid.parse(
    """
for a in [1] + [2]:
    print(a)
"""
)

print(list(module.body[0].body[0].value.args[0].infer()))

module = astroid.parse(
    """
class A:
    def __init__(self):
        for a in [self] + []:
            print(a)
"""
)

print(list(module.body[0].body[0].body[0].body[0].value.args[0].infer()))

module = astroid.parse(
    """
class A:
    def __init__(self):
        for a in [self, 1]:
            print(a)
"""
)

print(list(module.body[0].body[0].body[0].body[0].value.args[0].infer()))
python3 test.py
[<Const.int l.2 at 0x1075c9060>, <Const.int l.2 at 0x1075c90f0>]
[<Instance of builtins.list at 0x4418479552>]
[<Instance of .A at 0x4418481136>, <Const.int l.4 at 0x1075c8400>]

With for a in [self] + [] the inference seems to stop at recognising that it is a list and is unable to go any further.
I haven't looked at any astroid internals yet, but this should be a good start to find where this needs to be fixed or handled better.

DanielNoord added a commit to DanielNoord/pylint that referenced this issue Jan 18, 2022
@DanielNoord DanielNoord mentioned this issue Jan 18, 2022
4 tasks
@DanielNoord DanielNoord self-assigned this Jan 18, 2022
@DanielNoord DanielNoord added Needs astroid update Needs an astroid update (probably a release too) before being mergable False Positive 🦟 A message is emitted but nothing is wrong with the code labels Jan 18, 2022
DanielNoord added a commit that referenced this issue Mar 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Minor 💅 Polishing pylint is always nice Needs astroid update Needs an astroid update (probably a release too) before being mergable
Projects
None yet
3 participants