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

attrs: wrong type inferred from Factory attribute (using auto_attribs) #4341

Closed
belm0 opened this issue Apr 12, 2021 · 3 comments · Fixed by pylint-dev/astroid#2126 or pylint-dev/astroid#2303
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)
Milestone

Comments

@belm0
Copy link
Contributor

belm0 commented Apr 12, 2021

Steps to reproduce

attrs allows this form for factory, but pylint doesn't understand it:

import attr
from functools import partial
import numpy as np

@attr.attrs(auto_attribs=True)
class Foo:
    position: np.ndarray = attr.Factory(partial(np.zeros, 3))
    # position: np.ndarray = attr.attrib(default=attr.Factory(partial(np.zeros, 3)))  # this works

    def bar(self):
        return -self.position

Current behavior

 E1130: bad operand type for unary -: Factory (invalid-unary-operand-type)

Expected behavior

no error

pylint --version output

Result of pylint --version output:

pylint 2.7.2
astroid 2.5.1
Python 3.7.7 (default, Oct  5 2020, 09:18:33)
[Clang 9.0.0 (clang-900.0.39.2)]

Additional dependencies:

attrs=20.2.0
@hippo91
Copy link
Contributor

hippo91 commented May 13, 2021

@belm0 thanks for your report. Would it be possible to have smaller reproducer? For example without numpy dependency?
It would be clearer if this false positive arises from our handling of attr or numpy.

@hippo91 hippo91 added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) labels May 13, 2021
@belm0
Copy link
Contributor Author

belm0 commented May 16, 2021

confirmed it's not related to numpy:

import attr

@attr.attrs(auto_attribs=True)
class Foo:
    position: int = attr.Factory(lambda: 3)
    #position: int = attr.attrib(default=attr.Factory(lambda: 3))  # this works

    def bar(self):
        return -self.position
E1130: bad operand type for unary -: Factory (invalid-unary-operand-type)

@hippo91
Copy link
Contributor

hippo91 commented May 16, 2021

@belm0 thanks for this update!

AChenQ pushed a commit to AChenQ/tensorbay-python-sdk that referenced this issue May 19, 2021
pylint get annotation by function return not `__annotations__`,
so that it think `attr()` return a Field object,
will raise assigning-non-slot and no-member error

do the following for skip pylint check
* annotating slot to skip assigning-non-slot error
* add "__getattr__" function to skip no-member error

related issue
pylint-dev/pylint#4341
AChenQ pushed a commit to AChenQ/tensorbay-python-sdk that referenced this issue May 19, 2021
pylint get annotation by function return not `__annotations__`,
so that it think `attr()` return a Field object,
will raise assigning-non-slot and no-member error

do the following for skip pylint check
* annotating slot to skip assigning-non-slot error
* add "__getattr__" function to skip no-member error

related issue
pylint-dev/pylint#4341
AChenQ pushed a commit to AChenQ/tensorbay-python-sdk that referenced this issue May 19, 2021
pylint get annotation by function return not `__annotations__`,
so that it think `attr()` return a Field object,

raised error
`E0237: Assigning to attribute 'box2d' not defined in class slots
(assigning-non-slot)`
`E1101: Instance of 'Field' has no 'box3d' member (no-member)`

do the following for skip pylint check
* annotating slot to skip assigning-non-slot error
* add "__getattr__" function to skip no-member error

related issue
pylint-dev/pylint#4341
AChenQ pushed a commit to AChenQ/tensorbay-python-sdk that referenced this issue May 19, 2021
`attr()` returns `Field` as the class variables value, using type hint
as the real type of variables.
pylint use `Field` as the variable type, so it will report the following
errors

`E0237: Assigning to attribute 'box2d' not defined in class slots
(assigning-non-slot)`
`E1101: Instance of 'Field' has no 'box3d' member (no-member)`

do the following as a workaround for pylint errors
* remove `__slot__` for E0237
* add `__getattr__` function for E1101

related issue:
pylint-dev/pylint#4341
AChenQ pushed a commit to AChenQ/tensorbay-python-sdk that referenced this issue May 19, 2021
`attr()` returns `Field` as the class variables value, using type hint
as the real type of variables.
pylint use `Field` as the variable type, so it will report the following
errors

`E0237: Assigning to attribute 'box2d' not defined in class slots
(assigning-non-slot)`
`E1101: Instance of 'Field' has no 'box3d' member (no-member)`

do the following as a workaround for pylint errors
* remove `__slot__` for E0237
* add `__getattr__` function for E1101

related issue:
pylint-dev/pylint#4341

PR Closed: Graviti-AI#580
AChenQ pushed a commit to Graviti-AI/tensorbay-python-sdk that referenced this issue May 19, 2021
`attr()` returns `Field` as the class variables value, using type hint
as the real type of variables.
pylint use `Field` as the variable type, so it will report the following
errors

`E0237: Assigning to attribute 'box2d' not defined in class slots
(assigning-non-slot)`
`E1101: Instance of 'Field' has no 'box3d' member (no-member)`

do the following as a workaround for pylint errors
* remove `__slot__` for E0237
* add `__getattr__` function for E1101

related issue:
pylint-dev/pylint#4341

PR Closed: #580
mbyrnepr2 added a commit to mbyrnepr2/astroid that referenced this issue Apr 19, 2023
DanielNoord pushed a commit to pylint-dev/astroid that referenced this issue Apr 19, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.0.0b1 milestone Apr 19, 2023
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 3.0.0b1, 3.0.0a7 May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)
Projects
None yet
3 participants