Skip to content

False positive on arguments of constructor created by "attrs" #3492

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

Open
mthuurne opened this issue Apr 20, 2020 · 2 comments
Open

False positive on arguments of constructor created by "attrs" #3492

mthuurne opened this issue Apr 20, 2020 · 2 comments
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)

Comments

@mthuurne
Copy link
Contributor

Steps to reproduce

import attr

class Base:
    def __init__(self):
        pass

@attr.s(auto_attribs=True)
class Sub(Base):
    value: int

print(Sub(123))

Current behavior

PyLint reports about the last line:

E1121: Too many positional arguments for constructor call (too-many-function-args)

Expected behavior

No error should be printed, as the generated constructor takes two arguments (self and value).

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.8.2 (default, Mar 05 2020, 18:58:42) [GCC]

Maybe related

Other issues about support for the attrs library are #1698 and #2983. Superficially this issue doesn't seem to be the same thing, but perhaps there is overlap when digging deeper.

@PCManticore PCManticore added Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Bug 🪲 labels Apr 23, 2020
@zhiltsov-max
Copy link

Another way to reproduce and an option to mitigate:

from attrs import define, field

@define(auto_exc=False, init=False)
class BaseError(Exception):
    v: int = field(default=0, kw_only=True)

    def __init__(self, msg=None, *, v=None):
        super().__init__(msg)
        self.__attrs_init__(v=v or 0)

# Pylint will raise false positive warnings for derived classes,
# when __init__ is defined directly. Monkey-patching resolves the problem:
# setattr(BaseError, '__init__', BaseError._my__init__)

@define(auto_exc=False)
class Error1(BaseError):
    x: int
    y: int

print(Error1(1, 2)) # << here

pylint pylint_example.py
************* Module pylint_example
pylint_example.py:20:6: E1121: Too many positional arguments for constructor call (too-many-function-args)

Attrs 21.4.0, pylint 2.10.2. But it happened in earlier versions too.

@Pierre-Sassoulas
Copy link
Member

Possibly related to pylint-dev/astroid#1330 in astroid 2.10

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 Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)
Projects
None yet
Development

No branches or pull requests

6 participants