Skip to content

[mypy] Fix type annotations for data_structures->linked_list directory files #4927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions data_structures/linked_list/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- Last node: points to null
"""

from typing import Any
from typing import Any, Optional


class Node:
Expand All @@ -17,15 +17,18 @@ def __init__(self, item: Any, next: Any) -> None:

class LinkedList:
def __init__(self) -> None:
self.head = None
self.head: Optional[Node] = None
self.size = 0

def add(self, item: Any) -> None:
self.head = Node(item, self.head)
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
Expand All @@ -50,7 +53,7 @@ def __str__(self) -> str:
else:
iterate = self.head
item_str = ""
item_list = []
item_list: list[str] = []
while iterate:
item_list.append(str(iterate.item))
iterate = iterate.next
Expand Down
14 changes: 7 additions & 7 deletions data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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:
def __init__(self):
self.head = None
self.tail = None

def __iter__(self):
def __iter__(self) -> Iterator[Any]:
node = self.head
while self.head:
yield node.data
Expand Down Expand Up @@ -54,10 +54,10 @@ def insert_nth(self, index: int, data: Any) -> None:
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
Expand All @@ -76,7 +76,7 @@ def delete_nth(self, index: int = 0):
self.tail = temp
return delete_node.data

def is_empty(self):
def is_empty(self) -> bool:
return len(self) == 0


Expand Down
6 changes: 3 additions & 3 deletions data_structures/linked_list/has_loop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Optional


class ContainsLoopError(Exception):
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions data_structures/linked_list/middle_element_of_linked_list.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -14,7 +17,7 @@ def push(self, new_data: int) -> int:
self.head = new_node
return self.head.data

def middle_element(self) -> int:
def middle_element(self) -> Optional[int]:
"""
>>> link = LinkedList()
>>> link.middle_element()
Expand Down Expand Up @@ -54,6 +57,7 @@ def middle_element(self) -> int:
return slow_pointer.data
else:
print("No element found.")
return None


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions data_structures/linked_list/skip_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
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]] = []
Expand Down Expand Up @@ -49,7 +49,7 @@ def level(self) -> int:

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
Expand Down