fix(mypy): fix major issues with mypy

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-22 21:07:40 +02:00
parent e28f578fc2
commit dc6905b5e2
Signed by: mfocko
GPG key ID: 7C47D46246790496
5 changed files with 14 additions and 14 deletions

View file

@ -10,7 +10,7 @@ flake8:
flake8 *.py flake8 *.py
mypy: mypy:
mypy --strict --disallow-any-explicit node.py avl.py wavl.py ravl.py mypy --strict --disallow-any-explicit node.py ranked_tree.py avl.py wavl.py ravl.py rbt.py
clean: clean:
rm -rf __pycache__ .hypothesis .mypy_cache .pytest_cache rm -rf __pycache__ .hypothesis .mypy_cache .pytest_cache

4
avl.py
View file

@ -114,7 +114,7 @@ class AVLTree(RankedTree[T]):
return factor != 0 return factor != 0
def __delete_fixup( def __delete_fixup(
self, x: Optional[Node[T]], parent: Optional[Node[T]] = None self, x: Node[T], parent: Optional[Node[T]] = None
) -> bool: ) -> bool:
factor = _balance_factor(x) factor = _balance_factor(x)
if factor == 0: if factor == 0:
@ -143,7 +143,7 @@ class AVLTree(RankedTree[T]):
if not node and not parent: if not node and not parent:
return return
if not node: if not node and parent:
node, parent = parent, parent.parent node, parent = parent, parent.parent
while node and self.__delete_fixup(node, parent): while node and self.__delete_fixup(node, parent):

View file

@ -2,6 +2,7 @@ from abc import abstractmethod
import enum import enum
from typing import ( from typing import (
Iterable, Iterable,
Iterator,
Optional, Optional,
Generic, Generic,
Tuple, Tuple,
@ -27,7 +28,7 @@ class NodeType(enum.IntEnum):
T = TypeVar("T", bound=Comparable) T = TypeVar("T", bound=Comparable)
class Node(Generic[T]): class Node(Generic[T], Iterable[T]):
def __init__( def __init__(
self, self,
value: T, value: T,
@ -61,7 +62,7 @@ class Node(Generic[T]):
def __str__(self) -> str: def __str__(self) -> str:
return f"Node(value={self.value}, rank={self.rank})" return f"Node(value={self.value}, rank={self.rank})"
def __iter__(self) -> Iterable[T]: def __iter__(self) -> Iterator[T]:
""" """
Yields: Yields:
Keys from the subtree rooted at the node in an inorder fashion. Keys from the subtree rooted at the node in an inorder fashion.
@ -113,7 +114,7 @@ class Node(Generic[T]):
@staticmethod @staticmethod
def find_parent_node( def find_parent_node(
value: T, node: "Node[T]", missing: bool = True value: T, node: "Node[T]", missing: bool = True
) -> "Node[T]": ) -> "Optional[Node[T]]":
new_node: "Optional[Node[T]]" = node new_node: "Optional[Node[T]]" = node
while new_node and (missing or new_node.value != value): while new_node and (missing or new_node.value != value):

View file

@ -174,8 +174,7 @@ class RankedTree(Generic[T]):
self._transplant(node, node.left) self._transplant(node, node.left)
else: else:
n = Node.minimum(node.right) n = Node.minimum(node.right)
node.value = n.value node.value, n.value = n.value, node.value
n.value = None
return self._delete_node(n) return self._delete_node(n)
return (y, parent) return (y, parent)

12
rbt.py
View file

@ -18,7 +18,7 @@ class Colour(enum.IntEnum):
Black = 1 Black = 1
def has_double_black(x: Optional[Node]) -> bool: def has_double_black(x: Optional[Node[T]]) -> bool:
""" """
Checks for double black child of x. Checks for double black child of x.
@ -64,8 +64,8 @@ class RBTree(RankedTree[T]):
z: Node[T], z: Node[T],
y: Optional[Node[T]], y: Optional[Node[T]],
right_child: Node[T], right_child: Node[T],
rotate_left: RotateFunction, rotate_left: RotateFunction[T],
rotate_right: RotateFunction, rotate_right: RotateFunction[T],
) -> Node[T]: ) -> Node[T]:
p = z.parent p = z.parent
pp = p.parent pp = p.parent
@ -114,9 +114,9 @@ class RBTree(RankedTree[T]):
x: Node[T], x: Node[T],
w: Node[T], w: Node[T],
parent: Node[T], parent: Node[T],
right: Callable[[Node[T]], Node[T]], right: Callable[[Node[T]], Optional[Node[T]]],
rotate_left: RotateFunction, rotate_left: RotateFunction[T],
rotate_right: RotateFunction, rotate_right: RotateFunction[T],
) -> Tuple[Optional[Node[T]], Optional[Node[T]]]: ) -> Tuple[Optional[Node[T]], Optional[Node[T]]]:
if Node.difference(w) == Colour.Red: if Node.difference(w) == Colour.Red:
# Case 1 # Case 1