pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.26k stars 1.12k forks source link

Incorrect complaint about the `_fields` attribute of instances of `typing.NamedTuple` #9752

Closed crazyscientist closed 3 months ago

crazyscientist commented 3 months ago

Bug description

Imagine this simplified example:

from typing import Any, Dict, NamedTuple

class Example(NamedTuple):
    """Example docstring"""
    a: int
    b: str

    def as_dict(self) -> Dict[str, Any]:
        """Silly usage example for _fields attribute"""
        return {field: getattr(self, field) for field in self._fields}

example = Example(a=100, b="test")
print(example.as_dict())

It accesses the _fields attribute of typing.NamedTuple. This works as expected.

But pylint, complains.

Configuration

I used the default configuration

Command used

pylint <file_path>

Pylint output

************* Module file_path
<file_path>:11:57: E1101: Instance of 'Example' has no '_fields' member (no-member)

Expected behavior

NameTuple provides the _fields attribute and pylint should not complain

Pylint version

pylint 3.2.4
astroid 3.2.2
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

OS / Environment

Ubuntu 22.04

Additional dependencies

No response

mbyrnepr2 commented 3 months ago

Thanks @crazyscientist. It feels like we could add this case to https://github.com/pylint-dev/pylint/issues/7891 and track everything there; quite a similar case.

crazyscientist commented 3 months ago

@mbyrnepr2 Yes, that one looks very similar, but I failed to find it before reporting.