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

Infer return value of None for correctly inferred functions with no returns #983

Merged
merged 5 commits into from
May 24, 2021

Conversation

nelfin
Copy link
Contributor

@nelfin nelfin commented May 1, 2021

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

Functions with empty bodies or no return statements e.g. def f(): pass were returned as Uninferable from infer_call_result

Type of Changes

Type
🐛 Bug fix

Related Issue

Closes #485
Ref #663

Pylint PR: pylint-dev/pylint#4428

@nelfin
Copy link
Contributor Author

nelfin commented May 2, 2021

Rebased to fix conflict with changes to Instance.getitem

@nelfin nelfin marked this pull request as draft May 2, 2021 09:43
@nelfin nelfin marked this pull request as ready for review May 2, 2021 23:10
tests/unittest_inference.py Outdated Show resolved Hide resolved
astroid/bases.py Show resolved Hide resolved
@hippo91 hippo91 self-assigned this May 14, 2021
Copy link
Contributor

@hippo91 hippo91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nelfin thanks for this interesting MR!
I have got some interrogations before merging it. Maybe @cdce8p could also give us his point of view.

astroid/scoped_nodes.py Outdated Show resolved Hide resolved
astroid/bases.py Show resolved Hide resolved
tests/unittest_inference.py Show resolved Hide resolved
astroid/scoped_nodes.py Outdated Show resolved Hide resolved
tests/unittest_scoped_nodes.py Outdated Show resolved Hide resolved
tests/unittest_inference.py Outdated Show resolved Hide resolved
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All in all this looks like a reasonable change. Thanks for also opening pylint-dev/pylint#4428 to fix the pylint tests!

I ran some tests and didn't notice anything unusual, so should be ok to merge once all discussions are resolved.

@cdce8p cdce8p added the pylint-tested PRs that don't cause major regressions with pylint label May 23, 2021
@cdce8p cdce8p added this to the 2.5.7 milestone May 23, 2021
@nelfin nelfin force-pushed the feature/infer-none-with-no-return branch from 7ec6a68 to d66e3d1 Compare May 24, 2021 07:56
nelfin added a commit to nelfin/pylint that referenced this pull request May 24, 2021
@nelfin nelfin force-pushed the feature/infer-none-with-no-return branch from d66e3d1 to 6ba7778 Compare May 24, 2021 08:43
@nelfin nelfin requested a review from hippo91 May 24, 2021 08:46
@hippo91
Copy link
Contributor

hippo91 commented May 24, 2021

All in all this looks like a reasonable change. Thanks for also opening PyCQA/pylint#4428 to fix the pylint tests!

I ran some tests and didn't notice anything unusual, so should be ok to merge once all discussions are resolved.

Thanks @cdce8p !

Copy link
Contributor

@hippo91 hippo91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nelfin thanks for your answers! It is all good for me. Just left a comment regarding adding a comment but feel free to add it or not.

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.
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.
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 nelfin force-pushed the feature/infer-none-with-no-return branch from 6ba7778 to ce10268 Compare May 24, 2021 09:33
@nelfin
Copy link
Contributor Author

nelfin commented May 24, 2021

@nelfin thanks for your answers! It is all good for me. Just left a comment regarding adding a comment but feel free to add it or not.

I updated the comment with a more in-depth explanation

@hippo91 hippo91 merged commit d2394a3 into pylint-dev:master May 24, 2021
@hippo91
Copy link
Contributor

hippo91 commented May 24, 2021

Thanks @nelfin !

cdce8p pushed a commit to pylint-dev/pylint that referenced this pull request 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
pylint-tested PRs that don't cause major regressions with pylint Waiting on author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot infer empty functions
3 participants