diff --git a/artipy/artifacts.py b/artipy/artifacts.py index 44eca29..42333b0 100644 --- a/artipy/artifacts.py +++ b/artipy/artifacts.py @@ -2,9 +2,9 @@ from __future__ import annotations -from copy import deepcopy import random from collections.abc import Callable +from copy import deepcopy from itertools import starmap from artipy import MAX_RARITY, UPGRADE_STEP @@ -34,16 +34,19 @@ def _level_up_artifact(artifact: Artifact) -> None: artifact.level = new_level artifact.mainstat.set_value_by_level(new_level) + def pick_stat(artifact: Artifact) -> SubStat: if not artifact.rarity: - raise ValueError("Artifact must have a rarity set") - + msg = "Artifact must have a rarity set" + raise ValueError(msg) + stats = frozenset(s.name for s in (artifact.mainstat, *artifact.substats)) pool = {s: w for s, w in substat_weights.items() if s not in stats} - + if not pool: - raise ValueError("No valid stats available to pick from") - + msg = "No valid stats available to pick from" + raise ValueError(msg) + population, weights = map(tuple, zip(*pool.items(), strict=False)) new_stat_name = choose(population, weights) return create_substat(name=new_stat_name, rarity=artifact.rarity) @@ -385,11 +388,11 @@ def with_rarity(self, rarity: int) -> ArtifactBuilder: self._artifact.rarity = rarity return self - + @classmethod def five_star(cls) -> ArtifactBuilder: return cls().with_rarity(5) - + @classmethod def four_star(cls) -> ArtifactBuilder: return cls().with_rarity(4) diff --git a/artipy/data_gen.py b/artipy/data_gen.py index f64db24..7051c30 100644 --- a/artipy/data_gen.py +++ b/artipy/data_gen.py @@ -1,11 +1,12 @@ """Module that handles JSON data for the stats module.""" -import orjson import re from collections.abc import Iterator, Mapping, MutableMapping, Sequence from types import SimpleNamespace from typing import Any, ClassVar, cast +import orjson + from artipy import __data__ @@ -36,7 +37,7 @@ def recursive_namespace(data: Any) -> Any | SimpleNamespace: Any | SimpleNamespace: The converted data. """ if isinstance(data, Mapping): - data = cast(Mapping[str, Any], data) + data = cast("Mapping[str, Any]", data) return SimpleNamespace(**{ camel_to_snake_case(k): recursive_namespace(v) for k, v in data.items() }) @@ -86,7 +87,7 @@ def _load_data(self, file_name: str) -> None: with (__data__ / file_name).open("rb") as f: data = orjson.loads(f.read()) if not hasattr(self, "_data"): - self._data = [recursive_namespace(item) for item in data] # type: ignore + self._data = [recursive_namespace(item) for item in data] # type: ignore[reportUninitializedInstanceVariable] def as_list(self) -> list[SimpleNamespace]: """Return the data as a list. diff --git a/artipy/stats.py b/artipy/stats.py index e83d374..b4bb6b2 100644 --- a/artipy/stats.py +++ b/artipy/stats.py @@ -58,6 +58,7 @@ def set_value_by_level(self, level: int) -> None: """Set the value of the mainstat based on the level of the artifact.""" self.value = possible_mainstat_values(self.name, self.rarity)[level] + @dataclass(slots=True) class SubStat(Stat): """Substat dataclass for a Genshin Impact artifact.""" diff --git a/artipy/utils.py b/artipy/utils.py index 07c8c72..3b01e41 100644 --- a/artipy/utils.py +++ b/artipy/utils.py @@ -54,7 +54,7 @@ def possible_mainstat_values(stat: StatType, rarity: int) -> list[Decimal]: list[Decimal]: The possible values for the mainstat. """ mainstat_data = MAINSTAT_DATA.as_list() - values = cast(list[StatContainer], mainstat_data[1:]) + values = cast("list[StatContainer]", mainstat_data[1:]) data = [ j.value for i in values @@ -77,7 +77,7 @@ def possible_substat_values(stat: StatType, rarity: int) -> list[Decimal]: Returns: list[Decimal]: The possible values for the substat. """ - substat_data = cast(list[StatData], SUBSTAT_DATA.as_list()) + substat_data = cast("list[StatData]", SUBSTAT_DATA.as_list()) data = [ d for d in substat_data