Skip to content

Commit

Permalink
ci(ruff): enable the perflint and ruff rules
Browse files Browse the repository at this point in the history
* 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 <mfocko@redhat.com>
  • Loading branch information
mfocko committed Oct 12, 2023
1 parent 4edd370 commit ce9655d
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 105 deletions.
27 changes: 14 additions & 13 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import (
Any,
Callable,
ClassVar,
Optional,
TypeVar,
Union,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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]:
"""
Expand Down
8 changes: 4 additions & 4 deletions ogr/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions ogr/read_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions ogr/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions ogr/services/github/auth_providers/github_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion ogr/services/github/auth_providers/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
5 changes: 4 additions & 1 deletion ogr/services/github/auth_providers/tokman.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion ogr/services/github/flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: MIT

import datetime
from typing import ClassVar

from github import UnknownObjectException

Expand All @@ -11,7 +12,7 @@


class GithubCommitFlag(BaseCommitFlag):
_states = {
_states: ClassVar[dict[str, CommitStatus]] = {
"pending": CommitStatus.pending,
"success": CommitStatus.success,
"failure": CommitStatus.failure,
Expand Down
14 changes: 7 additions & 7 deletions ogr/services/github/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ogr/services/github/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions ogr/services/github/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 '- '
Expand Down Expand Up @@ -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 = ""

Expand Down
3 changes: 2 additions & 1 deletion ogr/services/gitlab/flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import datetime
import logging
from typing import ClassVar

import gitlab

Expand All @@ -15,7 +16,7 @@


class GitlabCommitFlag(BaseCommitFlag):
_states = {
_states: ClassVar[dict[str, CommitStatus]] = {
"pending": CommitStatus.pending,
"success": CommitStatus.success,
"failed": CommitStatus.failure,
Expand Down
10 changes: 5 additions & 5 deletions ogr/services/gitlab/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,16 @@ def create_pr(
body: str,
target_branch: str,
source_branch: str,
fork_username: str = None,
fork_username: Optional[str] = None,
) -> "PullRequest":
pass

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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions ogr/services/gitlab/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: MIT

import datetime
from typing import Optional
from typing import ClassVar, Optional

import gitlab
import requests
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit ce9655d

Please sign in to comment.