From 7ce540e53f20f3318ef9744c441543070638d2f8 Mon Sep 17 00:00:00 2001 From: Dominic Davis-Foster Date: Thu, 11 Feb 2021 17:23:35 +0000 Subject: [PATCH] Refactor using the new flake8-helper library. --- flake8_sphinx_links/__init__.py | 41 +++++++++++----------------- formate.toml | 1 + repo_helper.yml | 3 +++ requirements.txt | 1 + tox.ini | 48 ++++++++++++++++----------------- 5 files changed, 43 insertions(+), 51 deletions(-) diff --git a/flake8_sphinx_links/__init__.py b/flake8_sphinx_links/__init__.py index d56ccfd..83f9d7c 100644 --- a/flake8_sphinx_links/__init__.py +++ b/flake8_sphinx_links/__init__.py @@ -35,10 +35,13 @@ import platform import re import sys -from typing import Any, Dict, Generator, List, Pattern, Tuple, Type, Union +from typing import Dict, List, Pattern, Union + +# 3rd party +import flake8_helper __author__: str = "Dominic Davis-Foster" -__copyright__: str = "2020 Dominic Davis-Foster" +__copyright__: str = "2020-2021 Dominic Davis-Foster" __license__: str = "MIT" __version__: str = "0.1.0" __email__: str = "dominic@davis-foster.co.uk" @@ -56,7 +59,7 @@ SXL001: str = "SXL001 Double backticked strings should be a link to Python documentation." # noqa: E501 -#: List of keywords that should become :file:`:py:obj:\`{}\`` +#: List of keywords which should become :file:`:py:obj:\`{}\`` py_obj: List[str] = [ "True", "False", @@ -76,7 +79,7 @@ to prevent conflict with Sphinx objects. """ -#: List of keywords that should become :file:`:py:exc:\`{}\`` +#: List of keywords which should become :file:`:py:exc:\`{}\`` exc: List[str] = [ "BaseException", "Exception", @@ -174,14 +177,14 @@ regex: Pattern = re.compile(fr"(``)({all_objs})(``)") -class Visitor(ast.NodeVisitor): +class Visitor(flake8_helper.Visitor): """ - A Flake8 plugin to check docstrings for double backticked strings + AST visitor to check docstrings for double backticked strings which should be links to the Python documentation. """ # noqa: D400 def __init__(self) -> None: - self.errors: List[Tuple[int, int, str]] = [] + super().__init__() self._from_imports: Dict[str, str] = {} def _check_docstring(self, node: Union[ast.ClassDef, ast.FunctionDef, ast.Module]) -> None: @@ -242,26 +245,12 @@ def visit_Module(self, node: ast.Module) -> None: # noqa: D102 super().generic_visit(node) -class Plugin: - """ - Flake8 plugin which checks for use of platform specific sphinx_links codes. +class Plugin(flake8_helper.Plugin[Visitor]): """ + Flake8 plugin to check docstrings for double backticked strings + which should be links to the Python documentation. + """ # noqa: D400 name: str = __name__ version: str = __version__ - - def __init__(self, tree: ast.AST): - self._tree = tree - - def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]: - """ - Traverse the Abstract Syntax Tree, extract docstrings and check them. - - Yields a tuple of (line number, column offset, error message, type(self)) - """ - - visitor = Visitor() - visitor.visit(self._tree) - - for line, col, msg in visitor.errors: - yield line, col, msg, type(self) + visitor_class = Visitor diff --git a/formate.toml b/formate.toml index 160490a..300ff3c 100644 --- a/formate.toml +++ b/formate.toml @@ -37,6 +37,7 @@ known_third_party = [ "coverage_pyver_pragma", "domdf_python_tools", "flake8", + "flake8_helper", "github", "pytest", "pytest_cov", diff --git a/repo_helper.yml b/repo_helper.yml index e65372d..5e28fc5 100644 --- a/repo_helper.yml +++ b/repo_helper.yml @@ -41,3 +41,6 @@ min_coverage: 100 sphinx_conf_epilogue: - nitpicky = True + +tox_unmanaged: + - testenv:lint diff --git a/requirements.txt b/requirements.txt index 8d7f646..248c078 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ flake8>=3.7 +flake8-helper>=0.1.1 diff --git a/tox.ini b/tox.ini index b79f762..5a688ff 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,6 @@ # * testenv # * testenv:docs # * testenv:build -# * testenv:lint # * testenv:mypy # * testenv:pyup # * testenv:coverage @@ -56,30 +55,6 @@ commands = twine check dist/*.tar.gz dist/*.whl check-wheel-contents dist/ -[testenv:lint] -basepython = python3.6 -changedir = {toxinidir} -ignore_errors = True -skip_install = True -deps = - flake8 >=3.8.2 - flake8-2020 >= 1.6.0 - flake8-builtins>=1.5.3 - flake8-docstrings>=1.5.0 - flake8-dunder-all>=0.1.1 - flake8-github-actions>=0.1.0 - flake8-pyi>=20.10.0 - flake8-pytest-style>=1.3.0 - flake8-sphinx-links>=0.0.4 - flake8-strftime>=0.1.1 - flake8-typing-imports>=1.10.0 - git+https://github.com/PyCQA/pydocstyle@5118faa7173b0e5bbc230c4adf628758e13605bf - git+https://github.com/domdfcoding/flake8-quotes.git - git+https://github.com/domdfcoding/flake8-rst-docstrings.git - git+https://github.com/domdfcoding/flake8-rst-docstrings-sphinx.git - pygments>=2.7.1 -commands = python3 -m flake8_rst_docstrings_sphinx flake8_sphinx_links tests --allow-toolbox {posargs} - [testenv:mypy] basepython = python3.6 ignore_errors = True @@ -151,5 +126,28 @@ package = flake8_sphinx_links addopts = --color yes --durations 25 timeout = 300 +[testenv:lint] +basepython = python3.6 +changedir = {toxinidir} +ignore_errors = True +deps = + flake8 >=3.8.2 + flake8-2020 >= 1.6.0 + flake8-builtins>=1.5.3 + flake8-docstrings>=1.5.0 + flake8-dunder-all>=0.1.1 + flake8-github-actions>=0.1.0 + flake8-pyi>=20.10.0 + flake8-pytest-style>=1.3.0 + flake8-sphinx-links>=0.0.4 + flake8-strftime>=0.1.1 + flake8-typing-imports>=1.10.0 + git+https://github.com/PyCQA/pydocstyle@5118faa7173b0e5bbc230c4adf628758e13605bf + git+https://github.com/domdfcoding/flake8-quotes.git + git+https://github.com/domdfcoding/flake8-rst-docstrings.git + git+https://github.com/domdfcoding/flake8-rst-docstrings-sphinx.git + pygments>=2.7.1 +commands = python3 -m flake8_rst_docstrings_sphinx flake8_sphinx_links tests --allow-toolbox {posargs} + [dep_checker] allowed_unused = flake8