Skip to content

Commit

Permalink
Ruffで無視していたルールを無視しないようにする (#649)
Browse files Browse the repository at this point in the history
* ruffによるテストコードをチェック

* ANN201

* ANN201の追加

* ANN202の追加

* ruffで無視するルールを減らす

* python

* poetry update
  • Loading branch information
yuji38kwmt authored May 28, 2024
1 parent 41e6a1a commit 1c913a3
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 256 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ format:
poetry run ruff check ${SOURCE_FILES} ${TEST_FILES} --fix-only --exit-zero

lint:
poetry run ruff check ${SOURCE_FILES}
# テストコードはチェックを緩和する
# pygrep-hooks, flake8-datetimez, line-too-long, flake8-annotations, unused-noqa
poetry run ruff check ${TEST_FILES} --ignore PGH,DTZ,E501,ANN,RUF100,G004
poetry run ruff check ${SOURCE_FILES} ${TEST_FILES}
poetry run mypy ${SOURCE_FILES} ${TEST_FILES}
# テストコードはチェックを緩和するためpylintは実行しない
poetry run pylint --jobs=0 ${SOURCE_FILES}
Expand Down
16 changes: 8 additions & 8 deletions annofabapi/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional


def _issue_deprecated_warning_with_class(cls, stacklevel: int, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001
def _issue_deprecated_warning_with_class(cls, stacklevel: int, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001, ANN202
"""非推奨なクラスに対する警告メッセージを出力する。"""
old_class_name = f"{cls.__module__}.{cls.__name__}"
message = f"deprecated: '{old_class_name}'は{deprecated_date}以降に廃止します。"
Expand All @@ -13,10 +13,10 @@ def _issue_deprecated_warning_with_class(cls, stacklevel: int, deprecated_date:
warnings.warn(message, FutureWarning, stacklevel=stacklevel)


def _process_class(cls, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001
def decorator(function): # noqa: ANN001
def _process_class(cls, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001, ANN202
def decorator(function): # noqa: ANN001, ANN202
@wraps(function)
def wrapped(*args, **kwargs):
def wrapped(*args, **kwargs): # noqa: ANN202
_issue_deprecated_warning_with_class(cls, stacklevel=3, deprecated_date=deprecated_date, new_class_name=new_class_name)
return function(*args, **kwargs)

Expand All @@ -26,19 +26,19 @@ def wrapped(*args, **kwargs):
return cls


def _process_enum_class(cls, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001
def getattribute(self, name): # noqa: ANN001
def _process_enum_class(cls, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001, ANN202
def getattribute(self, name): # noqa: ANN001, ANN202
_issue_deprecated_warning_with_class(cls, stacklevel=4, deprecated_date=deprecated_date, new_class_name=new_class_name)
return super(type(self), self).__getattribute__(name)

cls.__getattribute__ = getattribute
return cls


def deprecated_class(_cls=None, *, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001
def deprecated_class(_cls=None, *, deprecated_date: str, new_class_name: Optional[str] = None): # noqa: ANN001, ANN202
"""クラスを非推奨にします。"""

def wrap(cls): # noqa: ANN001
def wrap(cls): # noqa: ANN001, ANN202
if issubclass(cls, enum.Enum):
return _process_enum_class(cls, deprecated_date=deprecated_date, new_class_name=new_class_name)
else:
Expand Down
16 changes: 8 additions & 8 deletions annofabapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from functools import wraps
from json import JSONDecodeError
from typing import Any, Dict, Optional, Tuple
from typing import Any, Callable, Dict, Optional, Tuple

import backoff
import requests
Expand Down Expand Up @@ -55,7 +55,7 @@ def _log_error_response(arg_logger: logging.Logger, response: requests.Response)
"""

def mask_key(d, key: str): # noqa: ANN001
def mask_key(d, key: str): # noqa: ANN001, ANN202
if key in d:
d[key] = "***"

Expand Down Expand Up @@ -110,7 +110,7 @@ def _create_request_body_for_logger(data: Any) -> Any: # noqa: ANN401
ログ出力用のrequest_body
"""

def mask_key(d, key: str): # noqa: ANN001
def mask_key(d, key: str): # noqa: ANN001, ANN202
if key in d:
d[key] = "***"

Expand Down Expand Up @@ -145,7 +145,7 @@ def _create_query_params_for_logger(params: Dict[str, Any]) -> Dict[str, Any]:
ログ出力用のparams
"""

def mask_key(d, key: str): # noqa: ANN001
def mask_key(d, key: str): # noqa: ANN001, ANN202
if key in d:
d[key] = "***"

Expand Down Expand Up @@ -174,14 +174,14 @@ def _should_retry_with_status(status_code: int) -> bool:
return False


def my_backoff(function): # noqa: ANN001
def my_backoff(function) -> Callable: # noqa: ANN001
"""
HTTP Status Codeが429 or 5XXのときはリトライする. 最大5分間リトライする。
"""

@wraps(function)
def wrapped(*args, **kwargs):
def fatal_code(e): # noqa: ANN001
def wrapped(*args, **kwargs): # noqa: ANN202
def fatal_code(e): # noqa: ANN001, ANN202
"""
リトライするかどうか
status codeが5xxのとき、またはToo many Requests(429)のときはリトライする。429以外の4XXはリトライしない
Expand Down Expand Up @@ -475,7 +475,7 @@ def _request_wrapper(
"""

# TODO 判定条件が不明
if url_path.startswith("/internal/"):
if url_path.startswith("/internal/"): # noqa: SIM108
url = f"{self.endpoint_url}/api{url_path}"
else:
url = f"{self.url_prefix}{url_path}"
Expand Down
2 changes: 1 addition & 1 deletion annofabapi/api2.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _request_wrapper(

return content, response

def _get_signed_access_v2(self, url_path: str):
def _get_signed_access_v2(self, url_path: str): # noqa: ANN202
query_params = {"url": f"/api/v2{url_path}"}
self.get_signed_access_v2(query_params)

Expand Down
18 changes: 9 additions & 9 deletions annofabapi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def input_data_id(self) -> str:
return self.__input_data_id

@abc.abstractmethod
def open_outer_file(self, data_uri: str):
def open_outer_file(self, data_uri: str): # noqa: ANN201
"""
外部ファイル(塗りつぶし画像など)を開き、対応するファイルオブジェクトを返す。
JSONファイルと同階層にある、"JSONファイルの拡張子を除いた名前"のディレクトリ配下を探します。
Expand Down Expand Up @@ -141,7 +141,7 @@ def input_data_id(self) -> str:
return self.__input_data_id

@abc.abstractmethod
def open_outer_file(self, data_uri: str):
def open_outer_file(self, data_uri: str): # noqa: ANN201
"""
外部ファイル(塗りつぶし画像など)を開き、対応するファイルオブジェクトを返す。
JSONファイルと同階層にある、"JSONファイルの拡張子を除いた名前"のディレクトリ配下を探します。
Expand Down Expand Up @@ -206,7 +206,7 @@ def load_json(self) -> Any: # noqa: ANN401
with self.__zip_file.open(self.json_file_path) as entry:
return json.load(entry)

def open_outer_file(self, data_uri: str):
def open_outer_file(self, data_uri: str): # noqa: ANN201
outer_file_path = _trim_extension(self.json_file_path) + "/" + data_uri
try:
return self.__zip_file.open(outer_file_path, mode="r")
Expand Down Expand Up @@ -237,10 +237,10 @@ def load_json(self) -> Any: # noqa: ANN401
with open(self.json_file_path, encoding="utf-8") as f:
return json.load(f)

def open_outer_file(self, data_uri: str):
def open_outer_file(self, data_uri: str): # noqa: ANN201
outer_file_path = _trim_extension(self.json_file_path) + "/" + data_uri
try:
return open(outer_file_path, mode="rb") # pylint: disable=consider-using-with
return open(outer_file_path, mode="rb") # noqa: SIM115, pylint: disable=consider-using-with
except FileNotFoundError as e:
raise AnnotationOuterFileNotFoundError(str(outer_file_path)) from e

Expand Down Expand Up @@ -271,7 +271,7 @@ def load_json(self) -> Any: # noqa: ANN401
with self.__zip_file.open(self.json_file_path) as entry:
return json.load(entry)

def open_outer_file(self, data_uri: str):
def open_outer_file(self, data_uri: str): # noqa: ANN201
outer_file_path = _trim_extension(self.json_file_path) + "/" + data_uri
try:
return self.__zip_file.open(outer_file_path, mode="r")
Expand Down Expand Up @@ -303,10 +303,10 @@ def load_json(self) -> Any: # noqa: ANN401
with open(self.json_file_path, encoding="utf-8") as f:
return json.load(f)

def open_outer_file(self, data_uri: str):
def open_outer_file(self, data_uri: str): # noqa: ANN201
outer_file_path = _trim_extension(self.json_file_path) + "/" + data_uri
try:
return open(outer_file_path, mode="rb") # pylint: disable=consider-using-with
return open(outer_file_path, mode="rb") # noqa: SIM115, pylint: disable=consider-using-with
except FileNotFoundError as e:
raise AnnotationOuterFileNotFoundError(str(outer_file_path)) from e

Expand Down Expand Up @@ -460,7 +460,7 @@ def __parse_annotation_dir(annotation_dir_path: Path, clazz: Type[Union[SimpleAn
if not input_data_file.is_file():
continue

if not input_data_file.suffix == ".json":
if input_data_file.suffix != ".json":
continue

parser = clazz(input_data_file)
Expand Down
28 changes: 14 additions & 14 deletions annofabapi/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def upload_data_to_s3(self, project_id: str, data: Any, content_type: str) -> st
CheckSumError: アップロードしたデータのMD5ハッシュ値が、S3にアップロードしたときのレスポンスのETagと一致しない
""" # noqa: E501

def get_md5_value_from_file(fp): # noqa: ANN001
def get_md5_value_from_file(fp): # noqa: ANN001, ANN202
md5_obj = hashlib.md5()
while True:
chunk = fp.read(2048 * md5_obj.block_size)
Expand Down Expand Up @@ -1143,9 +1143,9 @@ def get_account_daily_statistics(
ユーザ別タスク集計データ
"""

def decorator(f, project_id: str): # noqa: ANN001
def decorator(f, project_id: str): # noqa: ANN001, ANN202
@functools.wraps(f)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs): # noqa: ANN202
content, _ = f(project_id, *args, **kwargs)
return content

Expand Down Expand Up @@ -1177,9 +1177,9 @@ def get_inspection_daily_statistics(
"""

def decorator(f, project_id: str): # noqa: ANN001
def decorator(f, project_id: str): # noqa: ANN001, ANN202
@functools.wraps(f)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs): # noqa: ANN202
content, _ = f(project_id, *args, **kwargs)
return content

Expand All @@ -1202,9 +1202,9 @@ def get_phase_daily_statistics(self, project_id: str, *, from_date: Optional[str
"""

def decorator(f, project_id: str): # noqa: ANN001
def decorator(f, project_id: str): # noqa: ANN001, ANN202
@functools.wraps(f)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs): # noqa: ANN202
content, _ = f(project_id, *args, **kwargs)
return content

Expand All @@ -1227,9 +1227,9 @@ def get_task_daily_statistics(self, project_id: str, *, from_date: Optional[str]
"""

def decorator(f, project_id: str): # noqa: ANN001
def decorator(f, project_id: str): # noqa: ANN001, ANN202
@functools.wraps(f)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs): # noqa: ANN202
content, _ = f(project_id, *args, **kwargs)
return content

Expand All @@ -1251,9 +1251,9 @@ def get_worktime_daily_statistics(self, project_id: str, *, from_date: Optional[
プロジェクト全体のタスク作業時間集計データ
"""

def decorator(f, project_id: str): # noqa: ANN001
def decorator(f, project_id: str): # noqa: ANN001, ANN202
@functools.wraps(f)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs): # noqa: ANN202
content, _ = f(project_id, *args, **kwargs)
return content["data_series"]

Expand All @@ -1279,9 +1279,9 @@ def get_worktime_daily_statistics_by_account(
プロジェクトメンバーのタスク作業時間集計データ
"""

def decorator(f, project_id: str, account_id: str): # noqa: ANN001
def decorator(f, project_id: str, account_id: str): # noqa: ANN001, ANN202
@functools.wraps(f)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs): # noqa: ANN202
content, _ = f(project_id, account_id, *args, **kwargs)
return content["data_series"]

Expand Down Expand Up @@ -2210,7 +2210,7 @@ def get_job_from_job_id(arg_job_id: str) -> Optional[ProjectJobInfo]:
)
return JobStatus.FAILED

else:
else: # noqa: PLR5501
# 進行中
if job_access_count < max_job_access:
logger.info(
Expand Down
Loading

0 comments on commit 1c913a3

Please sign in to comment.