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:
Illia Volochii 2022-04-17 04:29:35 +03:00 committed by GitHub
parent 20bf3c0a01
commit daca85d555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 17 deletions

View File

@ -100,7 +100,7 @@ Key features of the ``Dependency Injector``:
@inject
def main(service: Service = Provide[Container.service]):
def main(service: Service = Provide[Container.service]) -> None:
...

View File

@ -106,7 +106,7 @@ Key features of the ``Dependency Injector``:
@inject
def main(service: Service = Provide[Container.service]):
def main(service: Service = Provide[Container.service]) -> None:
...

View File

@ -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
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.
@ -66,14 +67,14 @@ Before:
class ApiClient:
def __init__(self):
def __init__(self) -> None:
self.api_key = os.getenv("API_KEY") # <-- dependency
self.timeout = os.getenv("TIMEOUT") # <-- dependency
self.timeout = int(os.getenv("TIMEOUT")) # <-- dependency
class Service:
def __init__(self):
def __init__(self) -> None:
self.api_client = ApiClient() # <-- dependency
@ -94,18 +95,18 @@ After:
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.timeout = timeout # <-- dependency is injected
class Service:
def __init__(self, api_client: ApiClient):
def __init__(self, api_client: ApiClient) -> None:
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(
api_client=ApiClient(
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(
api_client=ApiClient(
api_key=os.getenv("API_KEY"),
timeout=os.getenv("TIMEOUT"),
timeout=int(os.getenv("TIMEOUT")),
),
),
)
@ -182,7 +183,7 @@ the dependency.
@inject
def main(service: Service = Provide[Container.service]):
def main(service: Service = Provide[Container.service]) -> None:
...

View File

@ -3,18 +3,18 @@ import os
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.timeout = timeout # <-- dependency is injected
class Service:
def __init__(self, api_client: ApiClient):
def __init__(self, api_client: ApiClient) -> None:
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(
api_client=ApiClient(
api_key=os.getenv("API_KEY"),
timeout=os.getenv("TIMEOUT"),
timeout=int(os.getenv("TIMEOUT")),
),
),
)

View File

@ -23,7 +23,7 @@ class Container(containers.DeclarativeContainer):
@inject
def main(service: Service = Provide[Container.service]):
def main(service: Service = Provide[Container.service]) -> None:
...