Skip to content

Commit

Permalink
Toughen ruff linting (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Feb 6, 2024
2 parents b01da5b + 79a4ce2 commit e9e95fb
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 76 deletions.
19 changes: 19 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ $ pip install --user --upgrade --pre gp-libs

- _Add your latest changes from PRs here_

### Development

- Strengthen linting (#31)

- Add flake8-commas (COM)

- https://docs.astral.sh/ruff/rules/#flake8-commas-com
- https://pypi.org/project/flake8-commas/

- Add flake8-builtins (A)

- https://docs.astral.sh/ruff/rules/#flake8-builtins-a
- https://pypi.org/project/flake8-builtins/

- Add flake8-errmsg (EM)

- https://docs.astral.sh/ruff/rules/#flake8-errmsg-em
- https://pypi.org/project/flake8-errmsg/

## gp-libs 0.0.6post0 (2024-02-01)

### Packaging
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
master_doc = "index"

project = about["__title__"]
copyright = about["__copyright__"]
project_copyright = about["__copyright__"]

version = "%s" % (".".join(about["__version__"].split("."))[:2])
release = "%s" % (about["__version__"])
Expand Down Expand Up @@ -95,7 +95,7 @@
"sidebar/navigation.html",
"sidebar/projects.html",
"sidebar/scroll-end.html",
]
],
}

# linkify_issues
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ select = [
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"A", # flake8-builtins
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"COM", # flake8-commas
"EM", # flake8-errmsg
"Q", # flake8-quotes
"PTH", # flake8-use-pathlib
"SIM", # flake8-simplify
Expand All @@ -103,6 +106,9 @@ select = [
"RUF", # Ruff-specific rules
"D", # pydocstyle
]
ignore = [
"COM812", # missing trailing comma, ruff format conflict
]

[tool.ruff.lint.isort]
known-first-party = [
Expand Down
25 changes: 16 additions & 9 deletions src/doctest_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def run(self) -> t.List[Node]:
node["options"][flag] = True # Skip the test
except InvalidSpecifier:
self.state.document.reporter.warning(
"'%s' is not a valid pyversion option" % spec, line=self.lineno
"'%s' is not a valid pyversion option" % spec,
line=self.lineno,
)
if "skipif" in self.options:
node["skipif"] = self.options["skipif"]
Expand Down Expand Up @@ -198,7 +199,7 @@ class DocTestFinderNameDoesNotExist(ValueError):
def __init__(self, string: str):
return super().__init__(
"DocTestFinder.find: name must be given "
f"when string.__name__ doesn't exist: {type(string)!r}"
f"when string.__name__ doesn't exist: {type(string)!r}",
)


Expand Down Expand Up @@ -298,8 +299,8 @@ def _find(
"source_lines": source_lines,
"globs": globs,
"seen": seen,
}
)
},
),
)
ext = pathlib.Path(name).suffix
logger.debug(f"parse, ext: {ext}")
Expand Down Expand Up @@ -328,7 +329,8 @@ def _find(
settings = OptionParser(components=(Parser,)).get_default_values()

doc = docutils.utils.new_document(
source_path=str(source_path), settings=settings
source_path=str(source_path),
settings=settings,
)
parser.parse(string, doc)

Expand Down Expand Up @@ -371,7 +373,9 @@ def condition(node: Node) -> bool:
if sys.version_info < (3, 13):

def _from_module(
self, module: t.Optional[t.Union[str, types.ModuleType]], object: object
self,
module: t.Optional[t.Union[str, types.ModuleType]],
object: object, # NOQA: A002
) -> bool:
"""Return true if the given object lives in the given module.
Expand All @@ -386,7 +390,7 @@ def _from_module(
# Type ignored because this is a private function.
return t.cast(
bool,
super()._from_module(module, object), # type:ignore[misc]
super()._from_module(module, object), # type:ignore[misc] # NOQA: A002
)

else: # pragma: no cover
Expand All @@ -412,7 +416,7 @@ class TestDocutilsPackageRelativeError(Exception):

def __init__(self) -> None:
return super().__init__(
"Package may only be specified for module-relative paths."
"Package may only be specified for module-relative paths.",
)


Expand Down Expand Up @@ -442,7 +446,10 @@ def testdocutils(
# Keep the absolute file paths. This is needed for Include directies to work.
# The absolute path will be applied to source_path when creating the docutils doc.
text, _ = doctest._load_testfile( # type: ignore
filename, package, module_relative, encoding or "utf-8"
filename,
package,
module_relative,
encoding or "utf-8",
)

# If no name was given, then use the file's name.
Expand Down
9 changes: 6 additions & 3 deletions src/linkify_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def condition(node: nodes.Node) -> bool:
isinstance(node, nodes.Text)
and len(re.findall(issue_re, node.astext())) > 0
) and not isinstance(
node.parent, (nodes.literal, nodes.FixedTextElement, nodes.reference)
node.parent,
(nodes.literal, nodes.FixedTextElement, nodes.reference),
)
return cond

Expand Down Expand Up @@ -74,13 +75,15 @@ def setup(app: Sphinx) -> SetupDict:
app.add_transform(LinkifyIssues)
app.add_config_value("issue_re", re.compile(DEFAULT_ISSUE_RE), "env")
app.add_config_value(
"issue_url_tpl", r"https://github.com/git-pull/gp-libs/issues/{issue_id}", "env"
"issue_url_tpl",
r"https://github.com/git-pull/gp-libs/issues/{issue_id}",
"env",
)

return SetupDict(
{
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
},
)
24 changes: 17 additions & 7 deletions src/pytest_doctest_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def pytest_unconfigure() -> None:


def pytest_collect_file(
file_path: pathlib.Path, parent: pytest.Collector
file_path: pathlib.Path,
parent: pytest.Collector,
) -> t.Optional[t.Union["DocTestDocutilsFile", "_pytest.doctest.DoctestModule"]]:
"""Test collector for pytest-doctest-docutils."""
config = parent.config
Expand All @@ -82,10 +83,11 @@ def pytest_collect_file(
(
_pytest.doctest._is_setup_py(file_path),
_pytest.doctest._is_main_py(file_path),
)
),
):
mod: t.Union[
DocTestDocutilsFile, _pytest.doctest.DoctestModule
DocTestDocutilsFile,
_pytest.doctest.DoctestModule,
] = _pytest.doctest.DoctestModule.from_parent(parent, path=file_path)
return mod
elif _is_doctest(config, file_path, parent):
Expand All @@ -94,7 +96,9 @@ def pytest_collect_file(


def _is_doctest(
config: pytest.Config, path: pathlib.Path, parent: pytest.Collector
config: pytest.Config,
path: pathlib.Path,
parent: pytest.Collector,
) -> bool:
if path.suffix in (".rst", ".md") and parent.session.isinitpath(path):
return True
Expand Down Expand Up @@ -142,7 +146,9 @@ def report_unexpected_exception(
test: "doctest.DocTest",
example: "doctest.Example",
exc_info: t.Tuple[
t.Type[BaseException], BaseException, types.TracebackType
t.Type[BaseException],
BaseException,
types.TracebackType,
],
) -> None:
if isinstance(exc_info[1], OutcomeException):
Expand Down Expand Up @@ -247,7 +253,9 @@ class DocutilsDocTestRunner(doctest.DocTestRunner):
"""DocTestRunner for doctest_docutils."""

def summarize( # type: ignore
self, out: "_Out", verbose: t.Optional[bool] = None
self,
out: "_Out",
verbose: t.Optional[bool] = None,
) -> t.Tuple[int, int]:
"""Summarize the test runs."""
string_io = io.StringIO()
Expand All @@ -261,7 +269,9 @@ def summarize( # type: ignore
return res

def _DocTestRunner__patched_linecache_getlines(
self, filename: str, module_globals: t.Any = None
self,
filename: str,
module_globals: t.Any = None,
) -> t.Any:
# this is overridden from DocTestRunner adding the try-except below
m = self._DocTestRunner__LINECACHE_FILENAME_RE.match(filename) # type: ignore
Expand Down
49 changes: 27 additions & 22 deletions tests/test_doctest_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class DocTestFinderFixture(t.NamedTuple):
"""
>>> 4 + 4
8
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -46,8 +46,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -63,8 +63,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
```
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -77,8 +77,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
```
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -91,8 +91,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -106,8 +106,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
:::
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -121,8 +121,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
```
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -138,8 +138,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
:::
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -155,8 +155,8 @@ class DocTestFinderFixture(t.NamedTuple):
>>> 4 + 4
8
```
"""
)
""",
),
},
tests_found=1,
),
Expand All @@ -179,8 +179,8 @@ class DocTestFinderFixture(t.NamedTuple):
6
```
````
"""
)
""",
),
},
tests_found=2,
),
Expand All @@ -195,7 +195,9 @@ def __init__(self, file_path_mode: str) -> None:


@pytest.mark.parametrize(
DocTestFinderFixture._fields, FIXTURES, ids=[f.test_id for f in FIXTURES]
DocTestFinderFixture._fields,
FIXTURES,
ids=[f.test_id for f in FIXTURES],
)
@pytest.mark.parametrize("file_path_mode", ["relative", "absolute"])
def test_DocutilsDocTestFinder(
Expand Down Expand Up @@ -230,7 +232,10 @@ def test_DocutilsDocTestFinder(
# Test
finder = doctest_docutils.DocutilsDocTestFinder()
text, _ = doctest._load_testfile( # type: ignore
str(first_test_filename), package=None, module_relative=False, encoding="utf-8"
str(first_test_filename),
package=None,
module_relative=False,
encoding="utf-8",
)
tests = finder.find(text, str(first_test_filename))
tests.sort(key=lambda test: test.name)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_linkify_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class LinkTestFixture(t.NamedTuple):


@pytest.mark.parametrize(
LinkTestFixture._fields, FIXTURES, ids=[f.test_id for f in FIXTURES]
LinkTestFixture._fields,
FIXTURES,
ids=[f.test_id for f in FIXTURES],
)
def test_links_show(
make_app: t.Callable[[t.Any], SphinxTestApp],
Expand Down
Loading

0 comments on commit e9e95fb

Please sign in to comment.