astroid icon indicating copy to clipboard operation
astroid copied to clipboard

Unsupported object typing when using `attr.ib` with `default`

Open danie1k opened this issue 7 years ago • 2 comments

This issue seems to be similar/continuation to #628. pylint misunderstood typing when using attr.ib(default=...).

I've tried typing.cast & attr.ib with type attr and the result is the same.

Tested on

  • Python 3.6.8
  • pylint 2.3.1
  • astroid 2.2.5

Steps to reproduce

  1. Sample python file:
    # tst.py
    from typing import Set
    import attr
    
    class ApiKlass:
        def a_method(self):
            pass
    
    @attr.s
    class D1:
        d = attr.ib()
        api: ApiKlass = attr.ib(default=ApiKlass())
        values: Set = attr.ib(default=set)
    
        def test(self):
            return self.api.a_method
    
        def set_test(self, value: str):
            if value not in self.values:
                raise ValueError()
    
  2. Run pylint tst.py.

Current behavior

[skip]
tst.py:16:15: E1101: Instance of '_CountingAttr' has no 'a_method' member (no-member)
[skip]
tst.py:19:24: E1135: Value 'self.values' doesn't support membership test (unsupported-membership-test)
tst.py:13:15: E1101: Instance of '_CountingAttr' has no 'a_method' member (no-member)

Expected behavior

Line 13 (class D1) should not raise no-member & unsupported-membership-test warnings.

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

danie1k avatar Mar 15 '19 18:03 danie1k

@danie1k Thanks for reporting the issue. Right now pylint does not understand any typing support for attrs. For each library that does things somewhat in a dynamic way, in which bucket attrs falls as well, we have to write custom inference transforms . You are seeing these issues because no one got to add support for attrib defaults just yet.

PCManticore avatar Mar 19 '19 08:03 PCManticore

Here's for example the current attrs transform: https://github.com/PyCQA/astroid/blob/master/astroid/brain/brain_attrs.py

PCManticore avatar Mar 19 '19 08:03 PCManticore