Skip to content

Commit 11449e6

Browse files
authored
Merge pull request faif#328 from rednafi/master
Added type hints to dependency injection pattern
2 parents 6da77a1 + 6479d33 commit 11449e6

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

patterns/dependency_injection.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,30 @@ def get_current_time_as_html_fragment(self):
2424
"""
2525

2626
import datetime
27+
from typing import Callable
2728

2829

2930
class ConstructorInjection:
30-
31-
def __init__(self, time_provider):
31+
def __init__(self, time_provider: Callable) -> None:
3232
self.time_provider = time_provider
3333

34-
def get_current_time_as_html_fragment(self):
34+
def get_current_time_as_html_fragment(self) -> str:
3535
current_time = self.time_provider()
36-
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
36+
current_time_as_html_fragment = '<span class="tinyBoldText">{}</span>'.format(
37+
current_time
38+
)
3739
return current_time_as_html_fragment
3840

3941

4042
class ParameterInjection:
41-
42-
def __init__(self):
43+
def __init__(self) -> None:
4344
pass
4445

45-
def get_current_time_as_html_fragment(self, time_provider):
46+
def get_current_time_as_html_fragment(self, time_provider: Callable) -> str:
4647
current_time = time_provider()
47-
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
48+
current_time_as_html_fragment = '<span class="tinyBoldText">{}</span>'.format(
49+
current_time
50+
)
4851
return current_time_as_html_fragment
4952

5053

@@ -54,16 +57,18 @@ class SetterInjection:
5457
def __init__(self):
5558
pass
5659

57-
def set_time_provider(self, time_provider):
60+
def set_time_provider(self, time_provider: Callable):
5861
self.time_provider = time_provider
5962

6063
def get_current_time_as_html_fragment(self):
6164
current_time = self.time_provider()
62-
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
65+
current_time_as_html_fragment = '<span class="tinyBoldText">{}</span>'.format(
66+
current_time
67+
)
6368
return current_time_as_html_fragment
6469

6570

66-
def production_code_time_provider():
71+
def production_code_time_provider() -> str:
6772
"""
6873
Production code version of the time provider (just a wrapper for formatting
6974
datetime for this example).
@@ -73,7 +78,7 @@ def production_code_time_provider():
7378
return current_time_formatted
7479

7580

76-
def midnight_time_provider():
81+
def midnight_time_provider() -> str:
7782
"""Hard-coded stub"""
7883
return "24:01"
7984

@@ -107,4 +112,5 @@ def main():
107112

108113
if __name__ == "__main__":
109114
import doctest
115+
110116
doctest.testmod(optionflags=doctest.ELLIPSIS)

0 commit comments

Comments
 (0)