Skip to content

Support the "attrs" library #1698

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

Closed
The-Compiler opened this issue Oct 11, 2017 · 11 comments · Fixed by pylint-dev/astroid#462
Closed

Support the "attrs" library #1698

The-Compiler opened this issue Oct 11, 2017 · 11 comments · Fixed by pylint-dev/astroid#462
Labels
Enhancement ✨ Improvement to a component Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)

Comments

@The-Compiler
Copy link
Contributor

Many projects (Twisted, Hypothesis, pytest, my own, and probably more) started using attrs because it makes it much more pleasant to write data classes.

However, pylint doesn't understand attrs' handling of default values:

import attr

@attr.s
class Foo:

    d = attr.ib(attr.Factory(dict))

f = Foo()
f.d['answer'] = 42

outputs:

E:  9, 0: 'f.d' does not support item assignment (unsupported-assignment-operation)

Unfortunately, I can't find a way to turn this off selectively either, e.g. by adding Foo to ignored-classes.

The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Oct 11, 2017
@PCManticore
Copy link
Contributor

This probably needs a brain plugin.
Also unfortunately ignored-classes can only be applied for a handful of errors, I think unsupported-assignment-operation doesn't fall in that category (ignored-classes is mostly for no-member and a bunch of others).

@PCManticore PCManticore added Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Enhancement ✨ Improvement to a component labels Oct 12, 2017
clrpackages pushed a commit to clearlinux-pkgs/astroid that referenced this issue Dec 20, 2017
….6.0

Ashley Whetter (1):
      Public facing node documentation

Bryce Guinta (1):
      Add transform for attr library to fix unsupported-assignment-operation false positives

Claudiu Popa (40):
      Don't crash when getting the string representation of BadUnaryOperationMessage
      Don't raise DuplicateBaseError when classes at different locations are used
      The func form of namedtuples with keywords is now understood
      Dunder class at method level is now inferred as the class of the method
      Fix indentation
      Enums created with functional syntax are now iterable. Close pylint-dev/pylint#1546
      Enums created with functional syntax are now iterable. Close pylint-dev/pylint#992
      Enum's functional form know about __members__
      Fix reconstruction of nodes for FormattedValue. Close pylint-dev/pylint#1582
      Ignore the test on <Python3.6
      namedtuple._replace expects the actual attributes to be replaced
      Disable stop-iteration-return until we'll drop support for python 2
      The result of using object.__new__ as class decorator is correctly inferred as instance
      Support AnnAssign in Enum bodies. Close #442
      Add brain tips for UUID.int
      Skip Uninferable inferred objects when inferring a Dict
      Check for both args and keyword arguments when extracting namedtuple's params. Close #450
      Remove print statement
      Ignore directories found in blacklist
      __package__ is accessible in every module. Close pylint-dev/pylint#1693
      Fix lint warnings
      lint the current directory, not the installed one
      Fix the remaining lint errors
      Run the local tests, not the installed ones, and strip the local path for coveralls
      Add ChangeLog entry
      Remove most of the copyright notices, since this file was written completely by @hippo91
      Add ChangeLog entries for @hippo91 changes
      astroid's master is 1.6
      Add pypy support for Python 3.3 in tox
      Revert "Add pypy support for Python 3.3 in tox"
      Remove file_stream since it was slated for removal for 1.6
      Fix lint
      When verifying duplicates classes in MRO, ignore on-the-fly generated classes
      Make Unknown a rhs assignable node
      Make attr.ib attributes instances of Unknown, not of Attribute
      Only mock attr.ib attributes
      Remove unreachable code
      Filter non-inferables nodes when inferring binary operations
      Add new release instructions as for pylint
      Add the release date

Hugo (4):
      Update classifiers (#453)
      Mark failing test as expected failure (#455)
      Drop support for EOL Python 3.3
      Update version comparisons after review

Michał Masłowski (1):
      Catch _NonDeducibleTypeHierarchy in inference._infer_augassign (#443)

hippo91 (8):
      Expose more objects in numpy brain tip
      Introduction of numpy.core.umath functions signatures inside numpy brain + add of a dedicated unit test
      Introduction of numpy.random.mtrand functions signatures inside numpy brain + add of a dedicated unit test
      Add of a class that wrapps the call to unittest.subTest method to handle different versions of python
      Add of encoding utf-8 declaration
      Deletion of old numpy test (replaced by a dedicated class)
      Add of copyright and simplification of the mock to try to pass python3.3 CI
      Add of decorator to skip the tests if numy module is not present

ioanatia (1):
      Fix finder for namespace packages (#440)

mathieui (1):
      Add brain_curses.py for curses attributes defined at runtime (#456)

Łukasz Rogalski (1):
      Improve understanding of collections.deque (#441)

2017-12-15 -- 1.6.0

   * When verifying duplicates classes in MRO, ignore on-the-fly generated classes

     Close pylint-dev/pylint#1706

   * Add brain tip for attrs library to prevent unsupported-assignment-operation false positives

	 Close pylint-dev/pylint#1698

   * file_stream was removed, since it was deprecated for three releases

     Instead one should use the .stream() method.

   * Vast improvements to numpy support

(NEWS truncated at 15 lines)
@tedbrandston
Copy link

So is this a thing where we should just be waiting for the next release of pylint? Or is there something we can do to get this working right now?

Thanks!

@The-Compiler
Copy link
Contributor Author

It's part of astroid 1.6.0 which already has been released.

@zadamah
Copy link

zadamah commented Jan 12, 2018

astroid 1.6.0 still reports an equivalent error if attr.s is given parameters, slightly modifying @The-Compiler example :

import attr

@attr.s(slots=True, cmp=True)
class Foo(object):
    d = attr.ib(attr.Factory(dict))

f = Foo()
f.d['answer'] = 42

outputs now :

E:  8, 0: 'f.d' does not support item assignment (unsupported-assignment-operation)

The predicate is_decorated_with_attrs (from astroid.brain.brain_attrs) is only True for decorators with no parameters, otherwise no transform is applied

I don't know enough about astroid (first day I read this part of the code) to propose a good way of handling this case, so if someone can help

And thank you all, this thread was a great read

@dfee
Copy link

dfee commented Aug 1, 2018

Why is this closed? It's not supported, at least not on:
• astroid 2.0.0.dev4,
• pylint 2.0.0,
• attrs 18.1.0

@zadamah
Copy link

zadamah commented Aug 1, 2018

I think that that attr decorators with no arguments are supported
My pull request for the case with arguments was merged to master but until recently it did not make its way to a release. @The-Compiler can you help us on this ?

@PCManticore
Copy link
Contributor

Folks, please open separate issues. Some attrs support landed in pylint but we cannot support everything out of the box unless we have separate issues for each particular thing that is not working. Also do take in account that we're all volunteers and we might not use attrs on a daily basis so there's a chance that we might not know what needs to be supported. Another thing is that asking why is this closed is usually not helpful, as most of the time we close issues either because there is no plan to fix them or (some partial) support for that particular feature landed already in the project.

@The-Compiler
Copy link
Contributor Author

@zadamah I don't understand what you want me to help with? It looks like with pylint-dev/astroid#479 this should work, and @dfee seems to be using an older astroid release.

@dfee Like @PCManticore said, if you're still having issues with an up-to-date pylint/astroid, please open a separate issue, with some more information than "it's not supported" - no issues here with attrs ever since this was closed.

@zadamah
Copy link

zadamah commented Aug 1, 2018

Actually I replied to @dfee here but it is about pylint-dev/astroid#479:

  • it was merged to astroid master on January
  • but it was not merged to the release branch 2.0 (last I checked)
  • so what I am not sure of is what can we do so that this feature is on the next astroid release and then in pylint as well ?

And obviously I completely agree what @PCManticore is saying. pylint is actually one of my favorite Python tools. I just want this feature to be available to everyone.

@PCManticore
Copy link
Contributor

@zadamah from what I can see your fix landed in the released version: https://github.com/PyCQA/astroid/blob/master/astroid/brain/brain_attrs.py. if it's in the master branch, then most likely it is released. Are you referring though to the 2.0 branch from astroid? If so, that's somewhat unrelated to the actual 2.0 release, since it is mostly an old refactoring branch that never got merged back.

@zadamah
Copy link

zadamah commented Aug 1, 2018

Yes I was referring to branch "2.0" from astroid, it got me a little confused.
And you are right, I haven't checked for while, but now I can see the changes in brain_attrs.py after upgrading to pylint 2.1.0 (and astroid 2.0.2).
Thank you @The-Compiler & @PCManticore.
And @dfee, I think that your issue could be solved by upgrading to the latest pylint release.

efiop added a commit to efiop/dvc-data that referenced this issue Aug 11, 2023
efiop added a commit to efiop/dvc-data that referenced this issue Aug 11, 2023
efiop added a commit to iterative/dvc-data that referenced this issue Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade)
Projects
None yet
5 participants