Skip to content

Commit 86a89b0

Browse files
committed
Don't use runtime_checkable
It only checks for the existence of properties and is a bit misleading
1 parent 2ff359e commit 86a89b0

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

strcs/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
FromMeta,
77
MergedMetaAnnotation,
88
MetaAnnotation,
9+
is_adjustable_creator,
10+
is_adjustable_meta,
911
)
1012
from .args_extractor import ArgsExtractor
1113
from .decorator import (
@@ -49,5 +51,7 @@
4951
"TypeCache",
5052
"WrappedCreator",
5153
"errors",
54+
"is_adjustable_creator",
55+
"is_adjustable_meta",
5256
"resolve_types",
5357
]

strcs/annotations.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
object as a creator override.
2020
"""
2121

22-
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar, runtime_checkable
22+
from typing import TYPE_CHECKING, Generic, Protocol, TypeGuard, TypeVar
2323

2424
import attrs
2525

@@ -38,7 +38,6 @@
3838
T = TypeVar("T")
3939

4040

41-
@runtime_checkable
4241
class AdjustableMeta(Protocol[T]):
4342
"""
4443
An interface used to modify the meta object when creating a field.
@@ -55,7 +54,10 @@ class AdjustableMeta(Protocol[T]):
5554
def adjusted_meta(self, meta: Meta, typ: Type[T], type_cache: TypeCache) -> Meta: ...
5655

5756

58-
@runtime_checkable
57+
def is_adjustable_meta(meta: object) -> TypeGuard[AdjustableMeta]:
58+
return hasattr(meta, "adjusted_meta")
59+
60+
5961
class AdjustableCreator(Protocol[T]):
6062
"""
6163
An interface used to modify the creator used when creating a field.
@@ -88,6 +90,10 @@ def adjusted_creator(
8890
) -> ConvertFunction[T] | None: ...
8991

9092

93+
def is_adjustable_creator(meta: object) -> TypeGuard[AdjustableCreator]:
94+
return hasattr(meta, "adjusted_creator")
95+
96+
9197
@attrs.define
9298
class MetaAnnotation:
9399
"""
@@ -226,7 +232,7 @@ def adjusted_meta(self, meta: Meta, typ: Type[T], type_cache: TypeCache) -> Meta
226232
if self.meta is None:
227233
return meta
228234

229-
if isinstance(self.meta, AdjustableMeta):
235+
if is_adjustable_meta(self.meta):
230236
return self.meta.adjusted_meta(meta, typ, type_cache)
231237

232238
if attrs.has(self.meta.__class__):

strcs/disassemble/_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,13 @@ def ann(self) -> Union["AdjustableMeta[T]", "AdjustableCreator[T]"] | None:
653653
# TODO: support multiple annotations
654654
annotation = self.annotations[0]
655655

656-
if isinstance(annotation, AdjustableMeta):
656+
from ..annotations import is_adjustable_creator, is_adjustable_meta
657+
658+
if is_adjustable_meta(annotation):
657659
ann = annotation
658660
elif isinstance(annotation, MetaAnnotation | MergedMetaAnnotation):
659661
ann = Ann[T](annotation)
660-
elif isinstance(annotation, AdjustableCreator):
662+
elif is_adjustable_creator(annotation):
661663
ann = annotation
662664
elif callable(annotation):
663665
ann = Ann[T](creator=annotation)

strcs/hints.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
TypeGuard,
2424
TypeVar,
2525
Union,
26-
runtime_checkable,
2726
)
2827

2928
import attrs
@@ -41,7 +40,6 @@ class IsField(Protocol):
4140
name: str
4241

4342

44-
@runtime_checkable
4543
class WithResolvedTypes(Protocol[C]):
4644
"""
4745
Strcs will mark classes it has resolved types for to prevent recursive loops.

strcs/hooks.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
import cattrs
1313

14-
from .annotations import AdjustableCreator, AdjustableMeta
14+
from .annotations import (
15+
is_adjustable_creator,
16+
is_adjustable_meta,
17+
)
1518
from .decorator import ConvertDefinition, ConvertFunction, CreateArgs
1619
from .disassemble import Type, TypeCache, fill, instantiate
1720
from .meta import Meta
@@ -151,10 +154,10 @@ def convert(self, value: object, typ: type[T] | Type[T]) -> T:
151154
if normal_creator and creator is None:
152155
creator = normal_creator
153156

154-
if isinstance(want.ann, AdjustableMeta | AdjustableCreator):
155-
if isinstance(want.ann, AdjustableMeta):
157+
if is_adjustable_meta(want.ann) or is_adjustable_creator(want.ann):
158+
if is_adjustable_meta(want.ann):
156159
meta = want.ann.adjusted_meta(meta, want, self.type_cache)
157-
if isinstance(want.ann, AdjustableCreator):
160+
if is_adjustable_creator(want.ann):
158161
creator = want.ann.adjusted_creator( # type: ignore[assignment]
159162
creator, # type: ignore[arg-type]
160163
self.register,

tests/disassemble/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ def adjusted_meta(
14181418
adjustment = AdjustMeta()
14191419
ann = Dis(Annotated[int, adjustment]).ann
14201420
assert ann is adjustment
1421-
assert isinstance(ann, strcs.AdjustableMeta)
1421+
assert strcs.is_adjustable_meta(ann)
14221422

14231423
meta = strcs.Meta({"two": 2})
14241424
m = ann.adjusted_meta(meta, Dis(int), type_cache)

0 commit comments

Comments
 (0)