Skip to content

Commit 2a469f4

Browse files
authored
Merge pull request faif#381 from 2ykwang/master
Added type hints
2 parents e639308 + d3d00e5 commit 2a469f4

File tree

7 files changed

+24
-19
lines changed

7 files changed

+24
-19
lines changed

patterns/behavioral/chaining_method.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1+
from __future__ import annotations
2+
3+
14
class Person:
2-
def __init__(self, name, action):
5+
def __init__(self, name: str, action: Action) -> None:
36
self.name = name
47
self.action = action
58

6-
def do_action(self):
9+
def do_action(self) -> Action:
710
print(self.name, self.action.name, end=" ")
811
return self.action
912

1013

1114
class Action:
12-
def __init__(self, name):
15+
def __init__(self, name: str) -> None:
1316
self.name = name
1417

15-
def amount(self, val):
18+
def amount(self, val: str) -> Action:
1619
print(val, end=" ")
1720
return self
1821

19-
def stop(self):
22+
def stop(self) -> None:
2023
print("then stop")
2124

2225

patterns/behavioral/iterator_alt.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*TL;DR
55
Traverses a container and accesses the container's elements.
66
"""
7+
from __future__ import annotations
78

89

910
class NumberWords:
@@ -17,14 +18,14 @@ class NumberWords:
1718
"five",
1819
)
1920

20-
def __init__(self, start, stop):
21+
def __init__(self, start: int, stop: int) -> None:
2122
self.start = start
2223
self.stop = stop
2324

24-
def __iter__(self): # this makes the class an Iterable
25+
def __iter__(self) -> NumberWords: # this makes the class an Iterable
2526
return self
2627

27-
def __next__(self): # this makes the class an Iterator
28+
def __next__(self) -> str: # this makes the class an Iterator
2829
if self.start > self.stop or self.start > len(self._WORD_MAP):
2930
raise StopIteration
3031
current = self.start

patterns/behavioral/observer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from __future__ import annotations
1313

1414
from contextlib import suppress
15-
from typing import List, Optional, Protocol
15+
from typing import Protocol
1616

1717

1818
# define a generic observer type
@@ -23,7 +23,7 @@ def update(self, subject: Subject) -> None:
2323

2424
class Subject:
2525
def __init__(self) -> None:
26-
self._observers: List[Observer] = []
26+
self._observers: list[Observer] = []
2727

2828
def attach(self, observer: Observer) -> None:
2929
if observer not in self._observers:
@@ -33,7 +33,7 @@ def detach(self, observer: Observer) -> None:
3333
with suppress(ValueError):
3434
self._observers.remove(observer)
3535

36-
def notify(self, modifier: Optional[Observer] = None) -> None:
36+
def notify(self, modifier: Observer | None = None) -> None:
3737
for observer in self._observers:
3838
if modifier != observer:
3939
observer.update(self)

patterns/behavioral/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def scan(self):
1818
self.pos += 1
1919
if self.pos == len(self.stations):
2020
self.pos = 0
21-
print("Scanning... Station is {} {}".format(self.stations[self.pos], self.name))
21+
print(f"Scanning... Station is {self.stations[self.pos]} {self.name}")
2222

2323

2424
class AmState(State):

patterns/behavioral/strategy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from __future__ import annotations
1212

13-
from typing import Callable, Type
13+
from typing import Callable
1414

1515

1616
class DiscountStrategyValidator: # Descriptor class for check perform
@@ -36,7 +36,7 @@ def __set__(self, obj: Order, value: Callable = None) -> None:
3636
else:
3737
setattr(obj, self.private_name, None)
3838

39-
def __get__(self, obj: object, objtype: Type = None):
39+
def __get__(self, obj: object, objtype: type = None):
4040
return getattr(obj, self.private_name)
4141

4242

patterns/creational/prototype.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020
*TL;DR
2121
Creates new object instances by cloning prototype.
2222
"""
23+
from __future__ import annotations
2324

24-
from typing import Any, Dict
25+
from typing import Any
2526

2627

2728
class Prototype:
2829
def __init__(self, value: str = "default", **attrs: Any) -> None:
2930
self.value = value
3031
self.__dict__.update(attrs)
3132

32-
def clone(self, **attrs: Any) -> "Prototype":
33+
def clone(self, **attrs: Any) -> Prototype:
3334
"""Clone a prototype and update inner attributes dictionary"""
3435
# Python in Practice, Mark Summerfield
3536
# copy.deepcopy can be used instead of next line.
@@ -42,7 +43,7 @@ class PrototypeDispatcher:
4243
def __init__(self):
4344
self._objects = {}
4445

45-
def get_objects(self) -> Dict[str, Prototype]:
46+
def get_objects(self) -> dict[str, Prototype]:
4647
"""Get all objects"""
4748
return self._objects
4849

patterns/fundamental/delegation_pattern.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from __future__ import annotations
1010

11-
from typing import Any, Callable, Union
11+
from typing import Any, Callable
1212

1313

1414
class Delegator:
@@ -31,7 +31,7 @@ class Delegator:
3131
def __init__(self, delegate: Delegate):
3232
self.delegate = delegate
3333

34-
def __getattr__(self, name: str) -> Union[Any, Callable]:
34+
def __getattr__(self, name: str) -> Any | Callable:
3535
attr = getattr(self.delegate, name)
3636

3737
if not callable(attr):

0 commit comments

Comments
 (0)