diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 65ec432..83917d2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,7 +25,10 @@ repos: - repo: https://github.com/pre-commit/pygrep-hooks rev: v1.10.0 hooks: + - id: python-check-blanket-noqa + - id: python-no-log-warn - id: python-use-type-annotations + - id: text-unicode-replacement-char - repo: https://github.com/asottile/pyupgrade rev: v3.21.2 @@ -35,7 +38,7 @@ repos: - "--py310-plus" - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.15.9" + rev: "v0.15.11" hooks: - id: ruff-check args: diff --git a/mkdocs_rss_plugin/git_manager/ci.py b/mkdocs_rss_plugin/git_manager/ci.py index f1594d2..a688c35 100644 --- a/mkdocs_rss_plugin/git_manager/ci.py +++ b/mkdocs_rss_plugin/git_manager/ci.py @@ -5,7 +5,8 @@ # ################################## # standard library -from os import environ, path +from os import environ +from pathlib import Path # 3rd party from git import Git @@ -111,4 +112,4 @@ def is_shallow_clone(self) -> bool: Returns: bool: True if a repo is shallow clone """ - return path.exists(".git/shallow") + return Path(".git/shallow").exists() diff --git a/mkdocs_rss_plugin/models.py b/mkdocs_rss_plugin/models.py index 6c433ab..ce1aa7e 100644 --- a/mkdocs_rss_plugin/models.py +++ b/mkdocs_rss_plugin/models.py @@ -8,18 +8,19 @@ from __future__ import annotations # standard -from collections.abc import MutableMapping from dataclasses import dataclass, field -from datetime import datetime -from pathlib import Path -from typing import Any - -# 3rd party -from mkdocs.structure.pages import Page +from typing import TYPE_CHECKING, Any # package modules from mkdocs_rss_plugin.__about__ import __title__, __version__ +if TYPE_CHECKING: + from collections.abc import MutableMapping + from datetime import datetime + from pathlib import Path + + from mkdocs.structure.pages import Page + # ############################################################################ # ########## Classes ############### # ################################## diff --git a/mkdocs_rss_plugin/plugin.py b/mkdocs_rss_plugin/plugin.py index 666ba48..a650280 100644 --- a/mkdocs_rss_plugin/plugin.py +++ b/mkdocs_rss_plugin/plugin.py @@ -60,7 +60,7 @@ class GitRssPlugin(BasePlugin[RssPluginConfig]): # allow to set the plugin multiple times in the same mkdocs config supports_multiple_instances = True - def __init__(self, *args, **kwargs): + def __init__(self, *args, **kwargs) -> None: """Instantiation.""" # pages storage super().__init__(*args, **kwargs) diff --git a/mkdocs_rss_plugin/util.py b/mkdocs_rss_plugin/util.py index 332a769..0f6d852 100644 --- a/mkdocs_rss_plugin/util.py +++ b/mkdocs_rss_plugin/util.py @@ -5,7 +5,6 @@ # ################################## # standard library -import logging from collections.abc import Iterable from datetime import date, datetime from functools import lru_cache @@ -76,16 +75,21 @@ def __init__( mkdocs_command_is_on_serve: bool = False, path: str = ".", use_git: bool = True, - ): + ) -> None: """Class hosting the plugin logic. Args: - path (str, optional): path to the git repository to use. Defaults to ".". - use_git (bool, optional): flag to use git under the hood or not. Defaults to True. + cache_dir: _description_. Defaults to DEFAULT_CACHE_FOLDER. integration_material_blog (bool, optional): option to enable - integration with Blog plugin from Material theme. Defaults to True. + integration with Blog plugin from Material theme. \ + Defaults to None. integration_material_social_cards (bool, optional): option to enable - integration with Social Cards plugin from Material theme. Defaults to True. + integration with Social Cards plugin from Material theme. \ + Defaults to None. + mkdocs_command_is_on_serve: _description_. Defaults to False. + path (str, optional): path to the git repository to use. Defaults to ".". + use_git (bool, optional): flag to use git under the hood or not. \ + Defaults to True. """ self.mkdocs_command_is_on_serve = mkdocs_command_is_on_serve if self.mkdocs_command_is_on_serve: @@ -294,20 +298,20 @@ def get_file_dates( format="%at", ) except GitCommandError as err: - logging.warning( + logger.info( f"Unable to read git logs of '{in_page.file.abs_src_path}'. " "Is git log readable? Falling back to build date. " "To disable this warning, set 'use_git: false' in plugin options. " f"Trace: {err}" ) except GitCommandNotFound as err: - logging.error( + logger.warning( "Unable to perform command 'git log'. Is git installed? " "Falling back to build date. " "To disable this warning, set 'use_git: false' in plugin options. " f"Trace: {err}" ) - self.git_is_valid = 0 + self.git_is_valid = False # convert timestamps into datetimes if isinstance(dt_created, (str, float, int)) and dt_created: dt_created = set_datetime_zoneinfo( @@ -374,7 +378,7 @@ def get_authors_from_meta(self, in_page: Page) -> Optional[tuple[str]]: elif isinstance(in_page.meta.get("author"), (list, tuple)): return tuple(in_page.meta.get("author")) else: - logging.warning( + logger.warning( "Type of author value in page.meta " f"({in_page.file.abs_src_path}) is not valid. " "It should be str, list or tuple, " @@ -396,7 +400,7 @@ def get_authors_from_meta(self, in_page: Page) -> Optional[tuple[str]]: else: return tuple(in_page.meta.get("authors")) else: - logging.warning( + logger.warning( "Type of authors value in page.meta (%s) is not valid. " "It should be str, list or tuple, not: %s." % in_page.file.abs_src_path, @@ -754,8 +758,8 @@ def get_site_url(mkdocs_config: MkDocsConfig) -> Optional[str]: Returns: str | None: site url """ - # this method exists because the following line returns an empty string instead of \ - # None (because the key always exists) + # this method exists because the following line returns an empty string instead + # of None (because the key always exists) defined_site_url = mkdocs_config.site_url # cases diff --git a/pyproject.toml b/pyproject.toml index 497b2ab..a6f9175 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -148,6 +148,57 @@ line_length = 88 multi_line_output = 3 use_parentheses = true +[tool.ruff] +line-length = 88 +target-version = "py310" +exclude = [".git", "__pycache__", "old", "build", "dist", "tests/dev", ".venv*"] + +[tool.ruff.format] +docstring-code-format = true +docstring-code-line-length = "dynamic" +indent-style = "space" +line-ending = "auto" +quote-style = "double" +skip-magic-trailing-comma = false + +[tool.ruff.lint] +select = [ + "A", # flake8-builtins + "ANN", # flake8-annotations + "B", # flake8-bugbear + "C90", # mccabe complexity + "E", # pycodestyle errors + "ERA", # eradicate (commented code) + "F", # pyflakes + "I", # isort + "LOG", # flake8-logging + "N", # pep8-naming + "PTH", # flake8-pathlib + "S", # bandit security + "TC", # flake8-type-checking + "T20", # flake8-print + "W", # pycodestyle warnings +] + +ignore = [ + "ANN201", # Missing return type annotation for public function + "ANN204", # Missing return type annotation for special method `__init__` + "ANN401", # Dynamically typed expressions (typing.Any) are disallowed in `**kwargs` + "E501", # line too long (handled by black) + # "E203", # whitespace before ':' + # "E226", # missing whitespace around arithmetic operator + # "E24", # multiple spaces after ',' +] + +[tool.ruff.lint.per-file-ignores] +# Ignore `E402` (import violations) in all `__init__.py` files +"__init__.py" = ["E402"] +# Ignore rules in tests +"tests/test_*.py" = ["ANN001", "ANN2", "S101", "T20"] + +[tool.ruff.lint.pydocstyle] +convention = "google" + # Pytest configuration [tool.pytest.ini_options] addopts = [ diff --git a/tests/base.py b/tests/base.py index 0f79556..e68dd6c 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Base class for unit tests.""" diff --git a/tests/test_build.py b/tests/test_build.py index 5a58b74..9f9a961 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_build_no_git.py b/tests/test_build_no_git.py index 0fa3272..a66b4bb 100644 --- a/tests/test_build_no_git.py +++ b/tests/test_build_no_git.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_config.py b/tests/test_config.py index 0f4299e..8db2ef2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_integrations_material.py b/tests/test_integrations_material.py index bb12b32..6c5ba7d 100644 --- a/tests/test_integrations_material.py +++ b/tests/test_integrations_material.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_integrations_material_blog.py b/tests/test_integrations_material_blog.py index 5b84e48..5a7bfe0 100644 --- a/tests/test_integrations_material_blog.py +++ b/tests/test_integrations_material_blog.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_integrations_material_social_cards.py b/tests/test_integrations_material_social_cards.py index 314382d..18299e3 100644 --- a/tests/test_integrations_material_social_cards.py +++ b/tests/test_integrations_material_social_cards.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_no_material.py b/tests/test_no_material.py index 8249f35..66088e6 100644 --- a/tests/test_no_material.py +++ b/tests/test_no_material.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Test build without Material theme installed. diff --git a/tests/test_rss_util.py b/tests/test_rss_util.py index e069f45..30b6188 100644 --- a/tests/test_rss_util.py +++ b/tests/test_rss_util.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: diff --git a/tests/test_timezoner.py b/tests/test_timezoner.py index f4a0d39..f9c9173 100644 --- a/tests/test_timezoner.py +++ b/tests/test_timezoner.py @@ -1,4 +1,4 @@ -#! python3 # noqa E265 +#! python3 # noqa: E265 """Usage from the repo root folder: