Fix linter errors; use custom check for print statements because flake8-print is not working anymore

This commit is contained in:
Sumner Evans
2020-05-08 12:38:16 -06:00
parent db358d8f8d
commit 00b96c9bd4
5 changed files with 34 additions and 19 deletions

View File

@@ -80,10 +80,11 @@ Code Style
This project follows `PEP-8`_ **strictly**. The *only* exception is maximum line This project follows `PEP-8`_ **strictly**. The *only* exception is maximum line
length, which is 88 for this project (in accordance with ``black``'s defaults). length, which is 88 for this project (in accordance with ``black``'s defaults).
Additionally, lines that contain a single string literal are allowed to extend Lines that contain a single string literal are allowed to extend past the
past that. maximum line length limit.
Additionally, this project uses ``black`` to enforce consistent, deterministic
code style. This project uses flake8, mypy, and black to do static analysis of the code and
to enforce a consistent (and as deterministic as possible) code style.
Although you can technically do all of the formatting yourself, it is Although you can technically do all of the formatting yourself, it is
recommended that you use the following tools (they are automatically installed recommended that you use the following tools (they are automatically installed
@@ -100,19 +101,22 @@ before knowing if your code is the correct style.
* ``flake8-importorder`` (with the ``edited`` import style): enforce ordering * ``flake8-importorder`` (with the ``edited`` import style): enforce ordering
of import statements. of import statements.
* ``flake8-pep3101``: no ``%`` string formatting. * ``flake8-pep3101``: no ``%`` string formatting.
* ``flake8-print`` no print statements. Use the more powerful and useful
``logging`` library instead. In the rare case that you actually want to
print to the terminal (the ``--version`` flag for example), then just
disable this check with a ``# noqa: T001`` comment.
* `mypy`_ is used for type checking. All type errors must be resolved. * `mypy`_ is used for type checking. All type errors must be resolved.
* `black`_ is used for auto-formatting. The CI process runs ``black --check`` to * `black`_ is used for auto-formatting. The CI process runs ``black --check`` to
make sure that you've run ``black`` on all files (or are just good at manually make sure that you've run ``black`` on all files (or are just good at manually
formatting). formatting).
* ``TODO`` statements must include an associated issue number (in other words, * ``TODO`` statements must include an associated issue number (in other words,
if you want to check in a change with outstanding TODOs, there must be an if you want to check in a change with outstanding TODOs, there must be an
issue associated with it to fix it). issue associated with it to fix it).
* ``print`` statements are not allowed. Use the more powerful and useful
``logging`` library instead. In the rare case that you actually want to print
to the terminal (the ``--version`` flag for example), then just disable this
check with a ``# noqa`` or a ``# noqa: T001`` comment.
.. _black: https://github.com/psf/black .. _black: https://github.com/psf/black
.. _`PEP-8`: https://www.python.org/dev/peps/pep-0008/ .. _`PEP-8`: https://www.python.org/dev/peps/pep-0008/
.. _mypy: http://mypy-lang.org/ .. _mypy: http://mypy-lang.org/
@@ -122,7 +126,8 @@ checks for uses of ``print``. You can run the same checks that the lint job runs
yourself with the following commands:: yourself with the following commands::
$ flake8 $ flake8
$ mypy sublime $ mypy sublime tests/**/*.py
$ black --check .
$ ./cicd/custom_style_check.py $ ./cicd/custom_style_check.py
Testing Testing

View File

@@ -6,8 +6,17 @@ from pathlib import Path
from termcolor import cprint from termcolor import cprint
todo_re = re.compile(r"#\s*TODO:?\s*") todo_re = re.compile(r"\s*#\s*TODO:?\s*")
accounted_for_todo = re.compile(r"#\s*TODO:?\s*\((#\d+)\)") accounted_for_todo = re.compile(r"\s*#\s*TODO:?\s*\((#\d+)\)")
print_re = re.compile(r"\s+print\(.*\)")
def noqa_re(error_id: str = ""):
return re.compile(rf"#\s*noqa(:\s*{error_id})?\s*\n$")
def eprint(*strings):
cprint(" ".join(strings), "red", end="", attrs=["bold"])
def check_file(path: Path) -> bool: def check_file(path: Path) -> bool:
@@ -16,8 +25,12 @@ def check_file(path: Path) -> bool:
valid = True valid = True
for i, line in enumerate(file, start=1): for i, line in enumerate(file, start=1):
if todo_re.search(line) and not accounted_for_todo.search(line): if todo_re.match(line) and not accounted_for_todo.match(line):
cprint(f"{i}: {line}", "red", end="", attrs=["bold"]) eprint(f"{i}: {line}")
valid = False
if print_re.search(line) and not noqa_re("T001").search(line):
eprint(f"{i}: {line}")
valid = False valid = False
file.close() file.close()

View File

@@ -1,7 +1,6 @@
import json import json
import logging import logging
import os import os
import types
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from time import sleep from time import sleep
@@ -9,7 +8,7 @@ from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
import requests import requests
from .api_objects import Response, Song from .api_objects import Response
from .. import Adapter, api_objects as API, ConfigParamDescriptor from .. import Adapter, api_objects as API, ConfigParamDescriptor

View File

@@ -15,7 +15,7 @@ except Exception:
tap_imported = False tap_imported = False
try: try:
import keyring # import keyring
has_keyring = True has_keyring = True
except ImportError: except ImportError:

View File

@@ -132,9 +132,7 @@ class SearchResult:
if getattr(self, member) is None: if getattr(self, member) is None:
setattr(self, member, set()) setattr(self, member, set())
setattr( setattr(self, member, getattr(self, member, set()).union(set(results)))
self, member, getattr(getattr(self, member, set()), "union")(set(results)),
)
def _to_result(self, it: Iterable[S], transform: Callable[[S], str],) -> List[S]: def _to_result(self, it: Iterable[S], transform: Callable[[S], str],) -> List[S]:
all_results = sorted( all_results = sorted(