From deb71167e7d5aabe24ae4a5c33e8e73dd3af8ece Mon Sep 17 00:00:00 2001 From: Ahmed Haj Abdel Khaleq <31858489+AhmedHaj@users.noreply.github.com> Date: Wed, 12 May 2021 02:22:42 -0400 Subject: [PATCH] [mypy] Fix type annotations for linked_stack.py, evaluate_postfix_notations.py, stack.py in data structures (#4409) * [mypy] Fix type annotations for linked_stack.py, next_greater_element.py, stack.py * Reformatted files according to black --- data_structures/stacks/evaluate_postfix_notations.py | 4 +++- data_structures/stacks/linked_stack.py | 6 ++++-- data_structures/stacks/stack.py | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index a03cb43bb..2a4baf9d6 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -1,3 +1,5 @@ +from typing import Any, List + """ The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation. @@ -21,7 +23,7 @@ def evaluate_postfix(postfix_notation: list) -> int: return 0 operations = {"+", "-", "*", "/"} - stack = [] + stack: List[Any] = [] for token in postfix_notation: if token in operations: diff --git a/data_structures/stacks/linked_stack.py b/data_structures/stacks/linked_stack.py index 1a2d07f20..0b9c9d45e 100644 --- a/data_structures/stacks/linked_stack.py +++ b/data_structures/stacks/linked_stack.py @@ -1,5 +1,5 @@ """ A Stack using a linked list like structure """ -from typing import Any +from typing import Any, Optional class Node: @@ -42,7 +42,7 @@ class LinkedStack: """ def __init__(self) -> None: - self.top = None + self.top: Optional[Node] = None def __iter__(self): node = self.top @@ -134,6 +134,8 @@ class LinkedStack: """ if self.is_empty(): raise IndexError("peek from empty stack") + + assert self.top is not None return self.top.data def clear(self) -> None: diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 276684e12..245d39b32 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -1,3 +1,6 @@ +from typing import List + + class StackOverflowError(BaseException): pass @@ -12,7 +15,7 @@ class Stack: """ def __init__(self, limit: int = 10): - self.stack = [] + self.stack: List[int] = [] self.limit = limit def __bool__(self) -> bool: