Skip to content

Provide option to warn about non-abstract empty functions #9772

@nickgaya

Description

@nickgaya

Feature

According to the documentation for the --no-warn-no-return option:

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.

I propose that the second bullet point should only apply to abstract methods. Functions and non-abstract methods with an empty body or a body that is just ellipsis should produce a "Missing return statement" error as usual.

This behavior could be controlled with a feature flag such as --warn-no-return-empty.

Pitch

Apart from abstract methods, I don't see why empty functions/methods should be excluded from no-return checking. For example, the following function clearly does not satisfy its return type annotation:

def return_a_string() -> str:
    pass

The same reasoning applies to methods. An empty method may indicate a programming error such as a missing @abstractmethod annotation.

from abc import ABC, abstractmethod
class MyABC(ABC):
    @abstractmethod
    def return_an_int(self) -> int: ...

    # whoops, forgot to annotate with @abstractmethod
    def return_a_string(self) -> str: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions