Skip to content

Commit

Permalink
Move away from runtime_checkable
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Nov 16, 2024
1 parent 764fa93 commit e5774c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 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.

## [1.23.1] - 2024-10-07
### Changed
- Support Python 3.13
Expand Down
18 changes: 11 additions & 7 deletions yuyo/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@

if typing.TYPE_CHECKING:
import tanjun
import typing_extensions
from typing_extensions import Self

_OtherT = typing.TypeVar("_OtherT")
Expand Down Expand Up @@ -2317,20 +2318,23 @@ def __init__(
self.name: str = name


@typing.runtime_checkable
class _CustomIdProto(typing.Protocol):
def set_custom_id(self, value: str, /) -> object:
raise NotImplementedError

@classmethod
def __subclasshook__(cls, value: typing.Any) -> bool:
try:
value.set_custom_id
return _is_custom_id_proto(value)

except AttributeError:
return False

return True
def _is_custom_id_proto(value: typing.Any, /) -> typing_extensions.TypeGuard[_CustomIdProto]:
try:
value.set_custom_id

except AttributeError:
return False

return True


class ActionColumnExecutor(AbstractComponentExecutor):
Expand Down Expand Up @@ -2474,7 +2478,7 @@ def __init__(
for field in self._static_fields.values():
if id_metadata and (metadata := (id_metadata.get(field.id_match) or id_metadata.get(field.name))):
builder = copy.copy(field.builder)
assert isinstance(builder, _CustomIdProto)
assert _is_custom_id_proto(builder)
builder.set_custom_id(f"{field.id_match}:{metadata}")

else:
Expand Down

0 comments on commit e5774c2

Please sign in to comment.