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

Functions with Optional[...] return annotations should not need all return statements #9000

Closed
retnikt opened this issue Jun 13, 2020 · 2 comments

Comments

@retnikt
Copy link

retnikt commented Jun 13, 2020

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request? Bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
def foo(bar: int) -> Optional[int]
    if bar == 3:
        return 6
  • What is the actual behavior/output?
error: Missing return statement
  • What is the behavior/output you expect?
    No error.
    If the function has a Optional return type, then surely it does not need a return statement everywhere because it might return None, and not returning means it implicitly returns None
  • What are the versions of mypy and Python you are using? mypy 0.780, CPython 3.8.3
    Do you see the same issue after installing mypy from Git master? Yes
  • What are the mypy flags you are using? (For example --strict-optional): None

According to the docs:

--no-warn-no-return

By default, mypy will generate errors when a function is missing return statements in some execution paths. The only exceptions are when:

   The function has a `None` or `Any` return type
   The function has an empty body or a body that is just ellipsis (`...`). Empty functions are often used for abstract methods.

Passing in --no-warn-no-return will disable these error messages in all cases.

So an exception for Optional should be added too (and Unions that contain None).

@oakkitten
Copy link

duplicate of #3974

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 19, 2020

Another duplicate is #7511.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants