-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
check for control-flow paths that fail to return a value #1229
Comments
It's because mypy doesn't do proper checking for Optional -- in effect |
I think this is more of the second issue. In order to ensure that functions return something, we'll need to do some control flow analysis, but I think it'll be quite some time before we get to that (because there's a lot else to do!). |
Thanks for your quick replies. I'll leave it up to you whether you want to leave this issue open or close the issue. |
Added "needs discussion" label because the design will require some, um, discussion. |
pass
doesn't cause error
Whatever we decide about functions with a return type of def f() -> int:
pass # no error
def g() -> int:
return # E: Return value expected Like @ddfisher says, this is a matter of doing control flow analysis. I wouldn't be surprised if we can cajole the |
It turns out that (as suggested at #320) just tacking on a The most interesting false positive cases were those in def expect(self, string: str) -> Token:
if self.current_str() == string:
self.ind += 1
return self.tok[self.ind - 1]
else:
self.parse_error()
def parse_error(self) -> None:
self.parse_error_at(self.current())
raise ParseError() Here |
This would probably require a |
Yeah, having an annotation for this would be reasonable. There are several ways we could do it. Here are some random ideas:
The last option might be reasonable if we don't want to rush a PEP 484 change (or it's hard to get the change through) or if we also want to add additional mypy-only annotations or directives, perhaps for things like enabling/disabling specific warnings. It would require a parser change. |
Whoa, for some reason github didn't send me an email for @rwbarton's comments, but did for @ddfisher's. I've rewritten the type binder code to be cleaner. Once the pull request is merged, you can avoid the hack of tacking on It looks to me that just special casing |
See python#1229. In most cases, `return None`; in some others, `assert False` in codepaths that don't occur, but that type information is insufficient to prove it. The only thing that doesn't work is self.parse_error().
I've put a branch that does this at https://github.com/ecprice/mypy/tree/error-on-fall . The only tests that fail are due to |
Probably true due to your new commit "Keep track of whether a try/except block can fall off its end." as there were surprisingly many occurrences involving It would be really nice to also have a way to test for false negatives on a real code base; maybe automatically adding an |
See python#1229. In most cases, `return None`; in some others, `assert False` in codepaths that don't occur, but that type information is insufficient to prove it. The only thing that doesn't work is self.parse_error().
A complementary way of checking the control flow logic is to give errors on unused lines of code. I've added a couple commits to It finds errors in several places that are like
which should perhaps be special cased as OK? |
See python#1229. In most cases, `return None`; in some others, `assert False` in codepaths that don't occur, but that type information is insufficient to prove it. The only thing that doesn't work is self.parse_error().
See python#1229. In most cases, `return None`; in some others, `assert False` in codepaths that don't occur, but that type information is insufficient to prove it. The only thing that doesn't work is self.parse_error().
See python#1229. In most cases, `return None`; in some others, `assert False` in codepaths that don't occur, but that type information is insufficient to prove it. The only thing that doesn't work is self.parse_error().
See python#1229. In most cases, `return None`; in some others, `assert False` in codepaths that don't occur, but that type information is insufficient to prove it. The only thing that doesn't work is self.parse_error().
I sort of like the decorator idea (alternate name suggestion: See also python/typing#165 which suggests a return type Another real use case: |
This was fixed in the last release, right? |
I noticed that for a function that should return a value, for example:
mypy doesn't report an error if there is a pass (and no return statement) in the function. Is this intentional?
The text was updated successfully, but these errors were encountered: