diff --git a/patterns/behavioral/catalog.py b/patterns/behavioral/catalog.py index f979ac32..7c91aa7d 100644 --- a/patterns/behavioral/catalog.py +++ b/patterns/behavioral/catalog.py @@ -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: @@ -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: @@ -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(): diff --git a/patterns/behavioral/chain_of_responsibility.py b/patterns/behavioral/chain_of_responsibility.py index d80b1633..9d46c4a8 100644 --- a/patterns/behavioral/chain_of_responsibility.py +++ b/patterns/behavioral/chain_of_responsibility.py @@ -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: @@ -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): @@ -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): @@ -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]: diff --git a/patterns/behavioral/command.py b/patterns/behavioral/command.py index b21d7f73..a88ea8be 100644 --- a/patterns/behavioral/command.py +++ b/patterns/behavioral/command.py @@ -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: @@ -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}") @@ -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}") diff --git a/patterns/behavioral/memento.py b/patterns/behavioral/memento.py index 3ec7e6be..e1d42fc2 100644 --- a/patterns/behavioral/memento.py +++ b/patterns/behavioral/memento.py @@ -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 @@ -25,7 +26,7 @@ class Transaction: """ deep = False - states = [] + states: List[Callable[[], None]] = [] def __init__(self, deep, *targets): self.deep = deep diff --git a/patterns/behavioral/registry.py b/patterns/behavioral/registry.py index a9fca443..d44a992e 100644 --- a/patterns/behavioral/registry.py +++ b/patterns/behavioral/registry.py @@ -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) diff --git a/patterns/behavioral/specification.py b/patterns/behavioral/specification.py index 07db267e..303ee513 100644 --- a/patterns/behavioral/specification.py +++ b/patterns/behavioral/specification.py @@ -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( @@ -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( @@ -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)) diff --git a/patterns/creational/borg.py b/patterns/creational/borg.py index e3f04b66..3ddc8c1d 100644 --- a/patterns/creational/borg.py +++ b/patterns/creational/borg.py @@ -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 diff --git a/patterns/structural/flyweight.py b/patterns/structural/flyweight.py index 29015705..fad17a8b 100644 --- a/patterns/structural/flyweight.py +++ b/patterns/structural/flyweight.py @@ -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 diff --git a/setup.cfg b/setup.cfg index 8f2de0ff..eb556c0a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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