pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Do not raise ``too-many-instance-attributes`` on dataclasses or with a higher threshold

Open vince6e74 opened this issue 1 year ago • 3 comments

Current problem

Dataclasses sometimes deliberately have a lot of arguments, which is also the purpose of the classes. With normal classes, methods and functions it makes sense if the argument list is not too long.

Desired solution

Therefore a conscious distinction between dataclass and normal class would be meaningful with the argument number.

Additional context

No response

vince6e74 avatar Sep 23 '23 22:09 vince6e74

@vince6e74 Could you give an example of this?

nickdrozd avatar Oct 05 '23 16:10 nickdrozd

I presume an example describing what the OP means could be:

# pylint: disable=missing-class-docstring,missing-module-docstring

from dataclasses import dataclass


@dataclass
class Foo:
    one: int
    two: int
    three: int
    four: int
    five: int
    six: int
    seven: int
    eight: int

Output:

$ > pylint test.py           
************* Module test
test.py:7:0: R0902: Too many instance attributes (8/7) (too-many-instance-attributes)

------------------------------------------------------------------
Your code has been rated at 9.00/10 (previous run: 0.00/10, +9.00)

I've encountered the same situation myself. I would have expected that dataclasses are excluded from the R0902 check. AFAIK, I do not know of a way to specifically indicate dataclasses are to be treated differently in that situation.

anton-ubi avatar Nov 15 '23 21:11 anton-ubi

…while with

class Foo(NamedTuple):
    one: int
    two: int
    three: int
    four: int
    five: int
    six: int
    seven: int
    eight: int
    nine: int
    ten: int
    eleven: int
    twelve: int
    thirteen: int
    fourteen: int
    fifteen: int

pylint is totally happy and does not complain at all.

clo-vis avatar Mar 20 '24 11:03 clo-vis