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

Optional return type requires explicit return statement for non-empty function #13203

Closed
logmackenzie opened this issue Jul 20, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@logmackenzie
Copy link

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
@logmackenzie logmackenzie added the bug mypy got something wrong label Jul 20, 2022
@sobolevn
Copy link
Member

I would argue that raising an error here:

def func(self) -> Optional[int]:
        self.x += 1

is a correct thing to do. Why? Most of the time user might really forget to return self.x from func.

If you want to silence this error for some reason, use explicit return:

def func(self) -> Optional[int]:
        self.x += 1
        return None

It should work correctly.

@hauntsaninja
Copy link
Collaborator

Duplicate of #7511 (comment)

@hauntsaninja hauntsaninja marked this as a duplicate of #7511 Jul 23, 2022
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants