Skip to content

Commit 193ee4c

Browse files
committedFeb 17, 2022
feat(typing): fix all problems by mypy
re faif#373
1 parent 1f2bb45 commit 193ee4c

File tree

9 files changed

+32
-27
lines changed

9 files changed

+32
-27
lines changed
 

‎patterns/behavioral/catalog.py

+6-3
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

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

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

+2-1
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

‎patterns/behavioral/registry.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from typing import Dict
2+
3+
14
class RegistryHolder(type):
25

3-
REGISTRY = {}
6+
REGISTRY: Dict[str, "RegistryHolder"] = {}
47

58
def __new__(cls, name, bases, attrs):
69
new_cls = type.__new__(cls, name, bases, attrs)

‎patterns/behavioral/specification.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ def not_specification(self):
3939

4040

4141
class AndSpecification(CompositeSpecification):
42-
_one = Specification()
43-
_other = Specification()
44-
4542
def __init__(self, one, other):
46-
self._one = one
47-
self._other = other
43+
self._one: Specification = one
44+
self._other: Specification = other
4845

4946
def is_satisfied_by(self, candidate):
5047
return bool(
@@ -54,12 +51,9 @@ def is_satisfied_by(self, candidate):
5451

5552

5653
class OrSpecification(CompositeSpecification):
57-
_one = Specification()
58-
_other = Specification()
59-
6054
def __init__(self, one, other):
61-
self._one = one
62-
self._other = other
55+
self._one: Specification = one
56+
self._other: Specification = other
6357

6458
def is_satisfied_by(self, candidate):
6559
return bool(
@@ -69,10 +63,8 @@ def is_satisfied_by(self, candidate):
6963

7064

7165
class NotSpecification(CompositeSpecification):
72-
_wrapped = Specification()
73-
7466
def __init__(self, wrapped):
75-
self._wrapped = wrapped
67+
self._wrapped: Specification = wrapped
7668

7769
def is_satisfied_by(self, candidate):
7870
return bool(not self._wrapped.is_satisfied_by(candidate))

‎patterns/creational/borg.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
*TL;DR
3333
Provides singleton-like behavior sharing state between instances.
3434
"""
35+
from typing import Dict
3536

3637

3738
class Borg:
38-
_shared_state = {}
39+
_shared_state: Dict[str, str] = {}
3940

4041
def __init__(self):
4142
self.__dict__ = self._shared_state

‎patterns/structural/flyweight.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Card:
3434
# Could be a simple dict.
3535
# With WeakValueDictionary garbage collection can reclaim the object
3636
# when there are no other references to it.
37-
_pool = weakref.WeakValueDictionary()
37+
_pool: weakref.WeakValueDictionary = weakref.WeakValueDictionary()
3838

3939
def __new__(cls, value, suit):
4040
# If the object exists in the pool - just return it

‎setup.cfg

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ exclude = venv*
77
filterwarnings =
88
; ignore TestRunner class from facade example
99
ignore:.*test class 'TestRunner'.*:Warning
10+
11+
[mypy]
12+
python_version = 3.8
13+
ignore_missing_imports = True

0 commit comments

Comments
 (0)
Please sign in to comment.