Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: update tool.nbqa.addopts settings #147

Merged
merged 5 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/repoma/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
cron_frequency=args.pin_requirements,
)
executor(mypy.main)
executor(pytest.main)
executor(pyright.main)
executor(pytest.main)
executor(pyupgrade.main)
executor(ruff.main)
executor(setup_cfg.main, args.ignore_author)
Expand Down
29 changes: 2 additions & 27 deletions src/repoma/check_dev_files/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from repoma.utilities import CONFIG_PATH
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import (
find_repo,
load_round_trip_precommit_config,
update_precommit_hook,
update_single_hook_precommit_repo,
)
Expand All @@ -16,6 +14,7 @@
get_sub_table,
load_pyproject,
to_toml_array,
update_nbqa_settings,
write_pyproject,
)
from repoma.utilities.setup_cfg import get_supported_python_versions
Expand All @@ -27,9 +26,9 @@ def main() -> None:
executor = Executor()
executor(_remove_outdated_settings)
executor(_update_black_settings)
executor(_update_nbqa_settings)
executor(_update_precommit_repo)
executor(_update_precommit_nbqa_hook)
executor(update_nbqa_settings, "black", to_toml_array(["--line-length=85"]))
executor.finalize()


Expand Down Expand Up @@ -83,27 +82,3 @@ def _update_precommit_nbqa_hook() -> None:
additional_dependencies=["black>=22.1.0"],
),
)


def _update_nbqa_settings() -> None:
# cspell:ignore addopts
if not CONFIG_PATH.precommit.exists():
return
if not __has_nbqa_precommit_repo():
return
pyproject = load_pyproject()
nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
expected = ["--line-length=85"]
if nbqa_table.get("black") != expected:
nbqa_table["black"] = expected
write_pyproject(pyproject)
msg = "Added nbQA configuration for black"
raise PrecommitError(msg)


def __has_nbqa_precommit_repo() -> bool:
config, _ = load_round_trip_precommit_config()
nbqa_repo = find_repo(config, "https://github.com/nbQA-dev/nbQA")
if nbqa_repo is None:
return False
return True
41 changes: 30 additions & 11 deletions src/repoma/check_dev_files/deprecated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Remove deprecated linters and formatters."""
import os
from typing import List
from typing import TYPE_CHECKING, List

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
Expand All @@ -15,6 +15,9 @@
set_setting,
)

if TYPE_CHECKING:
from tomlkit.items import Table


def remove_deprecated_tools() -> None:
executor = Executor()
Expand All @@ -28,6 +31,7 @@ def remove_deprecated_tools() -> None:
def _remove_flake8() -> None:
executor = Executor()
executor(__remove_configs, [".flake8"])
executor(__remove_nbqa_option, "flake8")
executor(__uninstall, "flake8", check_options=["lint", "sty"])
executor(__uninstall, "pep8-naming", check_options=["lint", "sty"])
executor(remove_extension_recommendation, "ms-python.flake8", unwanted=True)
Expand All @@ -42,6 +46,7 @@ def _remove_flake8() -> None:
def _remove_isort() -> None:
executor = Executor()
executor(__remove_isort_settings)
executor(__remove_nbqa_option, "isort")
executor(remove_extension_recommendation, "ms-python.isort", unwanted=True)
executor(remove_precommit_hook, "isort")
executor(remove_precommit_hook, "nbqa-isort")
Expand All @@ -50,6 +55,30 @@ def _remove_isort() -> None:
executor.finalize()


def __remove_isort_settings() -> None:
pyproject = load_pyproject()
if pyproject.get("tool", {}).get("isort") is None:
return
pyproject["tool"].remove("isort") # type: ignore[union-attr]
write_pyproject(pyproject)
msg = f"Removed [tool.isort] section from {CONFIG_PATH.pyproject}"
raise PrecommitError(msg)


def __remove_nbqa_option(option: str) -> None:
pyproject = load_pyproject()
# cspell:ignore addopts
nbqa_table: Table = pyproject.get("tool", {}).get("nbqa", {}).get("addopts")
if nbqa_table is None:
return
if nbqa_table.get(option) is None:
return
nbqa_table.remove(option)
write_pyproject(pyproject)
msg = f"Removed {option!r} nbQA options from {CONFIG_PATH.pyproject}"
raise PrecommitError(msg)


def _remove_pydocstyle() -> None:
executor = Executor()
executor(
Expand Down Expand Up @@ -77,16 +106,6 @@ def _remove_pylint() -> None:
executor.finalize()


def __remove_isort_settings() -> None:
pyproject = load_pyproject()
if pyproject.get("tool", {}).get("isort") is None:
return
pyproject["tool"].remove("isort") # type: ignore[union-attr]
write_pyproject(pyproject)
msg = f"Removed [tool.isort] section from {CONFIG_PATH.pyproject}"
raise PrecommitError(msg)


def __remove_configs(paths: List[str]) -> None:
executor = Executor()
for path in paths:
Expand Down
2 changes: 2 additions & 0 deletions src/repoma/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_sub_table,
load_pyproject,
to_toml_array,
update_nbqa_settings,
write_pyproject,
)
from repoma.utilities.readme import add_badge
Expand All @@ -40,6 +41,7 @@ def main() -> None:
executor(_update_precommit_hook)
executor(_update_precommit_nbqa_hook)
executor(_update_vscode_settings)
executor(update_nbqa_settings, "ruff", to_toml_array(["--line-length=85"]))
executor.finalize()


Expand Down
27 changes: 27 additions & 0 deletions src/repoma/utilities/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from tomlkit.items import Array, Table
from tomlkit.toml_document import TOMLDocument

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
from repoma.utilities.precommit import find_repo, load_round_trip_precommit_config


def complies_with_subset(settings: dict, minimal_settings: dict) -> bool:
Expand Down Expand Up @@ -48,4 +50,29 @@ def to_toml_array(items: Iterable[Any]) -> Array:
array.extend(items)
if len(array) > 1:
array.multiline(True)
else:
array.multiline(False)
return array


def update_nbqa_settings(key: str, expected: Any) -> None:
# cspell:ignore addopts
if not CONFIG_PATH.precommit.exists():
return
if not __has_nbqa_precommit_repo():
return
pyproject = load_pyproject()
nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
if nbqa_table.get(key) != expected:
nbqa_table[key] = expected
write_pyproject(pyproject)
msg = f"Added nbQA configuration for {key!r}"
raise PrecommitError(msg)


def __has_nbqa_precommit_repo() -> bool:
config, _ = load_round_trip_precommit_config()
nbqa_repo = find_repo(config, "https://github.com/nbQA-dev/nbQA")
if nbqa_repo is None:
return False
return True