Skip to content

Commit

Permalink
feat(typing): fix all problems by mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
yhay81 committed Feb 17, 2022
1 parent 1f2bb45 commit 193ee4c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 27 deletions.
9 changes: 6 additions & 3 deletions patterns/behavioral/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def main_method(self) -> None:
depending on self.param value
"""
self._instance_method_choices[self.param].__get__(self)()
self._instance_method_choices[self.param].__get__(self)() # type: ignore
# type ignore reason: https://github.com/python/mypy/issues/10206


class CatalogClass:
Expand Down Expand Up @@ -115,7 +116,8 @@ def main_method(self):
depending on self.param value
"""
self._class_method_choices[self.param].__get__(None, self.__class__)()
self._class_method_choices[self.param].__get__(None, self.__class__)() # type: ignore
# type ignore reason: https://github.com/python/mypy/issues/10206


class CatalogStatic:
Expand Down Expand Up @@ -151,7 +153,8 @@ def main_method(self) -> None:
depending on self.param value
"""

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


def main():
Expand Down
9 changes: 5 additions & 4 deletions patterns/behavioral/chain_of_responsibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
"""

from abc import ABC, abstractmethod
from typing import Optional, Tuple, TypeVar

T = TypeVar("T")
from typing import Optional, Tuple


class Handler(ABC):
def __init__(self, successor: Optional[T] = None):
def __init__(self, successor: Optional["Handler"] = None):
self.successor = successor

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


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


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

@staticmethod
def get_interval_from_db() -> Tuple[int, int]:
Expand Down
6 changes: 3 additions & 3 deletions patterns/behavioral/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects
"""

from typing import Union
from typing import List, Union


class HideFileCommand:
Expand All @@ -30,7 +30,7 @@ class HideFileCommand:

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

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

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

def execute(self, filename: str) -> None:
print(f"deleting {filename}")
Expand Down
3 changes: 2 additions & 1 deletion patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Provides the ability to restore an object to its previous state.
"""

from typing import Callable, List
from copy import copy, deepcopy


Expand All @@ -25,7 +26,7 @@ class Transaction:
"""

deep = False
states = []
states: List[Callable[[], None]] = []

def __init__(self, deep, *targets):
self.deep = deep
Expand Down
5 changes: 4 additions & 1 deletion patterns/behavioral/registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Dict


class RegistryHolder(type):

REGISTRY = {}
REGISTRY: Dict[str, "RegistryHolder"] = {}

def __new__(cls, name, bases, attrs):
new_cls = type.__new__(cls, name, bases, attrs)
Expand Down
18 changes: 5 additions & 13 deletions patterns/behavioral/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ def not_specification(self):


class AndSpecification(CompositeSpecification):
_one = Specification()
_other = Specification()

def __init__(self, one, other):
self._one = one
self._other = other
self._one: Specification = one
self._other: Specification = other

def is_satisfied_by(self, candidate):
return bool(
Expand All @@ -54,12 +51,9 @@ def is_satisfied_by(self, candidate):


class OrSpecification(CompositeSpecification):
_one = Specification()
_other = Specification()

def __init__(self, one, other):
self._one = one
self._other = other
self._one: Specification = one
self._other: Specification = other

def is_satisfied_by(self, candidate):
return bool(
Expand All @@ -69,10 +63,8 @@ def is_satisfied_by(self, candidate):


class NotSpecification(CompositeSpecification):
_wrapped = Specification()

def __init__(self, wrapped):
self._wrapped = wrapped
self._wrapped: Specification = wrapped

def is_satisfied_by(self, candidate):
return bool(not self._wrapped.is_satisfied_by(candidate))
Expand Down
3 changes: 2 additions & 1 deletion patterns/creational/borg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
*TL;DR
Provides singleton-like behavior sharing state between instances.
"""
from typing import Dict


class Borg:
_shared_state = {}
_shared_state: Dict[str, str] = {}

def __init__(self):
self.__dict__ = self._shared_state
Expand Down
2 changes: 1 addition & 1 deletion patterns/structural/flyweight.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Card:
# Could be a simple dict.
# With WeakValueDictionary garbage collection can reclaim the object
# when there are no other references to it.
_pool = weakref.WeakValueDictionary()
_pool: weakref.WeakValueDictionary = weakref.WeakValueDictionary()

def __new__(cls, value, suit):
# If the object exists in the pool - just return it
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ exclude = venv*
filterwarnings =
; ignore TestRunner class from facade example
ignore:.*test class 'TestRunner'.*:Warning

[mypy]
python_version = 3.8
ignore_missing_imports = True

0 comments on commit 193ee4c

Please sign in to comment.