mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Fix a few issues in the introduction (#580)
* Fix a statement about coupling and cohesion that is not always true https://enterprisecraftsmanship.com/posts/cohesion-coupling-difference/#_types_of_code_from_a_cohesion_and_coupling_perspective * Fix a typing issue in an example `ApiClient` expects timeout to be an integer (based on a type hint), but `os.getenv` returns a string when `TIMEOUT` is set. * Specify the `None` return type where it is missed * Fix typing issues in some other places * Edit a statement about coupling and cohesion Co-authored-by: Roman Mogylatov <rmogilatov@gmail.com> Co-authored-by: Roman Mogylatov <rmogilatov@gmail.com>
This commit is contained in:
parent
20bf3c0a01
commit
daca85d555
|
@ -100,7 +100,7 @@ Key features of the ``Dependency Injector``:
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def main(service: Service = Provide[Container.service]):
|
def main(service: Service = Provide[Container.service]) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ Key features of the ``Dependency Injector``:
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def main(service: Service = Provide[Container.service]):
|
def main(service: Service = Provide[Container.service]) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,8 @@ Coupling and cohesion are about how tough the components are tied.
|
||||||
- **High cohesion**. High cohesion is like using the screws. Very easy to disassemble and
|
- **High cohesion**. High cohesion is like using the screws. Very easy to disassemble and
|
||||||
assemble back or assemble a different way. It is an opposite to high coupling.
|
assemble back or assemble a different way. It is an opposite to high coupling.
|
||||||
|
|
||||||
When the cohesion is high the coupling is low.
|
Cohesion often correlates with coupling. Higher cohesion usually leads to lower coupling, and vice
|
||||||
|
versa.
|
||||||
|
|
||||||
Low coupling brings a flexibility. Your code becomes easier to change and test.
|
Low coupling brings a flexibility. Your code becomes easier to change and test.
|
||||||
|
|
||||||
|
@ -66,14 +67,14 @@ Before:
|
||||||
|
|
||||||
class ApiClient:
|
class ApiClient:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.api_key = os.getenv("API_KEY") # <-- dependency
|
self.api_key = os.getenv("API_KEY") # <-- dependency
|
||||||
self.timeout = os.getenv("TIMEOUT") # <-- dependency
|
self.timeout = int(os.getenv("TIMEOUT")) # <-- dependency
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.api_client = ApiClient() # <-- dependency
|
self.api_client = ApiClient() # <-- dependency
|
||||||
|
|
||||||
|
|
||||||
|
@ -94,18 +95,18 @@ After:
|
||||||
|
|
||||||
class ApiClient:
|
class ApiClient:
|
||||||
|
|
||||||
def __init__(self, api_key: str, timeout: int):
|
def __init__(self, api_key: str, timeout: int) -> None:
|
||||||
self.api_key = api_key # <-- dependency is injected
|
self.api_key = api_key # <-- dependency is injected
|
||||||
self.timeout = timeout # <-- dependency is injected
|
self.timeout = timeout # <-- dependency is injected
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
|
|
||||||
def __init__(self, api_client: ApiClient):
|
def __init__(self, api_client: ApiClient) -> None:
|
||||||
self.api_client = api_client # <-- dependency is injected
|
self.api_client = api_client # <-- dependency is injected
|
||||||
|
|
||||||
|
|
||||||
def main(service: Service): # <-- dependency is injected
|
def main(service: Service) -> None: # <-- dependency is injected
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@ -114,7 +115,7 @@ After:
|
||||||
service=Service(
|
service=Service(
|
||||||
api_client=ApiClient(
|
api_client=ApiClient(
|
||||||
api_key=os.getenv("API_KEY"),
|
api_key=os.getenv("API_KEY"),
|
||||||
timeout=os.getenv("TIMEOUT"),
|
timeout=int(os.getenv("TIMEOUT")),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -137,7 +138,7 @@ Now you need to assemble and inject the objects like this:
|
||||||
service=Service(
|
service=Service(
|
||||||
api_client=ApiClient(
|
api_client=ApiClient(
|
||||||
api_key=os.getenv("API_KEY"),
|
api_key=os.getenv("API_KEY"),
|
||||||
timeout=os.getenv("TIMEOUT"),
|
timeout=int(os.getenv("TIMEOUT")),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -182,7 +183,7 @@ the dependency.
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def main(service: Service = Provide[Container.service]):
|
def main(service: Service = Provide[Container.service]) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,18 +3,18 @@ import os
|
||||||
|
|
||||||
class ApiClient:
|
class ApiClient:
|
||||||
|
|
||||||
def __init__(self, api_key: str, timeout: int):
|
def __init__(self, api_key: str, timeout: int) -> None:
|
||||||
self.api_key = api_key # <-- dependency is injected
|
self.api_key = api_key # <-- dependency is injected
|
||||||
self.timeout = timeout # <-- dependency is injected
|
self.timeout = timeout # <-- dependency is injected
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
|
|
||||||
def __init__(self, api_client: ApiClient):
|
def __init__(self, api_client: ApiClient) -> None:
|
||||||
self.api_client = api_client # <-- dependency is injected
|
self.api_client = api_client # <-- dependency is injected
|
||||||
|
|
||||||
|
|
||||||
def main(service: Service): # <-- dependency is injected
|
def main(service: Service) -> None: # <-- dependency is injected
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ if __name__ == "__main__":
|
||||||
service=Service(
|
service=Service(
|
||||||
api_client=ApiClient(
|
api_client=ApiClient(
|
||||||
api_key=os.getenv("API_KEY"),
|
api_key=os.getenv("API_KEY"),
|
||||||
timeout=os.getenv("TIMEOUT"),
|
timeout=int(os.getenv("TIMEOUT")),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
|
@ -23,7 +23,7 @@ class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def main(service: Service = Provide[Container.service]):
|
def main(service: Service = Provide[Container.service]) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user