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 Instance of '_CountingAttr' has no 'append' member with attrs #7884

Closed
peendebak opened this issue Dec 1, 2022 · 4 comments · Fixed by pylint-dev/astroid#2059
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs astroid update Needs an astroid update (probably a release too) before being mergable Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning
Milestone

Comments

@peendebak
Copy link

peendebak commented Dec 1, 2022

Bug description

Pylint is generating warnings on dataclasses defined with attrs.
In particular on lists annotated with field, e.g. values: list[str] = field(factory=list).

With a.py equal to

# Minimal example 

from attrs import define, field

@define
class TestResult:
    """ Test class """

    name: str
    values: list[str] = field(factory=list)
    values2: list[str] = []


    def add_value(self, string : str) -> None:
        """ Ädd value """
        self.values.append(string)

    def hello(self):
        """ Print hello """
        print('hi')
        self.values2.append('hi')

Command used

pylint a.py

Pylint output

************* Module qi_analysis.experiments.x
x.py:16:8: E1101: Instance of '_CountingAttr' has no 'append' member (no-member)

------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 4.00/10, +1.00)

Expected behavior

No pylint errors

Pylint version

pylint 2.15.7
astroid 2.12.13
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)]
@peendebak peendebak added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Dec 1, 2022
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 1, 2022
@Pierre-Sassoulas Pierre-Sassoulas changed the title pylint generating warnings with attrs False positive Instance of '_CountingAttr' has no 'append' member with attrs Dec 1, 2022
@alonme
Copy link

alonme commented Mar 14, 2023

This seems to be originating from the way astroid detects if a class is decorated by attrs see.

This means that it will not detect attrs when using it as is shown in the official docs .

So - by changing your code to use attrs.define as the decorator the problem is solved

# Minimal example

import attrs
from attrs import define, field

@attrs.define
class TestResult:
    """ Test class """

    name: str
    values: list[str] = field(factory=list)
    values2: list[str] = []


    def add_value(self, string : str) -> None:
        """ Ädd value """
        self.values.append(string)

    def hello(self):
        """ Print hello """
        print('hi')
        self.values2.append('hi')

@alonme
Copy link

alonme commented Mar 15, 2023

I opened a PR with a POC to improve the way that astroid detects attrs,
However i think that if we don't get that in, we should add some pointer in the docs / warning to tell users to use @attrs.define to make it work

@alonme
Copy link

alonme commented Mar 29, 2023

@Pierre-Sassoulas Which should way should we go here?

  1. Add hints for which attrs decorators are "valid" to the docs?
  2. My Astroid PR?
  3. Add flags to enable more attrs decorators to be treated?

@Pierre-Sassoulas
Copy link
Member

I'm not super up to date on this issue, but creating/upgrading an astroid brain is almost always the easy way to fix things, so I would say your MR.

@Pierre-Sassoulas Pierre-Sassoulas added the Needs astroid update Needs an astroid update (probably a release too) before being mergable label Apr 1, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.2 milestone Apr 1, 2023
kraj pushed a commit to YoeDistro/meta-openembedded that referenced this issue Apr 10, 2023
Changelog:
=========
* Support more possible usages of "attrs" decorators.
  Closes pylint-dev/pylint#7884

Signed-off-by: Wang Mingyu <[email protected]>
Signed-off-by: Khem Raj <[email protected]>
daregit pushed a commit to daregit/yocto-combined that referenced this issue May 22, 2024
Changelog:
=========
* Support more possible usages of "attrs" decorators.
  Closes pylint-dev/pylint#7884

Signed-off-by: Wang Mingyu <[email protected]>
Signed-off-by: Khem Raj <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs astroid update Needs an astroid update (probably a release too) before being mergable Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants