Skip to content

Commit

Permalink
컨테이너에서 MagicMock 객체도 조회되도록 수정
Browse files Browse the repository at this point in the history
`isinstance`을 이용하면 `MagicMock`의 `spec` 타입 필터링 가능
  • Loading branch information
qodot committed Oct 2, 2023
1 parent ffd5b8d commit 850fff8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/appl/i_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ def register(self, obj: Any) -> None:
def resolve(self, type_: Type[T]) -> T:
impl_type = type_
if inspect.isabstract(type_):
impl_types = type_.__subclasses__()
if len(impl_types) == 0:
raise NotRegisteredTypeError(f"type: {type_}")
impl_type = None
for t, o in self.obj_map.items():
if isinstance(o, type_):
impl_type = t
break # just use first implementation

impl_type = impl_types[0]
if impl_type is None:
raise NotRegisteredTypeError(f"type: {type_}")

try:
obj = self.obj_map[impl_type]
Expand All @@ -32,7 +35,7 @@ def resolve(self, type_: Type[T]) -> T:

@abc.abstractmethod
def compose(self) -> None:
pass
...


class NotRegisteredTypeError(Exception):
Expand Down

0 comments on commit 850fff8

Please sign in to comment.