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

Cannot infer empty functions #485

Closed
brycepg opened this issue Jan 28, 2018 · 4 comments · Fixed by #983
Closed

Cannot infer empty functions #485

brycepg opened this issue Jan 28, 2018 · 4 comments · Fixed by #983
Labels

Comments

@brycepg
Copy link
Contributor

brycepg commented Jan 28, 2018

Steps to reproduce

import astroid
astroid.extract_node("""
def f():
    pass
f()
""").inferred()

Current behavior

raises StopIteration

Expected behavior

Returns [const.NoneType]

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

2.0.0

This also applies to procedural functions which don't explicitly return any values.

@PCManticore
Copy link
Contributor

Makes sense.

This happens because the inference for functions looks through each return value here (https://github.com/PyCQA/astroid/blob/629c92db2dc1b016f4bf47645c95c42e65fd3bd6/astroid/scoped_nodes.py#L1558) and tries to infer the result from there. But since functions like this don't have an explicit return value, the inference gets into raise_if_nothing_inferred over here (https://github.com/PyCQA/astroid/blob/ac3e82e9bd8678086325a71a927a06bbc43d415e/astroid/decorators.py#L140), resulting in the exception you see.

@brycepg
Copy link
Contributor Author

brycepg commented Mar 2, 2018

What should we infer for a function that always raises an exception? I don't think it should be None. Uninferable I guess?


I tried to add this, but It caused a cascade of errors where we are looking for Uninferable instead of const.None

@PCManticore
Copy link
Contributor

@brycepg I would say it should return Uninferable, as raising exceptions is not necessarily returning a value from the function. Although we'd want some mechanism to get what exceptions a function could raise.

Regarding your last statement, you mean you added Uninferable for functions that raise exceptions or for functions that return None? Which of these failed with the cascade of errors?

@brycepg
Copy link
Contributor Author

brycepg commented Mar 5, 2018

@PCManticore I made functions that do not have any return/yield nodes infer to None instead of Uninferable, and it broke a lot of tests.

nelfin added a commit to nelfin/astroid that referenced this issue May 1, 2021
Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
and it contains no Return nodes, then the implicit return value is None.
nelfin added a commit to nelfin/astroid that referenced this issue May 2, 2021
Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
and it contains no Return nodes, then the implicit return value is None.
nelfin added a commit to nelfin/astroid that referenced this issue May 2, 2021
Ref pylint-dev#485. To avoid additional breakage, we optionally extend is_abstract
to consider functions whose body is any raise statement (not just raise
NotImplementedError)
cdce8p added a commit to cdce8p/astroid that referenced this issue May 15, 2021
…h no returns

Squashed commit of the following:

commit 7ec6a68
Author: Andrew Haigh <hello@nelf.in>
Date:   Mon May 3 09:09:17 2021 +1000

    Update changelog

commit b50a7dd
Author: Andrew Haigh <hello@nelf.in>
Date:   Mon May 3 09:02:29 2021 +1000

    Update check of implicit return to ignore abstract methods

    Ref pylint-dev#485. To avoid additional breakage, we optionally extend is_abstract
    to consider functions whose body is any raise statement (not just raise
    NotImplementedError)

commit ff719c8
Author: Andrew Haigh <hello@nelf.in>
Date:   Sat May 1 12:19:52 2021 +1000

    Add check of __getitem__ signature to instance_getitem

commit 427d422
Author: Andrew Haigh <hello@nelf.in>
Date:   Sat May 1 11:45:27 2021 +1000

    Fix test definition of igetattr recursion and context_manager_inference

    Ref pylint-dev#663. This test did not actually check for regression of the issue
    fixed in 55076ca (i.e. it also passed on c87bea1 before the fix was
    applied). Additionally, it over-specified the behaviour it was
    attempting to check: whether the value returned from the context manager
    was Uninferable was not directly relevant to the test, so when this
    value changed due to unrelated fixes in inference, this test failed.

commit 8ec2b47
Author: Andrew Haigh <hello@nelf.in>
Date:   Sat Apr 24 09:16:16 2021 +1000

    Update FunctionDef.infer_call_result to infer None with no Returns

    Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
    and it contains no Return nodes, then the implicit return value is None.
nelfin added a commit to nelfin/astroid that referenced this issue May 24, 2021
Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
and it contains no Return nodes, then the implicit return value is None.
nelfin added a commit to nelfin/astroid that referenced this issue May 24, 2021
Ref pylint-dev#485. To avoid additional breakage, we optionally extend is_abstract
to consider functions whose body is any raise statement (not just raise
NotImplementedError)
nelfin added a commit to nelfin/astroid that referenced this issue May 24, 2021
Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
and it contains no Return nodes, then the implicit return value is None.
nelfin added a commit to nelfin/astroid that referenced this issue May 24, 2021
Ref pylint-dev#485. To avoid additional breakage, we optionally extend is_abstract
to consider functions whose body is any raise statement (not just raise
NotImplementedError)
nelfin added a commit to nelfin/astroid that referenced this issue May 24, 2021
Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
and it contains no Return nodes, then the implicit return value is None.
nelfin added a commit to nelfin/astroid that referenced this issue May 24, 2021
Ref pylint-dev#485. To avoid additional breakage, we optionally extend is_abstract
to consider functions whose body is any raise statement (not just raise
NotImplementedError)
cdce8p pushed a commit to pylint-dev/pylint that referenced this issue May 30, 2021
* Add regression tests of inference of implicit None return

Ref pylint-dev/astroid#485. Depends on pylint-dev/astroid#983.

* Update tests to return size from __len__

No reason why they can't when it's relevant, instead of disabling this
warning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants