[Python-ideas] A proliferation of (un-)Pythonically programmatic pragmas

MRAB python at mrabarnett.plus.com
Mon Nov 13 15:43:42 EST 2017

On 2017-11-13 19:10, Barry Warsaw wrote:
> I love many of the ancillary tools that help improve the quality of my
> Python code, including flake8, coverage, and mypy.  Each of these
> usually produces some great diagnostics, but none of them are perfect,
> so they also produce false positives that have to be suppressed.
> Each of these tools supports "pragmas", i.e. comments you can add to a
> line of code to suppress a warning.  E.g. with flake8 I can say:
>      import readline, rlcompleter        # noqa: F401
> to suppress the flake8 warnings about unused imports.  These modules
> work by side-effect, which of course a static analyzer can't know.
> Similarly, when mypy complains about a line it's confused on, I can add
> a comment to suppress the useless warning:
> try:
>      from pathlib import Path, PurePath
> except ImportError:
>      from pathlib2 import Path, PurePath      # type: ignore
> And when I want to boost coverage, but I know that some lines aren't
> covered under some versions of Python, I can do something like:
>              self.send_error(HTTPStatus.NOT_FOUND)   # pragma: nocover
> These are all well and good until I have to *combine* suppressions.
> E.g. in the pathlib2 pragma to mypy, I also get a flake8 warning and
> I've tried just about every combination of pragma comment I can think
> of, but I've not been able to make both tools happy.  I've resorted to
> refactoring the code into an entire module for flake8 to ignore and added:
>      # flake8: noqa
> to suppress all warnings in the file.  I actually have hit on a few
> places where I need to suppress warnings from all three tools on the
> same line, and I basically can't do it.
> The specifics aren't as important as the general use case: multiple
> tools competing for the same valuable real-estate.
> I have no ideas how to improve the situation, and of course any solution
> would involve some coordination between all of these tools, but it's
> beginning to feel like a losing battle.  Is there a better way?
I suppose that you could have suggest to them that they follow a 
convention such as:

1. There can be multiple pragmas in a comment, separated by semicolons: 
if you don't recognise it, skip past the semicolon.

2. A pragma can be prefixed with the name of the tool, e.g. "# 
flake8.noqa: F401": if there's a prefix, but it's not yours, skip past 
the semicolon.

More information about the Python-ideas mailing list