Skip to content

Commit 2e021bf

Browse files
committed
Added type hint to command pattern
1 parent 3514739 commit 2e021bf

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

patterns/behavioral/command.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,57 @@
2020
https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects
2121
"""
2222

23+
from typing import Union
24+
2325

2426
class HideFileCommand:
2527
"""
2628
A command to hide a file given its name
2729
"""
2830

29-
def __init__(self):
31+
def __init__(self) -> None:
3032
# an array of files hidden, to undo them as needed
3133
self._hidden_files = []
3234

33-
def execute(self, filename):
34-
print(f'hiding {filename}')
35+
def execute(self, filename: str) -> None:
36+
print(f"hiding {filename}")
3537
self._hidden_files.append(filename)
3638

37-
def undo(self):
39+
def undo(self) -> None:
3840
filename = self._hidden_files.pop()
39-
print(f'un-hiding {filename}')
41+
print(f"un-hiding {filename}")
4042

4143

4244
class DeleteFileCommand:
4345
"""
4446
A command to delete a file given its name
4547
"""
4648

47-
def __init__(self):
49+
def __init__(self) -> None:
4850
# an array of deleted files, to undo them as needed
4951
self._deleted_files = []
5052

51-
def execute(self, filename):
52-
print(f'deleting {filename}')
53+
def execute(self, filename: str) -> None:
54+
print(f"deleting {filename}")
5355
self._deleted_files.append(filename)
5456

55-
def undo(self):
57+
def undo(self) -> None:
5658
filename = self._deleted_files.pop()
57-
print(f'restoring {filename}')
59+
print(f"restoring {filename}")
5860

5961

6062
class MenuItem:
6163
"""
6264
The invoker class. Here it is items in a menu.
6365
"""
6466

65-
def __init__(self, command):
67+
def __init__(self, command: Union[HideFileCommand, DeleteFileCommand]) -> None:
6668
self._command = command
6769

68-
def on_do_press(self, filename):
70+
def on_do_press(self, filename: str) -> None:
6971
self._command.execute(filename)
7072

71-
def on_undo_press(self):
73+
def on_undo_press(self) -> None:
7274
self._command.undo()
7375

7476

@@ -101,4 +103,5 @@ def main():
101103

102104
if __name__ == "__main__":
103105
import doctest
106+
104107
doctest.testmod()

0 commit comments

Comments
 (0)