Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive: Field has no items() member #2600

Closed
kazaamjt opened this issue Nov 20, 2018 · 6 comments · Fixed by pylint-dev/astroid#1126
Closed

False positive: Field has no items() member #2600

kazaamjt opened this issue Nov 20, 2018 · 6 comments · Fixed by pylint-dev/astroid#1126
Labels
Bug 🪲 Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)

Comments

@kazaamjt
Copy link

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)

@kazaamjt kazaamjt changed the title False positive: Field has no items() member [typehints] False positive: Field has no items() member Nov 20, 2018
@PCManticore
Copy link
Contributor

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

@PCManticore PCManticore added Bug 🪲 Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) labels Nov 23, 2018
@jfthuong
Copy link

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

@eduardomezencio
Copy link

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

@PCManticore
Copy link
Contributor

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

@yellowhat
Copy link

Hi,
may I ask to reopen this issue.
I have the following code:

@dataclass
class Case:
    """Case class (group Item)"""

    name: str
    irr: float = 0
    items: List[Item] = field(default_factory=lambda: [])

    def add_item(self, item: Item) -> None:
        """Add an item to the item list."""

        self.items.append(item)

With the version 2.10.2, pylint returns:

E1101: Instance of 'Field' has no 'append' member (no-member)

Thanks

@Pierre-Sassoulas
Copy link
Member

Hi @yellowhat, could you open a new issue, please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants