From ff97801c4af00f3e0d99fda4e23c45b1c4c3b2c5 Mon Sep 17 00:00:00 2001 From: LuoShui Date: Mon, 9 Dec 2024 16:54:56 +0800 Subject: [PATCH] :sparkles: Use Ruff for linting --- .pre-commit-config.yaml | 15 ++++++++++ pyproject.toml | 29 ++++++++++++++++++- python_genshin_artifact/__init__.py | 26 ++++++++--------- .../_python_genshin_artifact.pyi | 2 +- python_genshin_artifact/assets.py | 6 ++-- python_genshin_artifact/enka/enka_parser.py | 8 ++--- tests/test_damage_calculator.py | 2 +- 7 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..2f1c111 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +default_language_version: + python: python3.10 +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.8.1 + hooks: + - id: ruff + args: + - --fix + - id: ruff-format +ci: + autofix_commit_msg: ":art: [pre-commit.ci] Auto format from pre-commit.com hooks" + autoupdate_commit_msg: ":arrow_up: [pre-commit.ci] pre-commit autoupdate" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 9612682..c2e1971 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,14 @@ bindings = "pyo3" requires = ["maturin>=1.0,<2.0"] build-backend = "maturin" +[dependency-groups] +testing = [ + 'pytest', +] +linting = [ + 'ruff', +] + [tool.pytest.ini_options] log_cli = true log_cli_level = "INFO" @@ -53,4 +61,23 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S" [tool.black] include = '\.pyi?$' line-length = 120 -target-version = ["py38"] \ No newline at end of file +target-version = ["py38"] + +[tool.ruff] +line-length = 120 + +[tool.ruff.lint] +extend-select = [ + "Q", # flake8-quotes + "C90", # complex-structure + "I", # isort + "F", # pyflakes + "B", # flake8-bugbear + "C4", # flake8-comprehensions +] +extend-ignore = [ + "E721", # using type() instead of isinstance() - we use this in tests +] +mccabe = { max-complexity = 13 } +isort = { known-first-party = ["python_genshin_artifact", "tests"] } + diff --git a/python_genshin_artifact/__init__.py b/python_genshin_artifact/__init__.py index 02b2fcf..4c1ce78 100644 --- a/python_genshin_artifact/__init__.py +++ b/python_genshin_artifact/__init__.py @@ -1,20 +1,20 @@ from ._python_genshin_artifact import ( - get_damage_analysis, - get_transformative_damage, - gen_character_meta_as_json, - gen_weapon_meta_as_json, - gen_artifact_meta_as_json, - gen_generate_locale_as_json, - TransformativeDamage, - CharacterInterface, - WeaponInterface, - BuffInterface, Artifact, - SkillInterface, - EnemyInterface, + BuffInterface, CalculatorConfig, - DamageResult, + CharacterInterface, DamageAnalysis, + DamageResult, + EnemyInterface, + SkillInterface, + TransformativeDamage, + WeaponInterface, + gen_artifact_meta_as_json, + gen_character_meta_as_json, + gen_generate_locale_as_json, + gen_weapon_meta_as_json, + get_damage_analysis, + get_transformative_damage, ) __all__ = ( diff --git a/python_genshin_artifact/_python_genshin_artifact.pyi b/python_genshin_artifact/_python_genshin_artifact.pyi index 3094b97..0521c8b 100644 --- a/python_genshin_artifact/_python_genshin_artifact.pyi +++ b/python_genshin_artifact/_python_genshin_artifact.pyi @@ -1,5 +1,5 @@ import sys -from typing import List, Optional, Tuple, TYPE_CHECKING, Dict, final +from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, final if sys.version_info < (3, 11): from typing_extensions import Literal diff --git a/python_genshin_artifact/assets.py b/python_genshin_artifact/assets.py index 4fc1179..167a061 100644 --- a/python_genshin_artifact/assets.py +++ b/python_genshin_artifact/assets.py @@ -1,11 +1,11 @@ import json -from typing import Dict, Tuple, List +from typing import Dict, List, Tuple from python_genshin_artifact import ( - gen_character_meta_as_json, - gen_weapon_meta_as_json, gen_artifact_meta_as_json, + gen_character_meta_as_json, gen_generate_locale_as_json, + gen_weapon_meta_as_json, ) diff --git a/python_genshin_artifact/enka/enka_parser.py b/python_genshin_artifact/enka/enka_parser.py index 3ad2a2a..de5a69d 100644 --- a/python_genshin_artifact/enka/enka_parser.py +++ b/python_genshin_artifact/enka/enka_parser.py @@ -1,13 +1,13 @@ import re -from typing import Tuple, List, Optional +from typing import List, Optional, Tuple +from python_genshin_artifact import Artifact, CharacterInterface, WeaponInterface from python_genshin_artifact.enka.artifacts import artifacts_name_map, equip_type_map from python_genshin_artifact.enka.assets import Assets from python_genshin_artifact.enka.characters import characters_map from python_genshin_artifact.enka.fight import fight_map, to_float from python_genshin_artifact.enka.weapon import weapon_name_map from python_genshin_artifact.error import EnkaParseException -from python_genshin_artifact import Artifact, CharacterInterface, WeaponInterface assets = Assets() @@ -75,8 +75,8 @@ def de_equip_list(equip_list: list[dict]) -> Tuple[WeaponInterface, List[Artifac _flat = _equip["flat"] try: artifact_id = int(re.findall(r"\d+", _flat["icon"])[0]) - except IndexError: - raise EnkaParseException(f"artifact_id is not found in {_flat['icon']}") + except IndexError as exc: + raise EnkaParseException(f"artifact_id is not found in {_flat['icon']}") from exc set_name = artifacts_name_map.get(artifact_id) if set_name is None: raise EnkaParseException(f"artifact_id={artifact_id} is not found in assets") diff --git a/tests/test_damage_calculator.py b/tests/test_damage_calculator.py index 4702128..d07a97f 100644 --- a/tests/test_damage_calculator.py +++ b/tests/test_damage_calculator.py @@ -1,9 +1,9 @@ from python_genshin_artifact import ( CalculatorConfig, - get_damage_analysis, CharacterInterface, SkillInterface, WeaponInterface, + get_damage_analysis, )