Skip to content

Commit 7239b18

Browse files
authored
Merge branch 'faif:master' into master
2 parents 2ee57e2 + 79d1275 commit 7239b18

35 files changed

Lines changed: 292 additions & 128 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ __pycache__
55
.tox/
66
venv
77
.vscode/
8-
.python-version
8+
.python-version
9+
.coverage

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ language: python
44

55
jobs:
66
include:
7-
- python: "3.7"
8-
env: TOXENV=py37
7+
- python: "3.8"
8+
env: TOXENV=py38
99
- python: "3.9"
1010
env: TOXENV=py39
1111

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ ifeq ("$(wildcard venv/bin/pip-sync)","")
3939
endif
4040

4141
# pip-tools
42-
@pip-compile --upgrade requirements-dev.txt
43-
@pip-compile --upgrade requirements.txt
44-
@pip-sync requirements-dev.txt requirements.txt
42+
# @pip-compile --upgrade requirements-dev.txt
43+
@pip-sync requirements-dev.txt
4544

4645

4746
.PHONY: pylinter
@@ -85,4 +84,4 @@ endif
8584
--select "B,C,E,F,W,T4,B9" \
8685
--ignore "E203,E266,E501,W503,F403,F401,E402" \
8786
--exclude ".git,__pycache__,old, build, \
88-
dist, venv" $(path)
87+
dist, venv, .tox" $(path)

patterns/behavioral/catalog.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def main_method(self) -> None:
7777
7878
depending on self.param value
7979
"""
80-
self._instance_method_choices[self.param].__get__(self)()
80+
self._instance_method_choices[self.param].__get__(self)() # type: ignore
81+
# type ignore reason: https://github.com/python/mypy/issues/10206
8182

8283

8384
class CatalogClass:
@@ -115,7 +116,8 @@ def main_method(self):
115116
116117
depending on self.param value
117118
"""
118-
self._class_method_choices[self.param].__get__(None, self.__class__)()
119+
self._class_method_choices[self.param].__get__(None, self.__class__)() # type: ignore
120+
# type ignore reason: https://github.com/python/mypy/issues/10206
119121

120122

121123
class CatalogStatic:
@@ -151,7 +153,8 @@ def main_method(self) -> None:
151153
depending on self.param value
152154
"""
153155

154-
self._static_method_choices[self.param].__get__(None, self.__class__)()
156+
self._static_method_choices[self.param].__get__(None, self.__class__)() # type: ignore
157+
# type ignore reason: https://github.com/python/mypy/issues/10206
155158

156159

157160
def main():

patterns/behavioral/chain_of_responsibility.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
"""
2020

2121
from abc import ABC, abstractmethod
22-
from typing import Optional, Tuple, TypeVar
23-
24-
T = TypeVar("T")
22+
from typing import Optional, Tuple
2523

2624

2725
class Handler(ABC):
28-
def __init__(self, successor: Optional[T] = None):
26+
def __init__(self, successor: Optional["Handler"] = None):
2927
self.successor = successor
3028

3129
def handle(self, request: int) -> None:
@@ -55,6 +53,7 @@ def check_range(request: int) -> Optional[bool]:
5553
if 0 <= request < 10:
5654
print(f"request {request} handled in handler 0")
5755
return True
56+
return None
5857

5958

6059
class ConcreteHandler1(Handler):
@@ -66,6 +65,7 @@ def check_range(self, request: int) -> Optional[bool]:
6665
if self.start <= request < self.end:
6766
print(f"request {request} handled in handler 1")
6867
return True
68+
return None
6969

7070

7171
class ConcreteHandler2(Handler):
@@ -76,6 +76,7 @@ def check_range(self, request: int) -> Optional[bool]:
7676
if start <= request < end:
7777
print(f"request {request} handled in handler 2")
7878
return True
79+
return None
7980

8081
@staticmethod
8182
def get_interval_from_db() -> Tuple[int, int]:

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/command.py

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

23-
from typing import Union
23+
from typing import List, Union
2424

2525

2626
class HideFileCommand:
@@ -30,7 +30,7 @@ class HideFileCommand:
3030

3131
def __init__(self) -> None:
3232
# an array of files hidden, to undo them as needed
33-
self._hidden_files = []
33+
self._hidden_files: List[str] = []
3434

3535
def execute(self, filename: str) -> None:
3636
print(f"hiding {filename}")
@@ -48,7 +48,7 @@ class DeleteFileCommand:
4848

4949
def __init__(self) -> None:
5050
# an array of deleted files, to undo them as needed
51-
self._deleted_files = []
51+
self._deleted_files: List[str] = []
5252

5353
def execute(self, filename: str) -> None:
5454
print(f"deleting {filename}")

patterns/behavioral/iterator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ def count_to(count):
1414

1515

1616
# Test the generator
17-
count_to_two = lambda: count_to(2)
18-
count_to_five = lambda: count_to(5)
17+
def count_to_two() -> None:
18+
return count_to(2)
19+
20+
21+
def count_to_five() -> None:
22+
return count_to(5)
1923

2024

2125
def main():

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/memento.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Provides the ability to restore an object to its previous state.
66
"""
77

8+
from typing import Callable, List
89
from copy import copy, deepcopy
910

1011

@@ -25,7 +26,7 @@ class Transaction:
2526
"""
2627

2728
deep = False
28-
states = []
29+
states: List[Callable[[], None]] = []
2930

3031
def __init__(self, deep, *targets):
3132
self.deep = deep
@@ -50,6 +51,12 @@ def __init__(self, method):
5051
self.method = method
5152

5253
def __get__(self, obj, T):
54+
"""
55+
A decorator that makes a function transactional.
56+
57+
:param method: The function to be decorated.
58+
"""
59+
5360
def transaction(*args, **kwargs):
5461
state = memento(obj)
5562
try:

0 commit comments

Comments
 (0)