Skip to content

[mypy] Fix type annotations for stack.py #5566

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 26, 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
2 changes: 1 addition & 1 deletion data_structures/stacks/balanced_parentheses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def balanced_parentheses(parentheses: str) -> bool:
>>> balanced_parentheses("")
True
"""
stack = Stack()
stack: Stack[str] = Stack()
bracket_pairs = {"(": ")", "[": "]", "{": "}"}
for bracket in parentheses:
if bracket in bracket_pairs:
Expand Down
4 changes: 2 additions & 2 deletions data_structures/stacks/dijkstras_two_stack_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def dijkstras_two_stack_algorithm(equation: str) -> int:
"""
operators = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub}

operand_stack = Stack()
operator_stack = Stack()
operand_stack: Stack[int] = Stack()
operator_stack: Stack[str] = Stack()

for i in equation:
if i.isdigit():
Expand Down
2 changes: 1 addition & 1 deletion data_structures/stacks/infix_to_postfix_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def infix_to_postfix(expression_str: str) -> str:
"""
if not balanced_parentheses(expression_str):
raise ValueError("Mismatched parentheses")
stack = Stack()
stack: Stack[str] = Stack()
postfix = []
for char in expression_str:
if char.isalpha() or char.isdigit():
Expand Down
18 changes: 11 additions & 7 deletions data_structures/stacks/stack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

from typing import Generic, TypeVar

T = TypeVar("T")


class StackOverflowError(BaseException):
pass
Expand All @@ -9,7 +13,7 @@ class StackUnderflowError(BaseException):
pass


class Stack:
class Stack(Generic[T]):
"""A stack is an abstract data type that serves as a collection of
elements with two principal operations: push() and pop(). push() adds an
element to the top of the stack, and pop() removes an element from the top
Expand All @@ -19,7 +23,7 @@ class Stack:
"""

def __init__(self, limit: int = 10):
self.stack: list[int] = []
self.stack: list[T] = []
self.limit = limit

def __bool__(self) -> bool:
Expand All @@ -28,13 +32,13 @@ def __bool__(self) -> bool:
def __str__(self) -> str:
return str(self.stack)

def push(self, data):
def push(self, data: T) -> None:
"""Push an element to the top of the stack."""
if len(self.stack) >= self.limit:
raise StackOverflowError
self.stack.append(data)

def pop(self):
def pop(self) -> T:
"""
Pop an element off of the top of the stack.

Expand All @@ -47,7 +51,7 @@ def pop(self):
raise StackUnderflowError
return self.stack.pop()

def peek(self):
def peek(self) -> T:
"""
Peek at the top-most element of the stack.

Expand All @@ -71,7 +75,7 @@ def size(self) -> int:
"""Return the size of the stack."""
return len(self.stack)

def __contains__(self, item) -> bool:
def __contains__(self, item: T) -> bool:
"""Check if item is in stack"""
return item in self.stack

Expand All @@ -80,7 +84,7 @@ def test_stack() -> None:
"""
>>> test_stack()
"""
stack = Stack(10)
stack: Stack[int] = Stack(10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that the reason that we used Stack(Generic[T]) instead of Stack(Any) was so that we did not need to declare the type of each instance at creation time.

assert bool(stack) is False
assert stack.is_empty() is True
assert stack.is_full() is False
Expand Down