Skip to content

Commit 39a828e

Browse files
committed
More precise typing for BaseService
1 parent 9e7777a commit 39a828e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
from abc import ABCMeta, abstractmethod
22
from collections.abc import Callable
3-
from typing import Any
43

54

6-
class BaseService(metaclass=ABCMeta):
5+
class BaseService[T](metaclass=ABCMeta):
76
"""This is a template of a a base service.
87
All services in the app should follow this rules:
98
* Input variables should be done at the __init__ phase
109
* Service should implement a single entrypoint without arguments
1110
1211
This is ok:
1312
@dataclass
14-
class UserCreator(BaseService):
13+
class UserCreator(BaseService[User]):
1514
first_name: str
16-
last_name: Optional[str]
15+
last_name: str | None
1716
1817
def act(self) -> User:
1918
return User.objects.create(first_name=self.first_name, last_name=self.last_name)
@@ -22,13 +21,13 @@ def act(self) -> User:
2221
2322
This is not ok:
2423
class UserCreator:
25-
def __call__(self, first_name: str, last_name: Optional[str]) -> User:
24+
def __call__(self, first_name: str, last_name: str | None) -> User:
2625
return User.objects.create(first_name=self.first_name, last_name=self.last_name)
2726
2827
For more implementation examples, check out https://github.com/tough-dev-school/education-backend/blob/master/src/apps/orders/services/order_course_changer.py
2928
"""
3029

31-
def __call__(self) -> Any:
30+
def __call__(self) -> T:
3231
self.validate()
3332
return self.act()
3433

@@ -41,5 +40,5 @@ def validate(self) -> None:
4140
validator()
4241

4342
@abstractmethod
44-
def act(self) -> Any:
43+
def act(self) -> T:
4544
raise NotImplementedError("Please implement in the service class")

0 commit comments

Comments
 (0)