diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index a5f5537b1..8ae171d71 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -6,7 +6,7 @@ Nodes contain data and also may link to other nodes: - Last node: points to null """ -from typing import Any +from typing import Any, Optional class Node: @@ -17,7 +17,7 @@ class Node: class LinkedList: def __init__(self) -> None: - self.head = None + self.head: Optional[Node] = None self.size = 0 def add(self, item: Any) -> None: @@ -25,7 +25,10 @@ class LinkedList: self.size += 1 def remove(self) -> Any: - if self.is_empty(): + # Switched 'self.is_empty()' to 'self.head is None' + # because mypy was considering the possibility that 'self.head' + # can be None in below else part and giving error + if self.head is None: return None else: item = self.head.item @@ -50,7 +53,7 @@ class LinkedList: else: iterate = self.head item_str = "" - item_list = [] + item_list: list[str] = [] while iterate: item_list.append(str(iterate.item)) iterate = iterate.next diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index f67c1e8f2..42794ba79 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,10 +1,10 @@ -from typing import Any +from typing import Any, Iterator, Optional class Node: def __init__(self, data: Any): - self.data = data - self.next = None + self.data: Any = data + self.next: Optional[Node] = None class CircularLinkedList: @@ -12,7 +12,7 @@ class CircularLinkedList: self.head = None self.tail = None - def __iter__(self): + def __iter__(self) -> Iterator[Any]: node = self.head while self.head: yield node.data @@ -54,10 +54,10 @@ class CircularLinkedList: def delete_front(self): return self.delete_nth(0) - def delete_tail(self) -> None: + def delete_tail(self) -> Any: return self.delete_nth(len(self) - 1) - def delete_nth(self, index: int = 0): + def delete_nth(self, index: int = 0) -> Any: if not 0 <= index < len(self): raise IndexError("list index out of range.") delete_node = self.head @@ -76,7 +76,7 @@ class CircularLinkedList: self.tail = temp return delete_node.data - def is_empty(self): + def is_empty(self) -> bool: return len(self) == 0 diff --git a/data_structures/linked_list/has_loop.py b/data_structures/linked_list/has_loop.py index 405ece7e2..a155ab4c7 100644 --- a/data_structures/linked_list/has_loop.py +++ b/data_structures/linked_list/has_loop.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Optional class ContainsLoopError(Exception): @@ -7,8 +7,8 @@ class ContainsLoopError(Exception): class Node: def __init__(self, data: Any) -> None: - self.data = data - self.next_node = None + self.data: Any = data + self.next_node: Optional[Node] = None def __iter__(self): node = self diff --git a/data_structures/linked_list/middle_element_of_linked_list.py b/data_structures/linked_list/middle_element_of_linked_list.py index 185c4ccbb..296696897 100644 --- a/data_structures/linked_list/middle_element_of_linked_list.py +++ b/data_structures/linked_list/middle_element_of_linked_list.py @@ -1,5 +1,8 @@ +from typing import Optional + + class Node: - def __init__(self, data: int) -> int: + def __init__(self, data: int) -> None: self.data = data self.next = None @@ -14,7 +17,7 @@ class LinkedList: self.head = new_node return self.head.data - def middle_element(self) -> int: + def middle_element(self) -> Optional[int]: """ >>> link = LinkedList() >>> link.middle_element() @@ -54,6 +57,7 @@ class LinkedList: return slow_pointer.data else: print("No element found.") + return None if __name__ == "__main__": diff --git a/data_structures/linked_list/skip_list.py b/data_structures/linked_list/skip_list.py index ee0b44607..be30592ec 100644 --- a/data_structures/linked_list/skip_list.py +++ b/data_structures/linked_list/skip_list.py @@ -5,14 +5,14 @@ https://epaperpress.com/sortsearch/download/skiplist.pdf from __future__ import annotations from random import random -from typing import Generic, TypeVar +from typing import Generic, Optional, TypeVar, Union KT = TypeVar("KT") VT = TypeVar("VT") class Node(Generic[KT, VT]): - def __init__(self, key: KT, value: VT): + def __init__(self, key: Union[KT, str] = "root", value: Optional[VT] = None): self.key = key self.value = value self.forward: list[Node[KT, VT]] = [] @@ -49,7 +49,7 @@ class Node(Generic[KT, VT]): class SkipList(Generic[KT, VT]): def __init__(self, p: float = 0.5, max_level: int = 16): - self.head = Node("root", None) + self.head: Node[KT, VT] = Node[KT, VT]() self.level = 0 self.p = p self.max_level = max_level