pythonpyflakespycodestyle

Python: pyflakes not interpreting noqa comment


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

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

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:


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

Solution

  • 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).