Skip to content

Commit

Permalink
Migrate to ruff (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati authored Mar 28, 2024
1 parent f6f4c11 commit 803b94d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 75 deletions.
43 changes: 0 additions & 43 deletions .flake8

This file was deleted.

14 changes: 1 addition & 13 deletions genshin-dev/lint-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
flake8

flake8-annotations-complexity # complex annotation
flake8-black # runs black
flake8-builtins # builtin shadowing
flake8-docstrings # proper formatting and grammar in docstrings
flake8-isort # runs isort
flake8-mutable # mutable default argument detection
flake8-pep3101 # new-style format strings only
flake8-print # complain about print statements in code
flake8-pytest-style # pytest checks
flake8-raise # exception raising
flake8-requirements # requirements.txt check
ruff
4 changes: 2 additions & 2 deletions genshin-dev/reformat-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
black
isort
sort-all
ruff
sort-all
8 changes: 4 additions & 4 deletions genshin/models/genshin/chronicle/abyss.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class CharacterRanks(APIModel):
most_played: typing.Sequence[AbyssRankCharacter] = Aliased("reveal_rank", default=[], mi18n="bbs/go_fight_count")
most_kills: typing.Sequence[AbyssRankCharacter] = Aliased("defeat_rank", default=[], mi18n="bbs/max_rout_count")
strongest_strike: typing.Sequence[AbyssRankCharacter] = Aliased("damage_rank", default=[], mi18n="bbs/powerful_attack")
most_damage_taken: typing.Sequence[AbyssRankCharacter] = Aliased("take_damage_rank", default=[], mi18n="bbs/receive_max_damage")
most_bursts_used: typing.Sequence[AbyssRankCharacter] = Aliased("energy_skill_rank", default=[], mi18n="bbs/element_break_count")
most_skills_used: typing.Sequence[AbyssRankCharacter] = Aliased("normal_skill_rank", default=[], mi18n="bbs/element_skill_use_count")
most_damage_taken: typing.Sequence[AbyssRankCharacter] = Aliased("take_damage_rank", default=[], mi18n="bbs/receive_max_damage") # noqa: E501
most_bursts_used: typing.Sequence[AbyssRankCharacter] = Aliased("energy_skill_rank", default=[], mi18n="bbs/element_break_count") # noqa: E501
most_skills_used: typing.Sequence[AbyssRankCharacter] = Aliased("normal_skill_rank", default=[], mi18n="bbs/element_skill_use_count") # noqa: E501
# fmt: on

def as_dict(self, lang: typing.Optional[str] = None) -> typing.Mapping[str, typing.Any]:
"""Helper function which turns fields into properly named ones"""
"""Turn fields into properly named ones."""
return {
self._get_mi18n(field, lang or self.lang): getattr(self, field.name)
for field in self.__fields__.values()
Expand Down
2 changes: 1 addition & 1 deletion genshin/models/genshin/chronicle/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Stats(APIModel):
# fmt: on

def as_dict(self, lang: typing.Optional[str] = None) -> typing.Mapping[str, typing.Any]:
"""Helper function which turns fields into properly named ones"""
"""Turn fields into properly named ones."""
return {
self._get_mi18n(field, lang or self.lang): getattr(self, field.name)
for field in self.__fields__.values()
Expand Down
2 changes: 1 addition & 1 deletion genshin/models/genshin/lineup.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def __parse_characters(cls, value: typing.Any) -> typing.Any:
if isinstance(value[0], typing.Sequence):
return value

return [[character for character in group["group"]] for group in value]
return [list(group["group"]) for group in value]


class Lineup(LineupPreview):
Expand Down
5 changes: 3 additions & 2 deletions genshin/models/honkai/chronicle/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def _model_to_dict(model: APIModel, lang: str = "en-us") -> typing.Mapping[str, typing.Any]:
"""Helper function which turns fields into properly named ones"""
"""Turn fields into properly named ones."""
ret: typing.Dict[str, typing.Any] = {}
for field in model.__fields__.values():
if not field.field_info.extra.get("mi18n"):
Expand Down Expand Up @@ -123,7 +123,7 @@ class OldAbyssStats(APIModel):
raw_tier: int = Aliased("latest_area", mi18n="bbs/settled_level")
raw_latest_rank: typing.Optional[int] = Aliased("latest_level", mi18n="bbs/rank")
# TODO: Add proper key
latest_type: str = Aliased( mi18n="bbs/latest_type")
latest_type: str = Aliased( mi18n="bbs/latest_type")
# fmt: on

@pydantic.validator("raw_q_singularis_rank", "raw_dirac_sea_rank", "raw_latest_rank", pre=True)
Expand Down Expand Up @@ -240,6 +240,7 @@ def __pack_gamemode_stats(cls, values: typing.Dict[str, typing.Any]) -> typing.D
return values

def as_dict(self, lang: str = "en-us") -> typing.Mapping[str, typing.Any]:
"""Turn fields into properly named ones."""
return _model_to_dict(self, lang)


Expand Down
5 changes: 4 additions & 1 deletion genshin/utility/extdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import json
import logging
import time
import typing
import warnings
Expand All @@ -20,6 +21,8 @@
"update_characters_genshindata",
)

LOGGER_ = logging.getLogger(__name__)

CACHE_FILE = fs.get_tempdir() / "characters.json"

if CACHE_FILE.exists() and time.time() - CACHE_FILE.stat().st_mtime < 7 * 24 * 60 * 60:
Expand Down Expand Up @@ -235,7 +238,7 @@ async def update_characters_any(
try:
await updator(langs)
except Exception:
continue
LOGGER_.exception("Failed to update characters with %s", updator.__name__)
else:
return

Expand Down
9 changes: 4 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def install_requirements(session: nox.Session, *requirements: str, literal: bool
"""Install requirements."""
if not literal and all(requirement.isalpha() for requirement in requirements):
files = ["requirements.txt"] + [f"./genshin-dev/{requirement}-requirements.txt" for requirement in requirements]
requirements = ("pip",) + tuple(arg for file in files for arg in ("-r", file))
requirements = ("pip", *tuple(arg for file in files for arg in ("-r", file)))

session.install("--upgrade", *requirements, silent=not isverbose())

Expand All @@ -52,16 +52,15 @@ def docs(session: nox.Session) -> None:
def lint(session: nox.Session) -> None:
"""Run this project's modules against the pre-defined flake8 linters."""
install_requirements(session, "lint")
session.run("flake8", "--version")
session.run("flake8", *GENERAL_TARGETS, *verbose_args())
session.run("ruff", "check", *GENERAL_TARGETS, *verbose_args())


@nox.session()
def reformat(session: nox.Session) -> None:
"""Reformat this project's modules to fit the standard style."""
install_requirements(session, "reformat")
session.run("black", *GENERAL_TARGETS, *verbose_args())
session.run("isort", *GENERAL_TARGETS, *verbose_args())
session.run("python", "-m", "black", *GENERAL_TARGETS, *verbose_args())
session.run("python", "-m", "ruff", "check", "--fix-only", "--fixable", "ALL", *GENERAL_TARGETS, *verbose_args())

session.log("sort-all")
LOGGER.disabled = True
Expand Down
68 changes: 65 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,72 @@ build-backend = "setuptools.build_meta"

[tool.black]
line-length = 120
target-version = ["py39"]

[tool.isort]
profile = "black"
[tool.ruff]
line-length = 120
target-version = "py38"

[tool.ruff.lint]
select = [
"A",
"C4",
"C9",
"D",
"E",
"F",
"S",
"W",
"T20",
"PT",
"RSE"
]
exclude = ["tests", "test.py"]

# A001, A002, A003: `id` variable/parameter/attribute
# C408: dict() with keyword arguments
# D101: Missing docstring in public module
# D105: Missing docstring in magic method
# D106: Missing docstring Model.Config
# D400: First line should end with a period
# D419: Docstring is empty
# PT007: Wrong values type in `@pytest.mark.parametrize` expected `list` of `tuple`
# PT018: Assertion should be broken down into multiple parts
# S101: Use of assert for type checking
# S303: Use of md5
# S311: Use of pseudo-random generators
# S324: Use of md5 without usedforsecurity=False (3.9+)
ignore = [
"A001", "A002", "A003",
"C408",
"D100", "D105", "D106", "D400", "D419",
"PT007", "PT018",
"S101", "S303", "S311", "S324",
]

# auto-fixing too intrusive
# F401: Unused import
# F841: Unused variable
# B007: Unused loop variable
unfixable = ["F401", "F841", "B007"]

[tool.ruff.lint.per-file-ignores]
# F401: unused import.
# F403: cannot detect unused vars if we use starred import
# D10*: docstrings
# S10*: hardcoded passwords
# F841: unused variable
"**/__init__.py" = ["F401", "F403"]
"tests/**" = ["D10", "S10", "F841"]

[tool.ruff.lint.mccabe]
max-complexity = 16

[tool.ruff.lint.pycodestyle]
max-line-length = 130

[tool.ruff.lint.pydocstyle]
convention = "numpy"
ignore-decorators = ["property"]

[tool.pytest.ini_options]
asyncio_mode = "auto"
Expand Down

0 comments on commit 803b94d

Please sign in to comment.