Pylint: False positive: Field has no items() member

Created on 20 Nov 2018  路  4Comments  路  Source: PyCQA/pylint

Steps to reproduce

from typing import Dict
from dataclasses import dataclass, field

@dataclass
class TestClass:
    foo: str
    bar: str
    baz_dict: Dict[str, str] = field(default_factory=dict)

    def some_func(self) -> None:
        for key, value in self.baz_dict.items():
            print(key)
            print(value)

Current behavior

Pylint reports:
"Instance of 'Field' has no 'items' member"

Expected behavior

No error, because the field is a Dict and thus does have the items() member.
(Pylint thinks it's just a Field, but it's the default factory returns a dict)

pylint --version output

pylint 2.1.1
astroid 2.0.4
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]

(I also tested with "pip install pylint astroid --pre -U", which resulted in the same thing)

astroid brain bug

Most helpful comment

Note: of course same behavior with keys and values :)

All 4 comments

Thanks for creating an issue @kazaamjt Pylint currently does not fully understand dataclasses, we'll probably need inference support in inferring field as the type of the value passed via default_factory

Note: of course same behavior with keys and values :)

While dataclasses are not supported, is there a way to disable certain checks on dataclasses only, using pylintrc?

You can try --ignored-classes to see if it helps.

Was this page helpful?
0 / 5 - 0 ratings