Skip to content

Commit f4d2766

Browse files
Merge pull request #55 from amirR01/chore/add-runtime-type-check-to-protodto
chore: add run-time type-check to proto_dto
2 parents 0853b0a + 0d94120 commit f4d2766

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

archipy/models/dtos/base_protobuf_dto.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any, ClassVar, Self
22

33
from archipy.models.dtos.base_dtos import BaseDTO
4+
from archipy.models.errors import InvalidEntityTypeError
45

56
try:
67
from google.protobuf.json_format import MessageToDict, ParseDict
@@ -32,6 +33,13 @@ def from_proto(cls, request: Message) -> Self:
3233
if cls._proto_class is None:
3334
raise NotImplementedError(f"{cls.__name__} is not mapped to a proto class.")
3435

36+
if not isinstance(request, cls._proto_class):
37+
raise InvalidEntityTypeError(
38+
message=f"{cls.__name__}.from_proto expected a different type of request.",
39+
expected_type=cls._proto_class.__name__,
40+
actual_type=type(request).__name__,
41+
)
42+
3543
input_data = MessageToDict(
3644
message=request,
3745
always_print_fields_with_no_presence=True,

0 commit comments

Comments
 (0)