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

Possible false positive "Missing return statement" if return type is Optional[int] etc. #9392

Closed
AndrewUshakov opened this issue Sep 1, 2020 · 2 comments
Labels
bug mypy got something wrong

Comments

@AndrewUshakov
Copy link

🐛 Bug Report

mypy generates error "Missing return statement" for both functions

def out() -> Optional[bool]:
    print('ops')


def sqr(x: Optional[int] = None) -> Optional[int]:
    if x is not None:
        return x**2

To Reproduce

  1. Create Python file test.py with the content above.
  2. Run mypy test.py

Expected Behavior

in both cases errors can be eliminated by adding just a return or return None at the end of both functions, but it is not really needed (does not change program behavior), because by default function without return statement at its end returns None which is a valid return value for Optional[]?

I know about the flag --no-warn-no-return but I do not want to mask other possible more dangerous events.

Actual Behavior

mypy generates error "Missing return statement" for both functions.

Your Environment

  • Mypy version used: 0.782
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini - file is abcent.
  • Python version used: 3.8.5 64-bit
  • Operating system and version: Windows 10
@AndrewUshakov AndrewUshakov added the bug mypy got something wrong label Sep 1, 2020
@AndrewUshakov AndrewUshakov changed the title Possible false posititive "Missing return statement" if return type is Optional[int] etc. Possible false positive "Missing return statement" if return type is Optional[int] etc. Sep 1, 2020
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Sep 1, 2020

Duplicate of #3974, #7511, #9000, #9312 and others

@AndrewUshakov
Copy link
Author

Thank you, I did not find #3974.

  1. I agree, that return None should exist in my second function according to PEP8:

"Be 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 return None, and an explicit return statement should be present at the end of the function (if reachable):"

But mypy error disappears if add just a return at the end of my second function, which is insufficient according to PEP8. Of course, it is a different small issue (false negative).

  1. My first function is valid according to PEP 8 (reason why Don't complain about missing return with Optional[<type>] #3974 was closed was PEP 8 violation).

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

2 participants