From be32706f0d0e4ea039ecf7b7cf91f9be4ea375a0 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 18:57:59 +0000 Subject: [PATCH 01/10] style: fix PD011 --- gitopscli/commands/deploy.py | 2 +- pyproject.toml | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/gitopscli/commands/deploy.py b/gitopscli/commands/deploy.py index 12b666c2..d72324f9 100644 --- a/gitopscli/commands/deploy.py +++ b/gitopscli/commands/deploy.py @@ -80,7 +80,7 @@ def __update_values(self, git_repo: GitRepo) -> dict[str, Any]: single_commit = args.single_commit or args.commit_message full_file_path = git_repo.get_full_file_path(args.file) updated_values = {} - for key, value in args.values.items(): # noqa: PD011 + for key, value in args.values.items(): try: updated_value = update_yaml_file(full_file_path, key, value) except (FileNotFoundError, IsADirectoryError) as ex: diff --git a/pyproject.toml b/pyproject.toml index aee27584..6ec6ad9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,13 +46,21 @@ target-version = "py311" [tool.ruff.lint] select = ["ALL"] -ignore = ["ANN101", "ANN401", -"C901", "PLR0913", # ignore complexity -"D", -"COM812", "ISC001", -"EM101", "EM102", -"S101", "TD", "TRY003"] - +ignore = [ + "ANN101", + "ANN401", + "C901", + "PLR0913", # ignore complexity + "D", + "COM812", + "ISC001", + "EM101", + "EM102", + "S101", + "TD", + "TRY003", + "PD", # false positives; not using Pandas +] [tool.ruff.per-file-ignores] "**/__init__.py" = ["F401"] "tests/**/*.py" = ["S106", "S108", "ANN", "PT009"] From 973a069138911dac61816bcce2266593f32e0896 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 18:58:39 +0000 Subject: [PATCH 02/10] style: fix FIX002 --- gitopscli/commands/sync_apps.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gitopscli/commands/sync_apps.py b/gitopscli/commands/sync_apps.py index 7b290c2a..0bd30c39 100644 --- a/gitopscli/commands/sync_apps.py +++ b/gitopscli/commands/sync_apps.py @@ -47,7 +47,6 @@ def _sync_apps_command(args: SyncAppsCommand.Args) -> None: ) -# TODO: BETTER NAMES FOR STUFF HERE # noqa: FIX002 def __sync_apps( tenant_git_repo: GitRepo, root_git_repo: GitRepo, From 6518a18d70510adb6292725c741f2982ff466cf3 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 18:59:00 +0000 Subject: [PATCH 03/10] style: fix ARG002 --- gitopscli/git_api/bitbucket_git_repo_api_adapter.py | 4 ++-- gitopscli/git_api/git_repo_api_logging_proxy.py | 2 +- gitopscli/git_api/github_git_repo_api_adapter.py | 4 ++-- gitopscli/git_api/gitlab_git_repo_api_adapter.py | 2 +- tests/git_api/test_git_repo.py | 6 ++---- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/gitopscli/git_api/bitbucket_git_repo_api_adapter.py b/gitopscli/git_api/bitbucket_git_repo_api_adapter.py index 4cd6c5b2..3161c6eb 100644 --- a/gitopscli/git_api/bitbucket_git_repo_api_adapter.py +++ b/gitopscli/git_api/bitbucket_git_repo_api_adapter.py @@ -85,8 +85,8 @@ def create_pull_request( def merge_pull_request( self, pr_id: int, - merge_method: Literal["squash", "rebase", "merge"] = "merge", # noqa: ARG002 - merge_parameters: dict[str, Any] | None = None, # noqa: ARG002 + _merge_method: Literal["squash", "rebase", "merge"] = "merge", + _merge_parameters: dict[str, Any] | None = None, ) -> None: pull_request = self.__bitbucket.get_pullrequest(self.__organisation, self.__repository_name, pr_id) self.__bitbucket.merge_pull_request( diff --git a/gitopscli/git_api/git_repo_api_logging_proxy.py b/gitopscli/git_api/git_repo_api_logging_proxy.py index a455d9f0..dd2691d2 100644 --- a/gitopscli/git_api/git_repo_api_logging_proxy.py +++ b/gitopscli/git_api/git_repo_api_logging_proxy.py @@ -40,7 +40,7 @@ def merge_pull_request( self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge", - merge_parameters: dict[str, Any] | None = None, # noqa: ARG002 + _merge_parameters: dict[str, Any] | None = None, ) -> None: logging.info("Merging pull request %s", pr_id) self.__api.merge_pull_request(pr_id, merge_method=merge_method) diff --git a/gitopscli/git_api/github_git_repo_api_adapter.py b/gitopscli/git_api/github_git_repo_api_adapter.py index 0b22bd73..553ebb25 100644 --- a/gitopscli/git_api/github_git_repo_api_adapter.py +++ b/gitopscli/git_api/github_git_repo_api_adapter.py @@ -61,7 +61,7 @@ def merge_pull_request( self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge", - merge_parameters: dict[str, Any] | None = None, # noqa: ARG002 + _merge_parameters: dict[str, Any] | None = None, ) -> None: pull_request = self.__get_pull_request(pr_id) pull_request.merge(merge_method=merge_method) @@ -70,7 +70,7 @@ def add_pull_request_comment( self, pr_id: int, text: str, - parent_id: int | None = None, # noqa: ARG002 + _parent_id: int | None = None, ) -> None: pull_request = self.__get_pull_request(pr_id) pull_request.create_issue_comment(text) diff --git a/gitopscli/git_api/gitlab_git_repo_api_adapter.py b/gitopscli/git_api/gitlab_git_repo_api_adapter.py index afa525b8..e3285689 100644 --- a/gitopscli/git_api/gitlab_git_repo_api_adapter.py +++ b/gitopscli/git_api/gitlab_git_repo_api_adapter.py @@ -100,7 +100,7 @@ def add_pull_request_comment( self, pr_id: int, text: str, - parent_id: int | None = None, # noqa:ARG002 + _parent_id: int | None = None, ) -> None: merge_request = self.__project.mergerequests.get(pr_id) merge_request.notes.create({"body": text}) diff --git a/tests/git_api/test_git_repo.py b/tests/git_api/test_git_repo.py index 77087df6..750398eb 100644 --- a/tests/git_api/test_git_repo.py +++ b/tests/git_api/test_git_repo.py @@ -264,8 +264,7 @@ def test_commit_with_custom_author(self, logging_mock): self.assertIn("README.md", commits[0].stats.files) logging_mock.info.assert_called_once_with("Creating commit with message: %s", "new commit") - @patch("gitopscli.git_api.git_repo.logging") - def test_commit_with_custom_author_name_but_no_email_returns_validation_error(self, logging_mock): # noqa: ARG002 + def test_commit_with_custom_author_name_but_no_email_returns_validation_error(self): with GitRepo(self.__mock_repo_api) as testee: with pytest.raises(GitOpsException) as ex: testee.commit( @@ -279,8 +278,7 @@ def test_commit_with_custom_author_name_but_no_email_returns_validation_error(se "Please provide the name and email address of the Git author or provide neither!" ) - @patch("gitopscli.git_api.git_repo.logging") - def test_commit_with_custom_author_email_but_no_name_returns_validation_error(self, logging_mock): # noqa: ARG002 + def test_commit_with_custom_author_email_but_no_name_returns_validation_error(self): with GitRepo(self.__mock_repo_api) as testee: with pytest.raises(GitOpsException) as ex: testee.commit( From fbecaf9fe5537d045db282cdf4ed7e3927857dc6 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:02:55 +0000 Subject: [PATCH 04/10] style: fix TRY300 --- gitopscli/git_api/gitlab_git_repo_api_adapter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gitopscli/git_api/gitlab_git_repo_api_adapter.py b/gitopscli/git_api/gitlab_git_repo_api_adapter.py index e3285689..d286f44c 100644 --- a/gitopscli/git_api/gitlab_git_repo_api_adapter.py +++ b/gitopscli/git_api/gitlab_git_repo_api_adapter.py @@ -82,7 +82,6 @@ def merge_pull_request( merge_request.rebase(merge_parameters) return merge_request.merge(merge_parameters) - return # noqa: TRY300 except gitlab.exceptions.GitlabMRClosedError as ex: # "Branch cannot be merged" error can occur if the server # is still processing the merge request internally @@ -95,6 +94,8 @@ def merge_pull_request( if max_retries == 0: raise GitOpsException("Error merging pull request: 'Branch cannot be merged'") from ex time.sleep(2.5) + else: + return def add_pull_request_comment( self, From 20a611e5f8fe6a788952f72bf286b900df1855a1 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:07:06 +0000 Subject: [PATCH 05/10] style: fix PLR2004 --- Makefile | 1 + gitopscli/git_api/gitlab_git_repo_api_adapter.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4c565100..4df25d14 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ init: format: poetry run ruff format gitopscli tests + poetry run ruff gitopscli tests --fix format-check: poetry run ruff format gitopscli tests --check diff --git a/gitopscli/git_api/gitlab_git_repo_api_adapter.py b/gitopscli/git_api/gitlab_git_repo_api_adapter.py index d286f44c..a1b7e09f 100644 --- a/gitopscli/git_api/gitlab_git_repo_api_adapter.py +++ b/gitopscli/git_api/gitlab_git_repo_api_adapter.py @@ -1,5 +1,6 @@ import logging import time +from http import HTTPStatus from typing import Any, Literal import gitlab @@ -29,7 +30,7 @@ def __init__( except gitlab.exceptions.GitlabAuthenticationError as ex: raise GitOpsException("Bad Personal Access Token") from ex except gitlab.exceptions.GitlabGetError as ex: - if ex.response_code == 404: # noqa: PLR2004 + if ex.response_code == HTTPStatus.NOT_FOUND: raise GitOpsException(f"Repository '{organisation}/{repository_name}' does not exist") from ex raise GitOpsException(f"Error getting repository: '{ex.error_message}'") from ex From a43b2bd78947cfddfb710c970c16f0b07ec808e1 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:09:16 +0000 Subject: [PATCH 06/10] style: fix PLW2901 --- gitopscli/io_api/yaml_util.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gitopscli/io_api/yaml_util.py b/gitopscli/io_api/yaml_util.py index 51536105..aa62cea8 100644 --- a/gitopscli/io_api/yaml_util.py +++ b/gitopscli/io_api/yaml_util.py @@ -73,9 +73,10 @@ def merge_yaml_element(file_path: str, element_path: str, desired_value: Any) -> work_path = work_path[key] for key, value in desired_value.items(): + tmp_value = value if key in work_path and work_path[key] is not None: - value = {**work_path[key], **value} # noqa: PLW2901 - work_path[key] = value + tmp_value = {**work_path[key], **tmp_value} + work_path[key] = tmp_value # delete missing key: current = work_path.copy().items() From 362a719471357470b6818444c1eca98f8583513b Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:12:05 +0000 Subject: [PATCH 07/10] style: fix B024 --- tests/commands/mock_mixin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/commands/mock_mixin.py b/tests/commands/mock_mixin.py index 323f4b94..4d95e37d 100644 --- a/tests/commands/mock_mixin.py +++ b/tests/commands/mock_mixin.py @@ -1,8 +1,7 @@ -from abc import ABC from unittest.mock import MagicMock, patch, seal -class MockMixin(ABC): # noqa: B024 +class MockMixin: def init_mock_manager(self, command_class: type) -> None: self.command_class = command_class self.mock_manager = MagicMock() From 7db62da443a32b412cdd8254c2c61304bfb64268 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:21:33 +0000 Subject: [PATCH 08/10] style: fix C901 --- .../git_api/bitbucket_git_repo_api_adapter.py | 19 +++++++++++-------- pyproject.toml | 6 +++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/gitopscli/git_api/bitbucket_git_repo_api_adapter.py b/gitopscli/git_api/bitbucket_git_repo_api_adapter.py index 3161c6eb..ceb0edc6 100644 --- a/gitopscli/git_api/bitbucket_git_repo_api_adapter.py +++ b/gitopscli/git_api/bitbucket_git_repo_api_adapter.py @@ -35,14 +35,7 @@ def get_clone_url(self) -> str: raise GitOpsException(f"Error connecting to '{self.__git_provider_url}''") from ex if "errors" in repo: for error in repo["errors"]: - exception = error["exceptionName"] - if exception == "com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException": - raise GitOpsException("Bad credentials") - if exception == "com.atlassian.bitbucket.project.NoSuchProjectException": - raise GitOpsException(f"Organisation '{self.__organisation}' does not exist") - if exception == "com.atlassian.bitbucket.repository.NoSuchRepositoryException": - raise GitOpsException(f"Repository '{self.__organisation}/{self.__repository_name}' does not exist") - raise GitOpsException(error["message"]) + raise self.__map_clone_error(error) if "links" not in repo: raise GitOpsException(f"Repository '{self.__organisation}/{self.__repository_name}' does not exist") for clone_link in repo["links"]["clone"]: @@ -52,6 +45,16 @@ def get_clone_url(self) -> str: raise GitOpsException("Couldn't determine repository URL.") return str(repo_url) + def __map_clone_error(self, error: dict[str, str]) -> GitOpsException: + exception = error["exceptionName"] + if exception == "com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException": + return GitOpsException("Bad credentials") + if exception == "com.atlassian.bitbucket.project.NoSuchProjectException": + return GitOpsException(f"Organisation '{self.__organisation}' does not exist") + if exception == "com.atlassian.bitbucket.repository.NoSuchRepositoryException": + return GitOpsException(f"Repository '{self.__organisation}/{self.__repository_name}' does not exist") + return GitOpsException(error["message"]) + def create_pull_request_to_default_branch( self, from_branch: str, diff --git a/pyproject.toml b/pyproject.toml index 6ec6ad9b..7c608be9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,9 +47,9 @@ target-version = "py311" [tool.ruff.lint] select = ["ALL"] ignore = [ - "ANN101", - "ANN401", - "C901", + "ANN101", # https://docs.astral.sh/ruff/rules/missing-type-self/ + "ANN401", # https://docs.astral.sh/ruff/rules/any-type/ + "C901", # https://docs.astral.sh/ruff/rules/complex-structure/ "PLR0913", # ignore complexity "D", "COM812", From 0933eb73699d394943641d3a739742785055023c Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:34:08 +0000 Subject: [PATCH 09/10] style: fix FIX002 --- gitopscli/appconfig_api/app_tenant_config.py | 2 +- pyproject.toml | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/gitopscli/appconfig_api/app_tenant_config.py b/gitopscli/appconfig_api/app_tenant_config.py index 7efc191f..f88cab25 100644 --- a/gitopscli/appconfig_api/app_tenant_config.py +++ b/gitopscli/appconfig_api/app_tenant_config.py @@ -76,7 +76,7 @@ def __set_dirty(self) -> None: def __generate_config_from_tenant_repo( tenant_repo: GitRepo, -) -> Any: # TODO: supposed to be ruamel object than Any # noqa: FIX002 +) -> Any: tenant_app_dirs = __get_all_tenant_applications_dirs(tenant_repo) tenant_config_template = f""" config: diff --git a/pyproject.toml b/pyproject.toml index 7c608be9..6a107899 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,14 +50,12 @@ ignore = [ "ANN101", # https://docs.astral.sh/ruff/rules/missing-type-self/ "ANN401", # https://docs.astral.sh/ruff/rules/any-type/ "C901", # https://docs.astral.sh/ruff/rules/complex-structure/ - "PLR0913", # ignore complexity - "D", - "COM812", - "ISC001", - "EM101", - "EM102", - "S101", - "TD", + "PLR0913", # https://docs.astral.sh/ruff/rules/too-many-arguments/ + "D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d + "COM812", # https://docs.astral.sh/ruff/rules/missing-trailing-comma/ (clashes with formatter) + "EM101", # https://docs.astral.sh/ruff/rules/raw-string-in-exception/ + "EM102", # https://docs.astral.sh/ruff/rules/f-string-in-exception/ + "S101", # https://docs.astral.sh/ruff/rules/assert/ "TRY003", "PD", # false positives; not using Pandas ] From 8af3f909487ffc0582b6cf3fb78e34c16e77d838 Mon Sep 17 00:00:00 2001 From: Christian Siegel Date: Wed, 8 Nov 2023 19:38:19 +0000 Subject: [PATCH 10/10] style: add some links to lint exceptions --- pyproject.toml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6a107899..06a35bc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,12 +56,17 @@ ignore = [ "EM101", # https://docs.astral.sh/ruff/rules/raw-string-in-exception/ "EM102", # https://docs.astral.sh/ruff/rules/f-string-in-exception/ "S101", # https://docs.astral.sh/ruff/rules/assert/ - "TRY003", - "PD", # false positives; not using Pandas + "TRY003", # https://docs.astral.sh/ruff/rules/raise-vanilla-args/ + "PD", # https://docs.astral.sh/ruff/rules/#pandas-vet-pd (false positives) ] [tool.ruff.per-file-ignores] "**/__init__.py" = ["F401"] -"tests/**/*.py" = ["S106", "S108", "ANN", "PT009"] +"tests/**/*.py" = [ + "S106", # https://docs.astral.sh/ruff/rules/hardcoded-password-func-arg/ + "S108", # https://docs.astral.sh/ruff/rules/hardcoded-temp-file/ + "ANN", # https://docs.astral.sh/ruff/rules/#flake8-annotations-ann + "PT009" # https://docs.astral.sh/ruff/rules/pytest-unittest-assertion/ +] # the following exclusions have been introduced to prevent huge changes # feel free to remove them and fix the code "gitopscli/appconfig_api/app_tenant_config.py" = ["PTH110", "PTH112", "PTH118"]