Skip to content

Commit

Permalink
Refactor using the new flake8-helper library.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Feb 11, 2021
1 parent 6cf98af commit 7ce540e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 51 deletions.
41 changes: 15 additions & 26 deletions flake8_sphinx_links/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:\`{<keyword>}\``
#: List of keywords which should become :file:`:py:obj:\`{<keyword>}\``
py_obj: List[str] = [
"True",
"False",
Expand All @@ -76,7 +79,7 @@
to prevent conflict with Sphinx objects.
"""

#: List of keywords that should become :file:`:py:exc:\`{<keyword>}\``
#: List of keywords which should become :file:`:py:exc:\`{<keyword>}\``
exc: List[str] = [
"BaseException",
"Exception",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions formate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ known_third_party = [
"coverage_pyver_pragma",
"domdf_python_tools",
"flake8",
"flake8_helper",
"github",
"pytest",
"pytest_cov",
Expand Down
3 changes: 3 additions & 0 deletions repo_helper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ min_coverage: 100

sphinx_conf_epilogue:
- nitpicky = True

tox_unmanaged:
- testenv:lint
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
flake8>=3.7
flake8-helper>=0.1.1
48 changes: 23 additions & 25 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# * testenv
# * testenv:docs
# * testenv:build
# * testenv:lint
# * testenv:mypy
# * testenv:pyup
# * testenv:coverage
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 7ce540e

Please sign in to comment.