-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Don't complain about missing return with Optional[<type>]
#3974
Comments
This is a style issue. There's something in PEP 8 that says you should have an explicit |
For anyone looking at this later, I think this is what they were talking about: PEP 8 - Programming RecommendationsBe consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as # Correct:
def foo(x):
if x >= 0:
return math.sqrt(x)
else:
return None
def bar(x):
if x < 0:
return None
return math.sqrt(x) # Wrong:
def foo(x):
if x >= 0:
return math.sqrt(x)
def bar(x):
if x < 0:
return
return math.sqrt(x) |
If this behavior is explicitly desired, then there should be a clearer error message. For example instead of
|
@abrahammurciano, I think that's a fair point, but I'd advise opening a new issue to discuss the error message, rather than leaving a comment on an issue that's been closed for 5 years |
Currently mypy complains about missing return here and adding
return None
in the end of the function fixes that. However I think that's undesirable:return None
line (evenreturn
doesn't work) which is just noise in the program code in this casereturn
andreturn None
mean absolutely the same in each function, but mypy only recognizes one of those forms in this case.Obviously that seems like a simple example, but I have a longer if/elif function where mypy just says
missing return on <first line of function>
which has two issues : it's not a type bug, and mypy doesn't the invalid branchThe text was updated successfully, but these errors were encountered: