2

I have something weird going on with pyflakes and noqa comments.

I have a class similar to the one below (MyExample):

  • It's the only file in a directory called pyflakes_f811_test.
  • It only inherits from abc.ABC.
  • I use typing.overload to overload a method within the class.

Invoking pyflakes from the command line messages redefinition of unused 'enter_yes_no' from line 25. Thus, I added in # noqa: F811 comments, but the messages do not go away.

My questions:

  • Does anyone know what's going on here?
  • Are there any known reasons this can happen?
  • Any tips on debugging this?

Source Code

Name: pyflakes_f811_overload.py

#!/usr/bin/env python3

"""Testing pyflakes F811."""


from abc import ABC
from enum import Enum
from typing import overload, Union


class YesNoOptions(Enum):
    """Enum representing basic states of a yes/no."""

    YES = "YES"
    NO = "NO"


class MyExample(ABC):  # pylint: disable=too-few-public-methods
    """Example class."""

    # pylint: disable=no-self-use
    @overload
    def enter_yes_no(self, input_: YesNoOptions):
        """Enter yes/no using an enum."""
        ...

    # pylint: disable=no-self-use
    @overload  # noqa: F811
    def enter_yes_no(self, input_: str):
        """Enter yes/no using a string."""
        ...

    def enter_yes_no(self, input_: Union[YesNoOptions, str]):  # noqa: F811
        """Enter yes/no."""
        if isinstance(input_, str):
            parsed_input = input_.upper()
        elif isinstance(input_, YesNoOptions):
            parsed_input = input_.value
        else:
            raise NotImplementedError(
                f"Did not implement yes/no parsing for input {repr(input_)} of "
                f"type {type(input_)}."
            )

        print(f"User entered: {parsed_input}")


Reproducing

pyflakes is invoked via the command line as such:

(pyflakes_venv) ➜  pyflakes_f811_test pyflakes ./pyflakes_f811_overload.py
./pyflakes_f811_overload.py:28: redefinition of unused 'enter_yes_no' from line 22
./pyflakes_f811_overload.py:33: redefinition of unused 'enter_yes_no' from line 28

Package versions:

python==3.6.5
pycodestyle==2.4.0
pyflakes==2.1.1
prospector==1.2.0
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
  • Reproducing the lint warnings with the code sample, configuration for `.prospector.yml`, and package versions you provided proves unsuccessful. Can you provide a reproducible setup? – Oluwafemi Sule Apr 02 '20 at 06:18
  • @OluwafemiSule I updated the question eliminating `prospector` to make it more reproducible. Any ideas now, and are you able to reproduce? – Intrastellar Explorer Apr 02 '20 at 18:05
  • At some point, you may want to consider whether the flexibility provided by such an overloaded function is really worth the effort it takes to make your tools accept it. – chepner Apr 02 '20 at 18:14
  • @IntrastellarExplorer Are you sure that Pyflakes support `noqa` comments? I cannot find this documented anywhere. You might want to use flake8 instead and check this post: https://stackoverflow.com/questions/5033727/how-do-i-get-pyflakes-to-ignore-a-statement – Nelson Yeung Apr 02 '20 at 18:16
  • In this case, is it really too much to ask the caller to use `enter_yes_no(YesNoOptions("YES"))`, rather than supporting `enter_yes_no("YES")` directly? Remembering *one* accepted type is a lot easier than remember which `k` types are accepted. ("Can I pass `True` directly, or do I have to convert that to `"YES"` or `YesNoOptions.YES` first?") – chepner Apr 02 '20 at 18:18
  • @NelsonYeung yes according to this comment, it seems to have been done: https://github.com/PyCQA/pyflakes/issues/320#issuecomment-365299137, I may be wrong though – Intrastellar Explorer Apr 02 '20 at 18:19
  • @chepner the sample code I have here is just a sample, with all dependencies abstracted away. My actual use case is different, and would have over-complicated this question. And per having an overloaded method, I think it's reasonable, when the tradeoff should be just adding `# noqa` comments. Just in this particular use case, it's causing me problems :/ – Intrastellar Explorer Apr 02 '20 at 18:24
  • @IntrastellarExplorer It sounds like he's using flake8, to be honest. There're a few mentions of flake8 in that issue. Since flake8 is a wrapper around Pyflakes, people would still log problems to Pyflakes. – Nelson Yeung Apr 02 '20 at 19:03

1 Answers1

2

Pyflakes does not support the noqa comments for ignoring specific lines. You can check in their source code https://github.com/PyCQA/pyflakes that there is not a mention of noqa. The noqa feature is only in flake8. Since flake8 uses Pyflakes I suggest you switch to flake8:

pip install flake8
flake8 ./pyflakes_f811_overload.py

For your particular problem of the @overload decorator, although it has been fixed in the master branch (#435), it has not been released yet (as of 02/April/2020).

Nelson Yeung
  • 3,262
  • 3
  • 19
  • 29