Coverage for app/core/base/base_uow.py: 100%
14 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
1from types import TracebackType
2from typing import TypeVar
4from sqlmodel import Session
6# サブクラス型を表す型パラメータ
7BaseUnitOfWorkType = TypeVar("BaseUnitOfWorkType", bound="BaseUnitOfWork")
9class BaseUnitOfWork:
10 def __init__(self, session: Session) -> None:
11 self.session: Session = session
13 def __enter__(self: BaseUnitOfWorkType) -> BaseUnitOfWorkType:
14 """__enter__の返り値がサブクラス型になるよう指定."""
15 return self # 型推論でサブクラス型 (T) が適用される
17 def __exit__(
18 self,
19 exc_type: type[BaseException] | None,
20 exc_value: BaseException | None,
21 traceback: TracebackType | None,
22 ) -> None:
23 """トランザクション管理."""
24 if exc_type is None:
25 self.session.commit()
26 else:
27 self.session.rollback()
28 self.session.close()