Skip to content

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

@retnikt

Description

@retnikt

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions