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

brain_attrs doesn't add PEP 526 instance variable annotations, causing no-member errors #2514

Open
Hnasar opened this issue Aug 23, 2024 · 1 comment
Milestone

Comments

@Hnasar
Copy link

Hnasar commented Aug 23, 2024

Steps to reproduce

In some contexts, Pylint issues a false positive with an attrs class with only a type annotation (see PEP 526. If we add the attribute via the astroid brain_attrs then this fixes the issue. (I'm not sure if this is a duplicate of #724 ). This is supported at runtime with the newer attrs decorators.

# pylint: disable=missing-docstring,too-few-public-methods,unnecessary-lambda-assignment
import attrs  # pylint: disable=import-error

@attrs.define
class AttrsCls:
    x: int

ac = AttrsCls(1)
ac.x += 1

Current behavior

regression_xxxx_attrs_no_member.py:13: [E1101(no-member), ] Instance of 'AttrsCls' has no 'x' member

Expected behavior

No error

Hnasar added a commit to Hnasar/astroid that referenced this issue Aug 23, 2024
Similar to dataclasses, the following class which uses instance
variable annotations is valid:

```py
@attrs.define
class AttrsCls:
    x: int
AttrsCls(1).x
```

However, before this commit astroid failed to transform the class
attribute into an instance attribute and this led to `no-member` errors
in pylint.

Only the new `attrs` API supports this form out-of-the-box, so just
address the common case.

Closes pylint-dev#2514
Pierre-Sassoulas pushed a commit that referenced this issue Aug 27, 2024
Similar to dataclasses, the following class which uses instance
variable annotations is valid:

```py
@attrs.define
class AttrsCls:
    x: int
AttrsCls(1).x
```

However, before this commit astroid failed to transform the class
attribute into an instance attribute and this led to `no-member` errors
in pylint.

Only the new `attrs` API supports this form out-of-the-box, so just
address the common case.

Closes #2514
@jacobtylerwalls jacobtylerwalls added this to the 3.3.3 milestone Sep 19, 2024
@jacobtylerwalls
Copy link
Member

Marking as blocker to ensure we forwardport the fix before the next feature release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants