From ce9655d5dc4eb06b10655b4393c51adafca0c802 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 24 Sep 2023 22:57:21 +0200 Subject: [PATCH] ci(ruff): enable the perflint and ruff rules * enable the performance rules in ruff * enable the Ruff-specific rules in ruff * disable check for ambiguous characters in comments; I intend to use guillemets Signed-off-by: Matej Focko --- ogr/abstract.py | 27 ++++++++-------- ogr/factory.py | 8 ++--- ogr/read_only.py | 8 ++--- ogr/services/base.py | 8 ++--- .../github/auth_providers/github_app.py | 6 ++-- ogr/services/github/auth_providers/token.py | 2 +- ogr/services/github/auth_providers/tokman.py | 5 ++- ogr/services/github/flag.py | 3 +- ogr/services/github/project.py | 14 ++++---- ogr/services/github/pull_request.py | 2 +- ogr/services/github/service.py | 18 +++++------ ogr/services/gitlab/flag.py | 3 +- ogr/services/gitlab/project.py | 10 +++--- ogr/services/gitlab/pull_request.py | 6 ++-- ogr/services/gitlab/service.py | 8 ++--- ogr/services/pagure/flag.py | 8 ++--- ogr/services/pagure/project.py | 32 +++++++++---------- ogr/services/pagure/pull_request.py | 2 +- ogr/services/pagure/service.py | 18 +++++------ pyproject.toml | 11 ++++--- .../integration/github/test_pull_requests.py | 11 ++----- 21 files changed, 105 insertions(+), 105 deletions(-) diff --git a/ogr/abstract.py b/ogr/abstract.py index 6a493cb2..4d4391bf 100644 --- a/ogr/abstract.py +++ b/ogr/abstract.py @@ -9,6 +9,7 @@ from typing import ( Any, Callable, + ClassVar, Optional, TypeVar, Union, @@ -152,7 +153,7 @@ def __new__(cls, name, bases, namespace): class OgrAbstractClass(metaclass=CatchCommonErrors): def __repr__(self) -> str: - return f"<{str(self)}>" + return f"<{self!s}>" class Reaction(OgrAbstractClass): @@ -441,9 +442,9 @@ def _get_all_comments(self) -> list[IssueComment]: def get_comments( self, - filter_regex: str = None, + filter_regex: Optional[str] = None, reverse: bool = False, - author: str = None, + author: Optional[str] = None, ) -> list[IssueComment]: """ Get list of issue comments. @@ -706,7 +707,7 @@ def create( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": """ Create new pull request. @@ -918,7 +919,7 @@ def get_comment(self, comment_id: int) -> PRComment: class CommitFlag(OgrAbstractClass): - _states: dict[str, CommitStatus] = {} + _states: ClassVar[dict[str, CommitStatus]] = {} def __init__( self, @@ -1351,10 +1352,10 @@ def project_create( def list_projects( self, - namespace: str = None, - user: str = None, - search_pattern: str = None, - language: str = None, + namespace: Optional[str] = None, + user: Optional[str] = None, + search_pattern: Optional[str] = None, + language: Optional[str] = None, ) -> list["GitProject"]: """ List projects for given criteria. @@ -1773,7 +1774,7 @@ def create_pr( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": """ Create new pull request. @@ -1904,7 +1905,7 @@ def change_token(self, new_token: str) -> None: """ raise NotImplementedError - def get_file_content(self, path: str, ref: str = None) -> str: + def get_file_content(self, path: str, ref: Optional[str] = None) -> str: """ Get a content of the file in the repo. @@ -1924,8 +1925,8 @@ def get_file_content(self, path: str, ref: str = None) -> str: def get_files( self, - ref: str = None, - filter_regex: str = None, + ref: Optional[str] = None, + filter_regex: Optional[str] = None, recursive: bool = False, ) -> list[str]: """ diff --git a/ogr/factory.py b/ogr/factory.py index 81829fce..e2ccc154 100644 --- a/ogr/factory.py +++ b/ogr/factory.py @@ -54,8 +54,8 @@ def covered_func(kls: type[GitService]): def get_project( url, - service_mapping_update: dict[str, type[GitService]] = None, - custom_instances: Iterable[GitService] = None, + service_mapping_update: Optional[dict[str, type[GitService]]] = None, + custom_instances: Optional[Iterable[GitService]] = None, force_custom_instance: bool = True, **kwargs, ) -> GitProject: @@ -112,7 +112,7 @@ def get_project( def get_service_class_or_none( url: str, - service_mapping_update: dict[str, type[GitService]] = None, + service_mapping_update: Optional[dict[str, type[GitService]]] = None, ) -> Optional[type[GitService]]: """ Get the matching service class from the URL. @@ -142,7 +142,7 @@ def get_service_class_or_none( def get_service_class( url: str, - service_mapping_update: dict[str, type[GitService]] = None, + service_mapping_update: Optional[dict[str, type[GitService]]] = None, ) -> type[GitService]: """ Get the matching service class from the URL. diff --git a/ogr/read_only.py b/ogr/read_only.py index 46313792..66bb6c33 100644 --- a/ogr/read_only.py +++ b/ogr/read_only.py @@ -174,7 +174,7 @@ def create_pr( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": return PullRequestReadOnly( title=title, @@ -194,9 +194,9 @@ def pr_comment( original_object: Any, pr_id: int, body: str, - commit: str = None, - filename: str = None, - row: int = None, + commit: Optional[str] = None, + filename: Optional[str] = None, + row: Optional[int] = None, ) -> "PRComment": pull_request = original_object.get_pr(pr_id) log_output(pull_request) diff --git a/ogr/services/base.py b/ogr/services/base.py index 02f38886..6c89637e 100644 --- a/ogr/services/base.py +++ b/ogr/services/base.py @@ -54,9 +54,9 @@ def target_branch_head_commit(self) -> str: def get_comments( self, - filter_regex: str = None, + filter_regex: Optional[str] = None, reverse: bool = False, - author: str = None, + author: Optional[str] = None, ): all_comments = self._get_all_comments() return filter_comments(all_comments, filter_regex, reverse, author) @@ -89,9 +89,9 @@ class BaseGitUser(GitUser): class BaseIssue(Issue): def get_comments( self, - filter_regex: str = None, + filter_regex: Optional[str] = None, reverse: bool = False, - author: str = None, + author: Optional[str] = None, ) -> list[IssueComment]: all_comments: list[IssueComment] = self._get_all_comments() return filter_comments(all_comments, filter_regex, reverse, author) diff --git a/ogr/services/github/auth_providers/github_app.py b/ogr/services/github/auth_providers/github_app.py index 9fa3f992..25633540 100644 --- a/ogr/services/github/auth_providers/github_app.py +++ b/ogr/services/github/auth_providers/github_app.py @@ -104,9 +104,9 @@ def get_token(self, namespace: str, repo: str) -> str: @staticmethod def try_create( - github_app_id: str = None, - github_app_private_key: str = None, - github_app_private_key_path: str = None, + github_app_id: Optional[str] = None, + github_app_private_key: Optional[str] = None, + github_app_private_key_path: Optional[str] = None, **_, ) -> Optional["GithubApp"]: return ( diff --git a/ogr/services/github/auth_providers/token.py b/ogr/services/github/auth_providers/token.py index e6172d9e..392d255c 100644 --- a/ogr/services/github/auth_providers/token.py +++ b/ogr/services/github/auth_providers/token.py @@ -34,7 +34,7 @@ def get_token(self, namespace: str, repo: str) -> str: @staticmethod def try_create( - token: str = None, + token: Optional[str] = None, max_retries: Union[int, Retry] = 0, **_, ) -> Optional["TokenAuthentication"]: diff --git a/ogr/services/github/auth_providers/tokman.py b/ogr/services/github/auth_providers/tokman.py index c261a1e9..a47d28eb 100644 --- a/ogr/services/github/auth_providers/tokman.py +++ b/ogr/services/github/auth_providers/tokman.py @@ -43,5 +43,8 @@ def get_token(self, namespace: str, repo: str) -> str: return response.json().get("access_token", None) @staticmethod - def try_create(tokman_instance_url: str = None, **_) -> Optional["Tokman"]: + def try_create( + tokman_instance_url: Optional[str] = None, + **_, + ) -> Optional["Tokman"]: return Tokman(tokman_instance_url) if tokman_instance_url else None diff --git a/ogr/services/github/flag.py b/ogr/services/github/flag.py index df20a5f6..b94405f8 100644 --- a/ogr/services/github/flag.py +++ b/ogr/services/github/flag.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT import datetime +from typing import ClassVar from github import UnknownObjectException @@ -11,7 +12,7 @@ class GithubCommitFlag(BaseCommitFlag): - _states = { + _states: ClassVar[dict[str, CommitStatus]] = { "pending": CommitStatus.pending, "success": CommitStatus.success, "failure": CommitStatus.failure, diff --git a/ogr/services/github/project.py b/ogr/services/github/project.py index f017a23d..881df96e 100644 --- a/ogr/services/github/project.py +++ b/ogr/services/github/project.py @@ -3,7 +3,7 @@ import datetime import logging -from typing import Optional, Union +from typing import ClassVar, Optional, Union import github from github import UnknownObjectException @@ -46,7 +46,7 @@ class GithubProject(BaseGitProject): service: "ogr_github.GithubService" # Permission levels that can merge PRs - CAN_MERGE_PERMS = ["admin", "write"] + CAN_MERGE_PERMS: ClassVar[set[str]] = {"admin", "write"} def __init__( self, @@ -319,7 +319,7 @@ def create_pr( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> PullRequest: pass @@ -331,8 +331,8 @@ def commit_comment( self, commit: str, body: str, - filename: str = None, - row: int = None, + filename: Optional[str] = None, + row: Optional[int] = None, ) -> CommitComment: github_commit: Commit = self.github_repo.get_commit(commit) if filename and row: @@ -447,8 +447,8 @@ def get_file_content(self, path: str, ref=None) -> str: def get_files( self, - ref: str = None, - filter_regex: str = None, + ref: Optional[str] = None, + filter_regex: Optional[str] = None, recursive: bool = False, ) -> list[str]: ref = ref or self.default_branch diff --git a/ogr/services/github/pull_request.py b/ogr/services/github/pull_request.py index c1f9c4bc..0c964bd9 100644 --- a/ogr/services/github/pull_request.py +++ b/ogr/services/github/pull_request.py @@ -136,7 +136,7 @@ def create( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": """ The default behavior is the pull request is made to the immediate parent repository diff --git a/ogr/services/github/service.py b/ogr/services/github/service.py index f569e7b8..b4452294 100644 --- a/ogr/services/github/service.py +++ b/ogr/services/github/service.py @@ -44,10 +44,10 @@ def __init__( self, token=None, read_only=False, - github_app_id: str = None, - github_app_private_key: str = None, - github_app_private_key_path: str = None, - tokman_instance_url: str = None, + github_app_id: Optional[str] = None, + github_app_private_key: Optional[str] = None, + github_app_private_key_path: Optional[str] = None, + tokman_instance_url: Optional[str] = None, github_authentication: GithubAuthentication = None, max_retries: Union[int, Retry] = 0, **kwargs, @@ -127,7 +127,7 @@ def github(self): def __str__(self) -> str: readonly_str = ", read_only=True" if self.read_only else "" - arguments = f", github_authentication={str(self.authentication)}{readonly_str}" + arguments = f", github_authentication={self.authentication!s}{readonly_str}" if arguments: # remove the first '- ' @@ -217,10 +217,10 @@ def get_pygithub_instance(self, namespace: str, repo: str) -> PyGithubInstance: def list_projects( self, - namespace: str = None, - user: str = None, - search_pattern: str = None, - language: str = None, + namespace: Optional[str] = None, + user: Optional[str] = None, + search_pattern: Optional[str] = None, + language: Optional[str] = None, ) -> list[GitProject]: search_query = "" diff --git a/ogr/services/gitlab/flag.py b/ogr/services/gitlab/flag.py index a19c7a62..d8c04261 100644 --- a/ogr/services/gitlab/flag.py +++ b/ogr/services/gitlab/flag.py @@ -3,6 +3,7 @@ import datetime import logging +from typing import ClassVar import gitlab @@ -15,7 +16,7 @@ class GitlabCommitFlag(BaseCommitFlag): - _states = { + _states: ClassVar[dict[str, CommitStatus]] = { "pending": CommitStatus.pending, "success": CommitStatus.success, "failed": CommitStatus.failure, diff --git a/ogr/services/gitlab/project.py b/ogr/services/gitlab/project.py index 012fc98e..40c8b0f0 100644 --- a/ogr/services/gitlab/project.py +++ b/ogr/services/gitlab/project.py @@ -267,7 +267,7 @@ def create_pr( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": pass @@ -275,8 +275,8 @@ def commit_comment( self, commit: str, body: str, - filename: str = None, - row: int = None, + filename: Optional[str] = None, + row: Optional[int] = None, ) -> "CommitComment": try: commit_object: ProjectCommit = self.gitlab_repo.commits.get(commit) @@ -371,8 +371,8 @@ def get_file_content(self, path, ref=None) -> str: def get_files( self, - ref: str = None, - filter_regex: str = None, + ref: Optional[str] = None, + filter_regex: Optional[str] = None, recursive: bool = False, ) -> list[str]: ref = ref or self.default_branch diff --git a/ogr/services/gitlab/pull_request.py b/ogr/services/gitlab/pull_request.py index 425798f2..5c3d810d 100644 --- a/ogr/services/gitlab/pull_request.py +++ b/ogr/services/gitlab/pull_request.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MIT import datetime -from typing import Optional +from typing import ClassVar, Optional import gitlab import requests @@ -20,7 +20,7 @@ class GitlabPullRequest(BasePullRequest): _raw_pr: _GitlabMergeRequest _target_project: "ogr_gitlab.GitlabProject" _source_project: "ogr_gitlab.GitlabProject" = None - _merge_commit_status: dict[str, MergeCommitStatus] = { + _merge_commit_status: ClassVar[dict[str, MergeCommitStatus]] = { "can_be_merged": MergeCommitStatus.can_be_merged, "cannot_be_merged": MergeCommitStatus.cannot_be_merged, "unchecked": MergeCommitStatus.unchecked, @@ -150,7 +150,7 @@ def create( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": """ How to create PR: diff --git a/ogr/services/gitlab/service.py b/ogr/services/gitlab/service.py index 728cce06..25c6e23d 100644 --- a/ogr/services/gitlab/service.py +++ b/ogr/services/gitlab/service.py @@ -136,10 +136,10 @@ def project_create( def list_projects( self, - namespace: str = None, - user: str = None, - search_pattern: str = None, - language: str = None, + namespace: Optional[str] = None, + user: Optional[str] = None, + search_pattern: Optional[str] = None, + language: Optional[str] = None, ) -> list[GitProject]: if namespace: group = self.gitlab_instance.groups.get(namespace) diff --git a/ogr/services/pagure/flag.py b/ogr/services/pagure/flag.py index 55e69d52..d6870fb3 100644 --- a/ogr/services/pagure/flag.py +++ b/ogr/services/pagure/flag.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MIT import datetime -from typing import Any +from typing import Any, ClassVar, Optional from ogr.abstract import CommitFlag, CommitStatus from ogr.services import pagure as ogr_pagure @@ -10,7 +10,7 @@ class PagureCommitFlag(BaseCommitFlag): - _states = { + _states: ClassVar[dict[str, CommitStatus]] = { "pending": CommitStatus.pending, "success": CommitStatus.success, "failure": CommitStatus.failure, @@ -44,9 +44,9 @@ def set( target_url: str, description: str, context: str, - percent: int = None, + percent: Optional[int] = None, trim: bool = False, - uid: str = None, + uid: Optional[str] = None, ) -> "CommitFlag": state = PagureCommitFlag._validate_state(state) diff --git a/ogr/services/pagure/project.py b/ogr/services/pagure/project.py index e12a4774..ca0af027 100644 --- a/ogr/services/pagure/project.py +++ b/ogr/services/pagure/project.py @@ -43,7 +43,7 @@ def __init__( repo: str, namespace: Optional[str], service: "ogr_pagure.PagureService", - username: str = None, + username: Optional[str] = None, is_fork: bool = False, ) -> None: super().__init__(repo, service, namespace) @@ -85,9 +85,9 @@ def _call_project_api( *args, add_fork_part: bool = True, add_api_endpoint_part: bool = True, - method: str = None, - params: dict = None, - data: dict = None, + method: Optional[str] = None, + params: Optional[dict] = None, + data: Optional[dict] = None, ) -> dict: """ Call project API endpoint. @@ -125,9 +125,9 @@ def _call_project_api_raw( *args, add_fork_part: bool = True, add_api_endpoint_part: bool = True, - method: str = None, - params: dict = None, - data: dict = None, + method: Optional[str] = None, + params: Optional[dict] = None, + data: Optional[dict] = None, ) -> RequestResponse: """ Call project API endpoint. @@ -283,7 +283,7 @@ def create_pr( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> PullRequest: pass @@ -453,8 +453,8 @@ def commit_comment( self, commit: str, body: str, - filename: str = None, - row: int = None, + filename: Optional[str] = None, + row: Optional[int] = None, ) -> CommitComment: raise OperationNotSupported("Commit comments are not supported on Pagure.") @@ -470,8 +470,8 @@ def set_commit_status( target_url: str, description: str, context: str, - percent: int = None, - uid: str = None, + percent: Optional[int] = None, + uid: Optional[str] = None, trim: bool = False, ) -> "CommitFlag": pass @@ -539,7 +539,7 @@ def full_repo_name(self) -> str: def __get_files( self, path: str, - ref: str = None, + ref: Optional[str] = None, recursive: bool = False, ) -> Iterable[str]: subfolders = ["."] @@ -548,7 +548,7 @@ def __get_files( path = subfolders.pop() split_path = [] if path != ".": - split_path = ["f"] + path.split("/") + split_path = ["f", *path.split("/")] response = self._call_project_api("tree", ref, *split_path) for file in response["content"]: @@ -559,8 +559,8 @@ def __get_files( def get_files( self, - ref: str = None, - filter_regex: str = None, + ref: Optional[str] = None, + filter_regex: Optional[str] = None, recursive: bool = False, ) -> list[str]: ref = ref or self.default_branch diff --git a/ogr/services/pagure/pull_request.py b/ogr/services/pagure/pull_request.py index c4d67473..66c9e647 100644 --- a/ogr/services/pagure/pull_request.py +++ b/ogr/services/pagure/pull_request.py @@ -149,7 +149,7 @@ def create( body: str, target_branch: str, source_branch: str, - fork_username: str = None, + fork_username: Optional[str] = None, ) -> "PullRequest": data = { "title": title, diff --git a/ogr/services/pagure/service.py b/ogr/services/pagure/service.py index 6a1957e7..617310a1 100644 --- a/ogr/services/pagure/service.py +++ b/ogr/services/pagure/service.py @@ -34,7 +34,7 @@ class PagureService(BaseGitService): def __init__( self, - token: str = None, + token: Optional[str] = None, instance_url: str = "https://src.fedoraproject.org", read_only: bool = False, insecure: bool = False, @@ -122,8 +122,8 @@ def user(self) -> "PagureUser": def call_api( self, url: str, - method: str = None, - params: dict = None, + method: Optional[str] = None, + params: Optional[dict] = None, data=None, ) -> dict: """ @@ -186,8 +186,8 @@ def call_api( def call_api_raw( self, url: str, - method: str = None, - params: dict = None, + method: Optional[str] = None, + params: Optional[dict] = None, data=None, ): """ @@ -367,9 +367,9 @@ def project_create( def list_projects( self, - namespace: str = None, - user: str = None, - search_pattern: str = None, - language: str = None, + namespace: Optional[str] = None, + user: Optional[str] = None, + search_pattern: Optional[str] = None, + language: Optional[str] = None, ) -> list[GitProject]: raise OperationNotSupported diff --git a/pyproject.toml b/pyproject.toml index 96f6b3df..cef0d7cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -74,20 +74,21 @@ select = [ "E", # pycodestyle Error "F", # Pyflakes "I", # isort - # "PERF",# Perflint + "PERF",# Perflint "PIE", # flake8-pie "PT", # flake8-pytest-style "RET", # flake8-return - # "RUF", # Ruff-specific rules + "RUF", # Ruff-specific rules "SIM", # flake8-simplify "UP", # pyupgrade "W", # pycodestyle Warning ] ignore = [ - "B017", # assert-raises-exception - "B022", # useless-contextlib-suppress - "RUF003"# Comment contains ambiguous character + "B017", # assert-raises-exception + "B022", # useless-contextlib-suppress + "RUF002", # you shall not touch my guillemets + "RUF003" # Comment contains ambiguous character ] line-length = 100 diff --git a/tests/integration/github/test_pull_requests.py b/tests/integration/github/test_pull_requests.py index 589f1b1e..e6848293 100644 --- a/tests/integration/github/test_pull_requests.py +++ b/tests/integration/github/test_pull_requests.py @@ -22,19 +22,12 @@ def test_pr_list(self): pr_list_closed = self.ogr_project.get_pr_list(status=PRStatus.closed) assert pr_list_closed assert len(pr_list_closed) >= 70 - - closed_pr_numbers = [] - for closed_pr in pr_list_closed: - closed_pr_numbers.append(closed_pr.id) - assert 93 in closed_pr_numbers + assert 93 in {pr.id for pr in pr_list_closed} pr_list_merged = self.ogr_project.get_pr_list(status=PRStatus.merged) assert pr_list_merged assert len(pr_list_merged) >= 1 - closed_pr_numbers = [] - for closed_pr in pr_list_merged: - closed_pr_numbers.append(closed_pr.id) - assert 93 not in closed_pr_numbers + assert 93 not in {pr.id for pr in pr_list_merged} pr_list = self.ogr_project.get_pr_list() assert pr_list