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

Inference fails on assert-only function #668

Closed
jtrakk opened this issue May 4, 2019 · 1 comment
Closed

Inference fails on assert-only function #668

jtrakk opened this issue May 4, 2019 · 1 comment
Labels

Comments

@jtrakk
Copy link

jtrakk commented May 4, 2019

I'm getting started with astroid. I want to find the functions that are called inside the test_sth function.

import astroid

source = """

def check_equal(a, b):
    assert a == b


def test_sth():
    check_equal(10, 11)

"""


def transform_call(node):
    for func in node.infer():
        print(func)


astroid.MANAGER.register_transform(astroid.nodes.Call, transform_call)
tree = astroid.parse(source)

I was expecting it to find the check_equal function. Running with python3.7, astroid 2.2.5, I get this exception:

Traceback

Traceback (most recent call last):
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/decorators.py", line 131, in raise_if_nothing_inferred
    yield next(generator)
StopIteration: {'node': <Call l.8 at 0x7fb9dcbfd5c0>, 'context': <astroid.context.InferenceContext object at 0x7fb9dcbfa0f8>}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/tmp/inf.py", line 21, in <module>
    tree = astroid.parse(source)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/builder.py", line 275, in parse
    return builder.string_build(code, modname=module_name, path=path)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/builder.py", line 142, in string_build
    return self._post_build(module, "utf-8")
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/builder.py", line 160, in _post_build
    module = self._manager.visit_transforms(module)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/manager.py", line 69, in visit_transforms
    return self._transform.visit(node)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 89, in visit
    module.body = [self._visit(child) for child in module.body]
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 89, in <listcomp>
    module.body = [self._visit(child) for child in module.body]
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 61, in _visit_generic
    return [self._visit_generic(child) for child in node]
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 61, in <listcomp>
    return [self._visit_generic(child) for child in node]
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 57, in _visit
    return self._transform(node)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/transforms.py", line 40, in _transform
    ret = transform_func(node)
  File "/home/user/tmp/inf.py", line 16, in transform_call
    for func in node.infer():
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/astroid/decorators.py", line 136, in raise_if_nothing_inferred
    raise exceptions.InferenceError(**error.args[0])
astroid.exceptions.InferenceError: Inference failed for <Call l.8 at 0x7fb9dcbfd5c0>.

I'm guessing this may be related to #485, but not sure. Am I doing something wrong or is there a workaround I can use? Thanks!

@jtrakk jtrakk changed the title Inference fails on function assert-only function Inference fails on assert-only function May 4, 2019
@PCManticore
Copy link
Contributor

Thanks for the report! You are not doing anything wrong, it's just a bug in the inference engine. We should infer an empty return there, instead of crashing if there is no explicit return in the body of the function. A workaround for now is to modify your code to have a blank return in it or to modify your transform and look for any return statements in the body of the function before triggering the inference code for that function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants