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.32k stars 1.14k forks source link

False positive `no-member` when wrapping dataclasses `field` #9972

Open Quexington opened 1 month ago

Quexington commented 1 month ago

Bug description

If I want to alias/wrap a field for a dataclass, then pylint thinks that the members of the dataclass at that point are actually Field objects (at least when checking for no-member). Here's a minimal example:


from dataclasses import dataclass, field

def field_alias(**kwargs):
    # do some stuff analyzing the args

    # The following ignore is "fine" because it only needs to happen in one spot
    return field(**kwargs)  # pylint: disable=invalid-field-call

class ObjectWSubField:
    sub_field: int

@dataclass(frozen=True)
class Temp:
    the_object: ObjectWSubField = field_alias(type=ObjectWSubField)

    def some_method(self):
        # no-member error thrown here and I don't want to throw in an ignore at every spot
        print(self.the_object.sub_field)

Configuration

No response

Command used

pylint a.py

Pylint output

a.py:15:14: E1101: Instance of 'Field' has no 'sub_field' member (no-member)

Expected behavior

No error throw because object is correctly identified as the type it is hinted as

Pylint version

pylint 3.2.6
astroid 3.2.4
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]

OS / Environment

Windows 11

Additional dependencies

No response

alexjircan commented 3 weeks ago

I have the same issue too :(

alexjircan commented 3 weeks ago

It is similar to #4813