pylint-dev / pylint-dev/astroid

Unable to infer through a class constructor even with type hints

Open
#769 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug 🪳 Good first issue inference
Dominant language
Python
Stars
583
Forks
357
Avg merge
1d 1h
Merged PRs (30d)
23

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two astroid.extract_node reproductions and tracing the func_node.func.infer() calls, comparing the constructor-created and argument-passed cases. Done means the inference loop no longer encounters astroid.Uninferable when the typed Test object is passed through TestParent’s constructor.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.