astroid icon indicating copy to clipboard operation
astroid copied to clipboard

Unable to infer through a class constructor even with type hints

Open artificial-aidan opened this issue 4 years ago • 2 comments

Steps to reproduce

import astroid

func_node = astroid.extract_node("""
class Test:
    def test():
        return
class TestParent:
    def __init__(self, test: Test):
        self._test = Test()  # type: Test

    def test_class(self):
        self._test.test() #@

test_parent = TestParent(Test())
""")

for inferred in func_node.func.infer():
    if inferred is astroid.Uninferable:
        assert()


func_node = astroid.extract_node("""
class Test:
    def test():
        return
class TestParent:
    def __init__(self, test: Test):
        self._test = test  # type: Test

    def test_class(self):
        self._test.test() #@

test_parent = TestParent(Test())
""")

for inferred in func_node.func.infer():
    if inferred is astroid.Uninferable:
        assert()

Current behavior

astroid cannot infer when the object is passed into the class constructor. Only when it is created in the constructor,

Expected behavior

astroid is able to infer through the constructor.

I may just be doing something wrong, but I've been beating my head against this wall for a while now.

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

2.3.3

artificial-aidan avatar Apr 23 '20 16:04 artificial-aidan

Hey @artificial-aidan Thanks for the report. You are hitting a limitation of astroid which is the fact that arguments passed via __init__ cannot be inferred, regardless of what types are passed in: https://github.com/PyCQA/astroid/issues/142

Adding support for types is on our roadmap but when we'll be able to act upon that roadmap is hard to tell.

PCManticore avatar Apr 23 '20 16:04 PCManticore

Thanks for the quick reply, glad to hear it wasn't my code. I'll subscribe to that bug and see what happens.

artificial-aidan avatar Apr 23 '20 16:04 artificial-aidan