-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
Mypy seems to require an explicit return statement for non-empty functions with an optional return type.
This is similar to #3974, but in that case the example had inconsistent return statements that are not permitted by PEP 8. However in my example it is acceptable to be consistent in not having a return statement in a function or method according to PEP 8, but it appears that if the function or method has any body without an explicit return statement mypy complains about Missing return statement
.
I believe this to be a bug as the behavior is inconsistent with my understanding of the PEP 8 standard though if I am mistaken please clarify.
To Reproduce
In this trivial example I have a base class that defines a couple methods that can optionally return values. The empty
method is empty and mypy does not have any issue with it, but it has an issue with func
for Missing return statement
.
from typing import Optional
class A:
def __init__(self) -> None:
self.x = 3
def empty(self) -> Optional[int]:
pass
def func(self) -> Optional[int]:
self.x += 1
class B(A):
def func(self) -> Optional[int]:
super().func()
if self.x > 10:
return self.x
return None
b = B()
print(b.func())
Expected Behavior
I would have expected no mypy errors.
Actual Behavior
I am getting Missing return statement
for class A
's method func
.
Your Environment
- Mypy version used: 0.971
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used: 3.8.7
- Operating system and version: Windows 10