1

How can I modify my pylintrc so that a given decorator is interpreted as a classmethod.

pydantic defines a validator decorator to allow for attribute validation of model classes and operates as a class method. pylint throws a

E0213: Method 'has_risk_assigned' should have "self" as first argument (no-self-argument)

for a validator method declared as:


from pydantic import BaseModel, validator

class RiskyRecord(BaseModel):
    # ... attributes ...

    @validator('risk')
    def has_risk_assigned(cls, v):
          # ... make sure that risk is properly assigned ...

How can I configure my pylintrc such that it views this decorator as defining a class (instead of instance) method?

Note: I want a solution in terms of pylintrc since there are multiple classes in this module that each use multiple validators; managing this warning in one place is more desirable.

I only see two classmethod related features in my current pylintrc; both only relate to the valid name(s) for the first argument.

Dave
  • 7,555
  • 8
  • 46
  • 88
  • I think this is a case where `pylint` needs a plugin to learn that `pydantic.validator(...)` returns a class method. – chepner Dec 02 '22 at 22:03

1 Answers1

1

Chepner said it correctly, in the comments; a plugin is necessary for Pylint to understand that pydantic.validator returns a class method.

Such a plugin exists in the form of pylint-pydantic and it works to prevent the no-self-argument message from being emitted when the pydantic.validator decorator is used.

It can be added to a pylintrc like so:

load-plugins = pylint_pydantic

Alternatively, using pyproject.toml:

[tool.pylint]
load-plugins = "pylint_pydantic"

For further reading about Pylint plugins, there is a short list available available in the Pylint documentation.

Mark Byrne
  • 85
  • 1
  • 5