pylint
pylint copied to clipboard
`no-self-argument` on Pydantic validator (decorator creates `classmethod`)
Bug description
from pydantic import validator, BaseModel
class Test(BaseModel):
a_field: int
@validator
def some_validator(cls, value):
return value
This produces the error below, although pydantic's validator
does indeed output a classmethod, and as such cls
makes sense. See also this issue on Pydantic where it's ascertained that the issue lies ultimately with the linter.
The workarounds:
- Using
@classmethod
after@validator
, though this doesn't seem to work for everyone, it produces warnings in IDEs such as pycharm, and is risky in general. - Slapping
# pylint: disable=no-self-argument
on every line where a validator is used, but this is very unwieldy.
Since the code works and is correct, the linter should not complain about it.
Unfortunately this seems a rather complex issue for me to investigate since I never contributed to pylint before and don't have the free time to start now. Hopefully someone's willing to look into it.
Configuration
No response
Command used
pylint src\temp.py
Pylint output
************* Module temp
src\temp.py:8:4: E0213: Method should have "self" as first argument (no-self-argument)
Expected behavior
No error, this is a known, if a bit unusual, pattern already recognised by editors such as PyCharm.
Pylint version
pylint 2.14.1
astroid 2.11.5
Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
OS / Environment
Windows 11
Additional dependencies
pydantic==1.9.0
+1
Reproduced in version pydantic==1.9.1, pylint==2.4.2
add version info pydantic==1.9.0 pylint 2.14.4 astroid 2.11.6 Python 3.9.10 (main, Feb 22 2022, 13:54:07) [GCC 11.2.0]
Hi! I notice that there exists a plugin for Pylint called pylint-pydantic
github PyPI
The error reported in this issue is supressed by pylint-pydantic:
pip install pylint-pydantic
pylint --load-plugins pylint_pydantic --extension-pkg-whitelist='pydantic' example.py
Would it be acceptable to defer to pylint-pydantic for this library-specific case, what do you think?
That is not a bad idea and thank you for finding it. I would still at least mention it in the documentation somewhere to make it more visibile (pydantic for example does the same to highlight plugins that silence false-positives related to pydantic code).
Could you confirm pylint-pydantic
work for you @berzi ? Letting an external plugin handle that would be great but it has very few stars and adding it to our doc would drive a lot of traffic to it, we should make sure it's ready for it.
Sure. I'll make you know on Monday.
I can confirm that installing pylint-pydantic
and enabling it:
# pyproject.toml
[tool.pylint.MASTER]
load-plugins = "pylint_pydantic"
Makes the error go away. :)
On Fri, 16 Sep 2022, 22:18 Pierre Sassoulas @.***> wrote:
Could you confirm pylint-pydantic work for you @berzi https://github.com/berzi ? Letting an external plugin handle that would be great but it has very few stars and adding it to our doc would drive a lot of traffic to it, we should make sure it's ready for it.
— Reply to this email directly, view it on GitHub https://github.com/PyCQA/pylint/issues/6900#issuecomment-1249775859, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHY3U46TML3PDTYXVTTOZCLV6TI2FANCNFSM5YJT5ATA . You are receiving this because you were mentioned.Message ID: @.***>
Thank you ! Let's close this issue once pylint-pydantic is documented on our side. We should also talk about other plugins at least about pylint-django at the same time.