Skip to content

Adding a check for inconsistent-return-statements inside function or methods. #1641

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

Merged
merged 18 commits into from
Sep 26, 2017

Conversation

hippo91
Copy link
Contributor

@hippo91 hippo91 commented Aug 25, 2017

The code is quite simple. I'followed the recommendations of @AWhetter. However the most difficult part
was to determine if a function has an implicit return statement. This is done inside the recursive _is_node_return_ended function. I'm not sure it can handle all encountered cases but the associated unit test is quite extensive...
Close #1267

Copy link
Contributor

@AWhetter AWhetter left a comment

Choose a reason for hiding this comment

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

This is looking pretty good! Thanks for the change.
One final comment, please could you add complete docstrings where appropriate. So document the arguments, their types, and and returns as well. We have a mix of docstring styles. I'd prefer Google style but if you're more comfortable with sphinx style then feel free to use that.

@@ -27,6 +27,52 @@ def _all_elements_are_true(gen):
return values and all(values)


def _get_exception_handlers(node, exception):
Copy link
Contributor

Choose a reason for hiding this comment

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

pylint.checkers.utils.node_ignores_exception is doing something every similar to this, except that it's returning a boolean instead of the list of handlers. So that we're doing handler detection consistently, please could you split utils.node_ignores_exception into a utils.get_exception_handlers function, and then utils.node_ignores_exception can call this and return True if there are any exception handlers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @AWhetter for this review. In fact i get inspired from pylint.checkers.utils.node_ignores_exception. I will do what you are asking asap.

# if statement is returning if there are exactly two return statements in its
# children : one for the body part, the other for the orelse part
return_stmts = [_is_node_return_ended(_child) for _child in node.get_children()]
return_stmts = [_rs for _rs in return_stmts if _rs]
Copy link
Contributor

Choose a reason for hiding this comment

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

Trivial: You could simplify this slightly by removing this line and using return sum(return_stmts) == 2. Or instead keep this line and make the line below return len(return_stmts) == 2.

Copy link
Contributor

@AWhetter AWhetter left a comment

Choose a reason for hiding this comment

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

Looks good to me. I'll let one of the others merge this because it's been a while since I've looked at this sort of code so it would be good to have someone else check it over as well. Thanks!

self._return_nodes[node.name] = [_rnode for _rnode in return_nodes
if _rnode.frame() == node.frame()]

def _check_consistent_returns(self, node): # pylint: disable=redundant-returns-doc
Copy link
Contributor

Choose a reason for hiding this comment

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

This is raising a redundant-returns-doc because you have a returns section in a function that doesn't return anything. Remove the returns section and then you will be able to remove this disable.

@AWhetter AWhetter added the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Sep 9, 2017
Copy link
Contributor

@PCManticore PCManticore left a comment

Choose a reason for hiding this comment

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

This one looks pretty solid, good job! Left only two comments for you to check out.

@@ -287,7 +287,7 @@ def _has_bare_super_call(fundef_node):
return False


def _safe_infer_call_result(node, caller, context=None):
def _safe_infer_call_result(node, caller, context=None): # pylint: disable=inconsistent-return-statements
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to just fix the occurences of this new check rather than disable them

# but if the exception raised is handled, then the handler has to
# ends with a return statement
exc = utils.safe_infer(node.exc)
exc_name = exc.pytype().split('.')[-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

exc can be None or Uninferable, please check against that.

@PCManticore PCManticore added reviewed-waiting-updates and removed Needs review 🔍 Needs to be reviewed by one or multiple more persons labels Sep 21, 2017
@PCManticore PCManticore merged commit 56a6723 into pylint-dev:master Sep 26, 2017
@PCManticore
Copy link
Contributor

Thank you @hippo91 ! One thing I forgot to ask you was to add yourself to Contributors file. Feel free to send a PR for that when you get a chance. Thanks a lot for your contributions!

@hippo91
Copy link
Contributor Author

hippo91 commented Sep 26, 2017 via email

@hippo91 hippo91 deleted the enh1267 branch September 30, 2017 10:28
christensson pushed a commit to christensson/pylint that referenced this pull request Sep 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants