flake8-pyi icon indicating copy to clipboard operation
flake8-pyi copied to clipboard

Callables whose return value is ignored

Open Akuli opened this issue 3 years ago • 3 comments

def foo(callback):
    callback()

There's a few ways to type this function:

def foo(callback: Callable[[], None]) -> None: ...
def foo(callback: Callable[[], Any]) -> None: ...
def foo(callback: Callable[[], object]) -> None: ...

Callable[[], None] is not great, because it doesn't allow you to pass a callback that does return something; that will also work at runtime, and the return value is ignored. When I learned about this, I changed my callbacks to use Callable[[], Any] instead. But Callable[[], object] would be even better.

We could check for this, but does it belong to this tool? This lint isn't really stub-specific.

Akuli avatar Jun 26 '22 09:06 Akuli

We could check for this, but does it belong to this tool? This lint isn't really stub-specific.

I'm starting to wonder if we should split the tool in two: one flake8 plugin for checks which only apply for stubs, another which has typing-related checks for .pyi files and .py files alike.

Or maybe we should just bite the bullet and add the necessary complexity to make the plugin .py-compatible as well, since an increasing number of our checks apply to both file types.

AlexWaygood avatar Jun 26 '22 09:06 AlexWaygood

We could also try to get some of the non-pyi-specific checks accepted into flake8/pyflakes(?).

srittau avatar Jun 26 '22 10:06 srittau

We could also try to get some of the non-pyi-specific checks accepted into flake8/pyflakes(?).

We could try, but I think they would probably be rejected. I think the checks we have here are probably too typing-specific.

There are other plugins that specialise in providing checks for annotations, e.g. https://github.com/sco1/flake8-annotations. The checks https://github.com/sco1/flake8-annotations currently does are pretty basic, though, imo.

AlexWaygood avatar Jun 26 '22 18:06 AlexWaygood