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

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

Closed
nickgaya opened this issue Nov 30, 2020 · 1 comment
Closed

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

nickgaya opened this issue Nov 30, 2020 · 1 comment
Labels

Comments

@nickgaya
Copy link

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: ...
@nickgaya
Copy link
Author

nickgaya commented Feb 4, 2022

Closing as a duplicate of #2350

@nickgaya nickgaya closed this as completed Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant