Skip to content

Commit

Permalink
chore: rewrite property pr (#174)
Browse files Browse the repository at this point in the history
* chore: rewrite property pr

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: repair pyproject.toml

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
NiceAesth and pre-commit-ci[bot] authored Sep 1, 2023
1 parent e44000c commit b07ebb4
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 101 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ repos:
rev: v3.10.1
hooks:
- id: pyupgrade
args: [--py37-plus, --keep-runtime-typing]
args: [--py39-plus, --keep-runtime-typing]
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.10.0
hooks:
- id: reorder-python-imports
args: [--py37-plus, --add-import, "from __future__ import annotations"]
args: [--py39-plus, --add-import, "from __future__ import annotations"]
- repo: https://github.com/asottile/add-trailing-comma
rev: v3.0.1
hooks:
Expand Down
5 changes: 2 additions & 3 deletions aiosu/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Type
from typing import Callable
from .models import OAuthToken

Expand All @@ -28,15 +27,15 @@ class Eventable(abc.ABC):
def __init__(self) -> None:
self._listeners: dict[str, list[Callable]] = {}

def _register_event(self, event: Type[BaseEvent]) -> None:
def _register_event(self, event: type[BaseEvent]) -> None:
r"""Registers an event
:param event: Event type to register
:type event: Type[BaseEvent]
"""
self._listeners[event._name] = []

def _register_listener(self, func: Callable, event: Type[BaseEvent]) -> None:
def _register_listener(self, func: Callable, event: type[BaseEvent]) -> None:
r"""Registers an event listener
:param func: Function to call when event is emitted
Expand Down
28 changes: 14 additions & 14 deletions aiosu/models/beatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime
from enum import Enum
from enum import unique
from functools import cached_property
from typing import Any
from typing import Literal
from typing import Optional
Expand Down Expand Up @@ -271,25 +272,12 @@ class Beatmap(BaseModel):
beatmapset: Optional[Beatmapset] = None
failtimes: Optional[BeatmapFailtimes] = None

@model_validator(mode="before")
@classmethod
def _set_url(cls, values: dict[str, Any]) -> dict[str, Any]:
if values.get("url") is None:
id = values["id"]
beatmapset_id = values["beatmapset_id"]
mode = Gamemode(values["mode"])
values[
"url"
] = f"https://osu.ppy.sh/beatmapsets/{beatmapset_id}#{mode}/{id}"
return values

@computed_field # type: ignore
@property
def discussion_url(self) -> str:
return f"https://osu.ppy.sh/beatmapsets/{self.beatmapset_id}/discussion/{self.id}/general"

@computed_field # type: ignore
@property
@cached_property
def count_objects(self) -> Optional[int]:
"""Total count of the objects.
Expand All @@ -304,6 +292,18 @@ def count_objects(self) -> Optional[int]:
return None
return self.count_spinners + self.count_circles + self.count_sliders

@model_validator(mode="before")
@classmethod
def _set_url(cls, values: dict[str, Any]) -> dict[str, Any]:
if values.get("url") is None:
id = values["id"]
beatmapset_id = values["beatmapset_id"]
mode = Gamemode(values["mode"])
values[
"url"
] = f"https://osu.ppy.sh/beatmapsets/{beatmapset_id}#{mode}/{id}"
return values

@classmethod
def _from_api_v1(cls, data: Any) -> Beatmap:
return cls.model_validate(
Expand Down
5 changes: 3 additions & 2 deletions aiosu/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""
from __future__ import annotations

from collections.abc import Coroutine
from datetime import datetime
from functools import cached_property
from functools import partial
from typing import Any
from typing import Coroutine
from typing import Literal
from typing import Optional

Expand Down Expand Up @@ -75,7 +76,7 @@ class Country(BaseModel):
name: str

@computed_field # type: ignore
@property
@cached_property
def flag_emoji(self) -> str:
r"""Emoji for the flag.
Expand Down
2 changes: 1 addition & 1 deletion aiosu/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Event(BaseModel):
created_at: datetime
id: int
type: EventType
r"""Information on types: https://github.com/ppy/osu-web/blob/master/resources/assets/lib/interfaces/event-json.ts"""
r"""Information on types: https://github.com/ppy/osu-web/blob/master/resources/js/interfaces/event-json.ts"""
parse_error: Optional[bool] = None
achievement: Optional[Achievement] = None
user: Optional[EventUser] = None
Expand Down
67 changes: 31 additions & 36 deletions aiosu/models/lazer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

from datetime import datetime
from functools import cached_property
from typing import Any
from typing import Optional

Expand Down Expand Up @@ -85,32 +86,26 @@ class LazerScoreStatistics(BaseModel):
perfect: int = 0
legacy_combo_increase: int = 0

@computed_field # type: ignore
@property
def count_300(self) -> int:
return self.great

@computed_field # type: ignore
@property
def count_100(self) -> int:
return self.ok

@computed_field # type: ignore
@property
def count_50(self) -> int:
return self.meh

@computed_field # type: ignore
@property
def count_miss(self) -> int:
return self.miss

@computed_field # type: ignore
@property
def count_geki(self) -> int:
return self.perfect

@computed_field # type: ignore
@property
def count_katu(self) -> int:
return self.good
Expand Down Expand Up @@ -151,43 +146,18 @@ class LazerScore(BaseModel):
pp: Optional[float] = None
weight: Optional[ScoreWeight] = None

@computed_field # type: ignore
@property
def mods_str(self) -> str:
return "".join(str(mod) for mod in self.mods)

@computed_field # type: ignore
@property
def created_at(self) -> datetime:
return self.ended_at

@computed_field # type: ignore
@property
def completion(self) -> Optional[float]:
"""Beatmap completion.
:return: Beatmap completion of a score (%). 100% for passes. None if no beatmap.
:rtype: Optional[float]
"""
if not self.beatmap:
return None

if self.passed:
return 100.0

return calculate_score_completion(self.statistics, self.beatmap)

@computed_field # type: ignore
@property
def mode(self) -> Gamemode:
return Gamemode(self.ruleset_id)

@computed_field # type: ignore
@property
def score(self) -> int:
return self.total_score

@computed_field # type: ignore
@property
def has_replay(self) -> bool:
return self.replay

@property
def score_url(self) -> Optional[str]:
r"""Link to the score.
Expand All @@ -203,7 +173,6 @@ def score_url(self) -> Optional[str]:
else f"https://osu.ppy.sh/scores/{self.id}"
)

@computed_field # type: ignore
@property
def replay_url(self) -> Optional[str]:
r"""Link to the replay.
Expand All @@ -219,6 +188,32 @@ def replay_url(self) -> Optional[str]:
else f"https://osu.ppy.sh/scores/{self.id}/download"
)

@computed_field # type: ignore
@cached_property
def completion(self) -> Optional[float]:
"""Beatmap completion.
:return: Beatmap completion of a score (%). 100% for passes. None if no beatmap.
:rtype: Optional[float]
"""
if not self.beatmap:
return None

if self.passed:
return 100.0

return calculate_score_completion(self.statistics, self.beatmap)

@computed_field # type: ignore
@cached_property
def mode(self) -> Gamemode:
return Gamemode(self.ruleset_id)

@computed_field # type: ignore
@cached_property
def mods_str(self) -> str:
return "".join(str(mod) for mod in self.mods)

@model_validator(mode="before")
@classmethod
def _fail_rank(cls, values: dict[str, Any]) -> dict[str, Any]:
Expand Down
3 changes: 1 addition & 2 deletions aiosu/models/mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from enum import IntEnum
from enum import unique
from functools import reduce
from typing import Type
from typing import TYPE_CHECKING

from pydantic import GetCoreSchemaHandler
Expand Down Expand Up @@ -209,7 +208,7 @@ def __or__(self, __o: Any) -> int:
@classmethod
def __get_pydantic_core_schema__(
cls,
source_type: Type[Any],
source_type: type[Any],
handler: GetCoreSchemaHandler,
) -> CoreSchema:
return core_schema.no_info_before_validator_function(
Expand Down
37 changes: 18 additions & 19 deletions aiosu/models/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

from datetime import datetime
from functools import cached_property
from typing import Optional
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -161,24 +162,6 @@ class Score(BaseModel):
beatmap_id: Optional[int] = None
"""Only present on API v1"""

@computed_field # type: ignore
@property
def completion(self) -> Optional[float]:
"""Beatmap completion.
:raises ValueError: If mode is unknown
:return: Beatmap completion of a score (%). 100% for passes. None if no beatmap.
:rtype: Optional[float]
"""
if not self.beatmap:
return None

if self.passed:
return 100.0

return calculate_score_completion(self.mode, self.statistics, self.beatmap)

@computed_field # type: ignore
@property
def score_url(self) -> Optional[str]:
r"""Link to the score.
Expand All @@ -194,7 +177,6 @@ def score_url(self) -> Optional[str]:
else f"https://osu.ppy.sh/scores/{self.id}"
)

@computed_field # type: ignore
@property
def replay_url(self) -> Optional[str]:
r"""Link to the replay.
Expand All @@ -210,6 +192,23 @@ def replay_url(self) -> Optional[str]:
else f"https://osu.ppy.sh/scores/{self.id}/download"
)

@computed_field # type: ignore
@cached_property
def completion(self) -> Optional[float]:
"""Beatmap completion.
:raises ValueError: If mode is unknown
:return: Beatmap completion of a score (%). 100% for passes. None if no beatmap.
:rtype: Optional[float]
"""
if not self.beatmap:
return None

if self.passed:
return 100.0

return calculate_score_completion(self.mode, self.statistics, self.beatmap)

@model_validator(mode="before")
@classmethod
def _fail_rank(cls, values: dict[str, Any]) -> dict[str, Any]:
Expand Down
5 changes: 3 additions & 2 deletions aiosu/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime
from enum import Enum
from enum import unique
from functools import cached_property
from typing import Any
from typing import Literal
from typing import Optional
Expand Down Expand Up @@ -111,7 +112,7 @@ class UserRankHistoryElement(BaseModel):
data: list[int]

@computed_field # type: ignore
@property
@cached_property
def average_gain(self) -> float:
r"""Average rank gain.
Expand Down Expand Up @@ -230,7 +231,7 @@ class UserStats(BaseModel):
variants: Optional[list[UserStatsVariant]] = None

@computed_field # type: ignore
@property
@cached_property
def pp_per_playtime(self) -> float:
r"""PP per playtime.
Expand Down
3 changes: 1 addition & 2 deletions aiosu/utils/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ..models.mods import Mod

if TYPE_CHECKING:
from typing import Type
from typing import Callable
from ..models.score import Score

Expand All @@ -24,7 +23,7 @@
cast_int: Callable[..., int] = lambda x: int(x or 0)


def get_calculator(mode: Gamemode) -> Type[AbstractAccuracyCalculator]:
def get_calculator(mode: Gamemode) -> type[AbstractAccuracyCalculator]:
r"""Returns the accuracy calculator for the given gamemode.
:param mode: The gamemode to get the calculator for
Expand Down
3 changes: 1 addition & 2 deletions aiosu/utils/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from .accuracy import TaikoAccuracyCalculator

if TYPE_CHECKING:
from typing import Type
from ..models.score import Score
from ..models.beatmap import BeatmapDifficultyAttributes
from ..models.performance import PerformanceAttributes
Expand Down Expand Up @@ -54,7 +53,7 @@ def calculate(self, score: Score) -> PerformanceAttributes:
...


def get_calculator(mode: Gamemode) -> Type[AbstractPerformanceCalculator]:
def get_calculator(mode: Gamemode) -> type[AbstractPerformanceCalculator]:
r"""Returns the performance calculator for the given gamemode.
:param mode: The gamemode to get the calculator for
Expand Down
Loading

0 comments on commit b07ebb4

Please sign in to comment.