Pydantic: Pylint cannot find fields from parent classes

Created on 4 Sep 2020  路  5Comments  路  Source: samuelcolvin/pydantic

Bug

Pylint complains about missing field if you subclass Pydantic models.

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.6.1
            pydantic compiled: True
                 install path: /Users/powerjo/.local/share/virtualenvs/pydantic-bug-sHQXBxEI/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Jul 21 2020, 10:42:08)  [Clang 11.0.0 (clang-1100.0.33.17)]
                     platform: macOS-10.14.6-x86_64-i386-64bit
     optional deps. installed: []
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=invalid-name
# pylint: disable=too-few-public-methods
# pylint: disable=trailing-newlines
from pydantic import BaseModel


class Parent:
    def __init__(self):
        self.id = 'bla'


class Child(Parent):
    ...


class PydanticParent(BaseModel):
    id: str


class PydanticChild(PydanticParent):
    ...


def test_pydantic_model():
    child = PydanticChild(id='a')
    assert child.id


def test_normal_class():
    child = Child()
    assert child.id

Run pylint --extension-pkg-whitelist=pydantic test_p.py to see the error:

************* Module test_p
test_p.py:29:11: E1101: Instance of 'PydanticChild' has no 'id' member (no-member)

------------------------------------------------------------------
Your code has been rated at 6.88/10 (previous run: 3.75/10, +3.12)
bug

Most helpful comment

Hi @mlisovyi your issue is solved by simply using v1: int = ....

It is very unfortunate that there are those stumble stones ...

Comments like this are very frustrating. Pydantic is not backed by any large company; I work on it along with others for free to help the community and for fun.

Please refrain from complaining about open source software, remember you're not a customer and I'm not paid to help you.

All 5 comments

Duplicate of #568 I think. Even if not this can't be fixed in pydantic AFAIK, I think it's a problem with pylint.

Is there a known workaround for this issue similar to the suggested @classmethod trick in the referenced issue?

It is very unfortunate that there are those stumble stones on the way to adopt _pydanctic_ as a tool. It kind of hinders the very first promise in the docs:

plays nicely with your IDE/linter/brain

Unfortunately, switching the linter from _pylint_ to anything else is in most cases very demanding and the team decision would be rather not to consider _pydantic_, which in itself is an awesome tool :(


BTW, this specific issue appears only for parameters withou default values.
This is parsed by pylint w/o errors:

from pydantic import BaseModel

class A(BaseModel):
    v1: int = 1

class B(A):
    pass

b = B(v1=2)
# this is parsed by pylint well
print(b.v1)

While this does not:

from pydantic import BaseModel

class A(BaseModel):
    v1: int

class B(A):
    pass

b = B(v1=2)
# this triggers `no-member` (E1101) in pylint
print(b.v1)

Hi @mlisovyi your issue is solved by simply using v1: int = ....

It is very unfortunate that there are those stumble stones ...

Comments like this are very frustrating. Pydantic is not backed by any large company; I work on it along with others for free to help the community and for fun.

Please refrain from complaining about open source software, remember you're not a customer and I'm not paid to help you.

Thanks for the advice on the workaround! Apologies for hurting your feelings

For the record, using v1: int = ... is causing mypy to complain. Using v1: int = Field(...) silences both mypy and pylint.

Was this page helpful?
0 / 5 - 0 ratings