Skip to content

Commit af3be65

Browse files
committed
Added type hints to proxy pattern
1 parent 8894264 commit af3be65

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ __pycache__
33
.idea
44
*.egg-info/
55
.tox/
6+
venv

patterns/structural/proxy.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
without changing its interface.
1616
"""
1717

18+
from typing import Union
19+
1820

1921
class Subject:
2022
"""
@@ -26,7 +28,7 @@ class Subject:
2628
able to use RealSubject or Proxy interchangeably with no change in code.
2729
"""
2830

29-
def do_the_job(self, user):
31+
def do_the_job(self, user: str) -> None:
3032
raise NotImplementedError()
3133

3234

@@ -36,30 +38,31 @@ class RealSubject(Subject):
3638
good example.
3739
"""
3840

39-
def do_the_job(self, user):
40-
print(f'I am doing the job for {user}')
41+
def do_the_job(self, user: str) -> None:
42+
print(f"I am doing the job for {user}")
4143

4244

4345
class Proxy(Subject):
44-
def __init__(self):
46+
def __init__(self) -> None:
4547
self._real_subject = RealSubject()
4648

47-
def do_the_job(self, user):
49+
def do_the_job(self, user: str) -> None:
4850
"""
4951
logging and controlling access are some examples of proxy usages.
5052
"""
5153

52-
print(f'[log] Doing the job for {user} is requested.')
54+
print(f"[log] Doing the job for {user} is requested.")
5355

54-
if user == 'admin':
56+
if user == "admin":
5557
self._real_subject.do_the_job(user)
5658
else:
57-
print(f'[log] I can do the job just for `admins`.')
59+
print(f"[log] I can do the job just for `admins`.")
5860

5961

60-
def client(job_doer, user):
62+
def client(job_doer: Union[RealSubject, Proxy], user: str) -> None:
6163
job_doer.do_the_job(user)
6264

65+
6366
def main():
6467
"""
6568
>>> proxy = Proxy()
@@ -82,6 +85,7 @@ def main():
8285
"""
8386

8487

85-
if __name__ == '__main__':
88+
if __name__ == "__main__":
8689
import doctest
87-
doctest.testmod()
90+
91+
doctest.testmod()

0 commit comments

Comments
 (0)