From e5774c2855065677ebd8e18fd65618ee2033e26d Mon Sep 17 00:00:00 2001 From: Faster Speeding Date: Sat, 16 Nov 2024 22:29:18 +0000 Subject: [PATCH] Move away from runtime_checkable --- CHANGELOG.md | 4 ++++ yuyo/components.py | 18 +++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ae3b64..2b3c2673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/yuyo/components.py b/yuyo/components.py index ba086f52..d5f1b6f9 100644 --- a/yuyo/components.py +++ b/yuyo/components.py @@ -86,6 +86,7 @@ if typing.TYPE_CHECKING: import tanjun + import typing_extensions from typing_extensions import Self _OtherT = typing.TypeVar("_OtherT") @@ -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): @@ -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: