From daca85d5550516b9bbc99113b0c1a25da46d8467 Mon Sep 17 00:00:00 2001 From: Illia Volochii Date: Sun, 17 Apr 2022 04:29:35 +0300 Subject: [PATCH] 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 Co-authored-by: Roman Mogylatov --- README.rst | 2 +- docs/index.rst | 2 +- docs/introduction/di_in_python.rst | 21 +++++++++++---------- examples/demo/after.py | 8 ++++---- examples/demo/with_di.py | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.rst b/README.rst index f9be191d..e3b11e7a 100644 --- a/README.rst +++ b/README.rst @@ -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: ... diff --git a/docs/index.rst b/docs/index.rst index 6ff500d1..9fca971c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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: ... diff --git a/docs/introduction/di_in_python.rst b/docs/introduction/di_in_python.rst index 8e16a5f8..46c46beb 100644 --- a/docs/introduction/di_in_python.rst +++ b/docs/introduction/di_in_python.rst @@ -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: ... diff --git a/examples/demo/after.py b/examples/demo/after.py index 92240644..dad25cec 100644 --- a/examples/demo/after.py +++ b/examples/demo/after.py @@ -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")), ), ), ) diff --git a/examples/demo/with_di.py b/examples/demo/with_di.py index bf154976..58114e55 100644 --- a/examples/demo/with_di.py +++ b/examples/demo/with_di.py @@ -23,7 +23,7 @@ class Container(containers.DeclarativeContainer): @inject -def main(service: Service = Provide[Container.service]): +def main(service: Service = Provide[Container.service]) -> None: ...