Skip to content

Don't complain about missing return with Optional[<type>] #3974

@alehander92

Description

@alehander92
from typing import Optional

def s(e: int) -> Optional[int]:
    if e > 2:
        return 4

s(2)

Currently mypy complains about missing return here and adding return None in the end of the function fixes that. However I think that's undesirable:

  • It's not actually catching a bug: it's a false positive. False positives are bad as they lead to lost time and confusion
  • It motivates people to put a useless return None line (even return doesn't work) which is just noise in the program code in this case
  • It invalidates core Python behavior: since the dawn of time, no return, return and return None mean absolutely the same in each function, but mypy only recognizes one of those forms in this case.

Obviously that seems like a simple example, but I have a longer if/elif function where mypy just says missing return on <first line of function> which has two issues : it's not a type bug, and mypy doesn't the invalid branch

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