pyflakes: F811 (redefinition of unused variable) incorrectly triggered with @overload decorator

PEP 484 introduced the @overload decorator for type annotations.

[…] a series of @overload-decorated definitions must be followed by exactly one non-@overload-decorated definition (for the same function/method). The @overload-decorated definitions are for the benefit of the type checker only, since they will be overwritten by the non-@overload-decorated definition, while the latter is used at runtime but should be ignored by a type checker.

The following should pass PyFlakes without errors, but currently triggers F811 errors:

from typing import overload


@overload
def utf8(value: None) -> None:
    pass


@overload
def utf8(value: bytes) -> bytes:
    pass


@overload
def utf8(value: str) -> bytes:
    pass


def utf8(value):
    pass  # actual implementation
test.py:9:1: F811 redefinition of unused 'utf8' from line 4
test.py:14:1: F811 redefinition of unused 'utf8' from line 9
test.py:19:1: F811 redefinition of unused 'utf8' from line 14

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 20
  • Comments: 15 (8 by maintainers)

Most upvoted comments

that’s not possible to detect statically – I’d suggest not doing that (I don’t think mypy can detect that either)

I’d suggest explicitly using the overload decorator

yea works on master, thanks!

Hi, it’s good to see https://github.com/PyCQA/pyflakes/pull/371 solved this partially, but overloads on classes is still incorrectly triggered

import typing
from typing import overload


class Foo:
    @overload
    def f(self, s):  # type: (None) -> None
        pass

    @overload
    def f(self, s):  # type: (int) -> int
        pass

    def f(self, s):
        return s

    @typing.overload
    def g(self, s):  # type: (None) -> None
        pass

    @typing.overload
    def g(self, s):  # type: (int) -> int
        pass

    def g(self, s):
        return s
☁  shm  pyflakes a.py 
a.py:11: redefinition of unused 'f' from line 6
a.py:16: redefinition of unused 'f' from line 10
☁  shm  pyflakes --version
2.1.1 Python 3.6.7 on Linux

I’m now working around this by using full typing.overload name

<strike>Added this to .pydocstyle

[pydocstyle]
ignore-decorators = overload
</strike> We ended up adding `# noqa: F811` comments.

facing the same issue. do you have a workaround?