Skip to content

Commit

Permalink
formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Jun 15, 2024
1 parent 7320ae0 commit 8358424
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion python/lib/sift_internal/convert/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

ProtobufMessage = Message


class AsProtobuf(ABC):
"""
Abstract base class used to create create sub-types that can be treated
as an object that can be converted into an instance of `ProtobufMessage`.
"""

@abstractmethod
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
"""
Expand All @@ -19,11 +21,15 @@ def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:


T = TypeVar("T", bound=ProtobufMessage)


def try_cast_pb(value: AsProtobuf, target_klass: Type[T]) -> T:
"""
Tries to cast the `value` to `target_klass`, otherwise, returns a `TypeError`.
"""
value_pb = value.as_pb(target_klass)
if isinstance(value_pb, target_klass):
return cast(target_klass, value_pb)
raise TypeError(f"Expected a '{target_klass.__module__}{target_klass.__name__}' but got {value.__module__}{value.__class__.__name__}")
raise TypeError(
f"Expected a '{target_klass.__module__}{target_klass.__name__}' but got {value.__module__}{value.__class__.__name__}"
)
1 change: 1 addition & 0 deletions python/lib/sift_internal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

T = TypeVar("T")


def any_as(value: Any, target_klass: Type[T]) -> Optional[T]:
"""
Attempts to convert `value` of type `Any` to `target_klass`, otherwise return `None`.
Expand Down
2 changes: 1 addition & 1 deletion python/scripts/dev
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bootstrap() {

fmt() {
source venv/bin/activate
black lib/sift_py
black lib/sift_py lib/sift_internal
}

run_tests() {
Expand Down

0 comments on commit 8358424

Please sign in to comment.