From bc4f6a826abdde8de39533512c1da68ee43f0454 Mon Sep 17 00:00:00 2001 From: Archaengel Date: Sun, 24 Oct 2021 00:56:58 -0700 Subject: [PATCH 1/2] Fix mypy annotations for stack_using_dll.py --- data_structures/stacks/stack_using_dll.py | 38 +++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py index 75e0cd20640d..a5c32b521952 100644 --- a/data_structures/stacks/stack_using_dll.py +++ b/data_structures/stacks/stack_using_dll.py @@ -1,15 +1,19 @@ # A complete working Python program to demonstrate all # stack operations using a doubly linked list +from typing import Generic, Optional, TypeVar -class Node: - def __init__(self, data): +T = TypeVar("T") + + +class Node(Generic[T]): + def __init__(self, data: T): self.data = data # Assign data - self.next = None # Initialize next as null - self.prev = None # Initialize prev as null + self.next: Optional[Node[T]] = None # Initialize next as null + self.prev: Optional[Node[T]] = None # Initialize prev as null -class Stack: +class Stack(Generic[T]): """ >>> stack = Stack() >>> stack.is_empty() @@ -35,10 +39,10 @@ class Stack: 2->1->0-> """ - def __init__(self): - self.head = None + def __init__(self) -> None: + self.head: Optional[Node[T]] = None - def push(self, data): + def push(self, data: T) -> None: """add a Node to the stack""" if self.head is None: self.head = Node(data) @@ -49,21 +53,23 @@ def push(self, data): new_node.prev = None self.head = new_node - def pop(self): + def pop(self) -> Optional[T]: """pop the top element off the stack""" if self.head is None: return None else: + assert self.head is not None temp = self.head.data self.head = self.head.next - self.head.prev = None + if self.head is not None: + self.head.prev = None return temp - def top(self): + def top(self) -> Optional[T]: """return the top element of the stack""" - return self.head.data + return self.head.data if self.head is not None else None - def __len__(self): + def __len__(self) -> int: temp = self.head count = 0 while temp is not None: @@ -71,10 +77,10 @@ def __len__(self): temp = temp.next return count - def is_empty(self): + def is_empty(self) -> bool: return self.head is None - def print_stack(self): + def print_stack(self) -> None: print("stack elements are:") temp = self.head while temp is not None: @@ -86,7 +92,7 @@ def print_stack(self): if __name__ == "__main__": # Start with the empty stack - stack = Stack() + stack: Stack[int] = Stack() # Insert 4 at the beginning. So stack becomes 4->None print("Stack operations using Doubly LinkedList") From b83038e21bd60e7e1158367fff967b410ffbfb14 Mon Sep 17 00:00:00 2001 From: Archaengel Date: Tue, 26 Oct 2021 11:43:45 -0700 Subject: [PATCH 2/2] Replace Optional with inline union type --- data_structures/stacks/stack_using_dll.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py index a5c32b521952..a129665f209f 100644 --- a/data_structures/stacks/stack_using_dll.py +++ b/data_structures/stacks/stack_using_dll.py @@ -1,7 +1,9 @@ # A complete working Python program to demonstrate all # stack operations using a doubly linked list -from typing import Generic, Optional, TypeVar +from __future__ import annotations + +from typing import Generic, TypeVar T = TypeVar("T") @@ -9,8 +11,8 @@ class Node(Generic[T]): def __init__(self, data: T): self.data = data # Assign data - self.next: Optional[Node[T]] = None # Initialize next as null - self.prev: Optional[Node[T]] = None # Initialize prev as null + self.next: Node[T] | None = None # Initialize next as null + self.prev: Node[T] | None = None # Initialize prev as null class Stack(Generic[T]): @@ -40,7 +42,7 @@ class Stack(Generic[T]): """ def __init__(self) -> None: - self.head: Optional[Node[T]] = None + self.head: Node[T] | None = None def push(self, data: T) -> None: """add a Node to the stack""" @@ -53,7 +55,7 @@ def push(self, data: T) -> None: new_node.prev = None self.head = new_node - def pop(self) -> Optional[T]: + def pop(self) -> T | None: """pop the top element off the stack""" if self.head is None: return None @@ -65,7 +67,7 @@ def pop(self) -> Optional[T]: self.head.prev = None return temp - def top(self) -> Optional[T]: + def top(self) -> T | None: """return the top element of the stack""" return self.head.data if self.head is not None else None