Skip to content

Commit

Permalink
Move away from runtime_checkable (#968)
Browse files Browse the repository at this point in the history
Co-authored-by: always-on-duty[bot] <120557446+always-on-duty[bot]@users.noreply.github.com>
  • Loading branch information
FasterSpeeding and always-on-duty[bot] authored Nov 16, 2024
1 parent 27907a1 commit ebd0d7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Moved away from using `typing.runtime_checkable` as this is unreliable in
newer Python versions.

## [2.17.6] - 2024-10-07
### Changed
- Support Python 3.13.
Expand Down
16 changes: 13 additions & 3 deletions tanjun/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from . import components

if typing.TYPE_CHECKING:
import typing_extensions
from alluka import abc as alluka
from typing_extensions import Self

Expand Down Expand Up @@ -132,12 +133,21 @@ async def stop(self) -> None:
"""


@typing.runtime_checkable
class _ComponentProto(typing.Protocol):
def add_schedule(self, schedule: AbstractSchedule, /) -> typing.Any:
raise NotImplementedError


def _is_component_proto(value: typing.Any, /) -> typing_extensions.TypeGuard[_ComponentProto]:
try:
value.add_schedule

except AttributeError:
return False

return True


def as_interval(
interval: typing.Union[int, float, datetime.timedelta],
/,
Expand Down Expand Up @@ -298,7 +308,7 @@ def copy(self) -> Self:

def load_into_component(self, component: tanjun.Component, /) -> None:
# <<inherited docstring from tanjun.components.AbstractComponentLoader>>.
if isinstance(component, _ComponentProto):
if _is_component_proto(component):
component.add_schedule(self)

def set_start_callback(self, callback: _CallbackSig, /) -> Self:
Expand Down Expand Up @@ -1083,7 +1093,7 @@ async def _loop(self, client: alluka.Client, /) -> None:

def load_into_component(self, component: tanjun.Component, /) -> None:
# <<inherited docstring from tanjun.components.AbstractComponentLoader>>.
if isinstance(component, _ComponentProto):
if _is_component_proto(component):
component.add_schedule(self)

def start(self, client: alluka.Client, /, *, loop: typing.Optional[asyncio.AbstractEventLoop] = None) -> None:
Expand Down

0 comments on commit ebd0d7e

Please sign in to comment.