Skip to content

Commit ed4f000

Browse files
committed
refactor: add Lifetime and Implementation types
1 parent 8025209 commit ed4f000

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

starlette_di/service_collection.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
to register services.
33
"""
44

5-
from collections.abc import Callable
6-
from typing import Literal, TypeVar
7-
85
from .service_provider import Service, ServiceProvider
9-
10-
T = TypeVar('T')
6+
from .types import Implementation, Lifetime, T
117

128

139
class ServiceCollection:
@@ -49,7 +45,7 @@ def __init__(self) -> None:
4945
def add_singleton(
5046
self,
5147
service_type: type[T],
52-
implementation: type[T] | Callable[..., T] | None = None,
48+
implementation: Implementation = None,
5349
):
5450
"""Register a service as a singleton
5551
(one instance for the application lifetime).
@@ -61,7 +57,7 @@ def add_singleton(
6157
----------
6258
service_type : type[T]
6359
Service type.
64-
implementation : type[T] | Callable[..., T] | None, optional
60+
implementation : Implementation, optional
6561
Implementation type or factory function, by default None.
6662
6763
Returns
@@ -86,7 +82,7 @@ def add_singleton(
8682
def add_transient(
8783
self,
8884
service_type: type[T],
89-
implementation: type[T] | Callable[..., T] | None = None,
85+
implementation: Implementation = None,
9086
):
9187
"""Register a service as transient
9288
(new instance created each time it's requested).
@@ -98,7 +94,7 @@ def add_transient(
9894
----------
9995
service_type : type[T]
10096
Service type.
101-
implementation : type[T] | Callable[..., T] | None, optional
97+
implementation : Implementation, optional
10298
Implementation type or factory function, by default None.
10399
104100
Returns
@@ -123,7 +119,7 @@ def add_transient(
123119
def add_scoped(
124120
self,
125121
service_type: type[T],
126-
implementation: type[T] | Callable[..., T] | None = None,
122+
implementation: Implementation = None,
127123
):
128124
"""Register a service as scoped
129125
(one instance per request).
@@ -135,7 +131,7 @@ def add_scoped(
135131
----------
136132
service_type : type[T]
137133
Service type.
138-
implementation : type[T] | Callable[..., T] | None, optional
134+
implementation : Implementation, optional
139135
Implementation type or factory function, by default None.
140136
141137
Returns
@@ -159,9 +155,9 @@ def add_scoped(
159155

160156
def add(
161157
self,
162-
lifetime: Literal['singleton', 'scoped', 'transient'],
158+
lifetime: Lifetime,
163159
service_type: type[T],
164-
implementation: type[T] | Callable[..., T] | None = None,
160+
implementation: Implementation = None,
165161
):
166162
"""Registers a service.
167163
@@ -174,7 +170,7 @@ def add(
174170
Lifetime of the service.
175171
service_type : type[T]
176172
Service type.
177-
implementation : type[T] | Callable[..., T] | None, optional
173+
implementation : Implementation, optional
178174
Implementation type or factory function, by default None.
179175
180176
Returns

starlette_di/service_provider.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class to resolve services.
99
from collections.abc import Callable
1010
from dataclasses import dataclass
1111
from inspect import Parameter, isclass, signature
12-
from typing import Any, Generic, Literal, TypeVar
12+
from typing import Any, Generic, TypeVar
13+
14+
from .types import Implementation, Lifetime
1315

1416
T = TypeVar('T', bound=object)
1517

@@ -18,10 +20,10 @@ class to resolve services.
1820
class Service(Generic[T]):
1921
"""Service information."""
2022

21-
lifetime: Literal['singleton', 'scoped', 'transient']
23+
lifetime: Lifetime
2224
"""Lifetime of the service."""
2325

24-
implementation: type[T] | Callable[..., T] | None
26+
implementation: Implementation
2527
"""Implementation of the service.
2628
2729
It can be a class or a factory function.

starlette_di/types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""This module provides the types used in the DI system."""
22

33
from collections.abc import Awaitable, Callable
4-
from typing import ParamSpec, TypeVar
4+
from typing import Literal, ParamSpec, TypeVar
55

66
from starlette.responses import Response
77

88
T = TypeVar('T')
99
P = ParamSpec('P')
1010

1111
EndpointFunction = Callable[P, Awaitable[Response]]
12+
13+
Implementation = type[T] | Callable[..., T] | None
14+
15+
Lifetime = Literal['singleton', 'scoped', 'transient']

0 commit comments

Comments
 (0)