Skip to content

[mypy] Fix type annotations for linked_stack.py #5576

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 5 commits into from
Oct 31, 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
7 changes: 5 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
* [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py)
* [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py)
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
* [Pressure Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/pressure_conversions.py)
* [Rgb Hsv Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/rgb_hsv_conversion.py)
* [Roman Numerals](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_numerals.py)
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)
Expand Down Expand Up @@ -196,12 +197,12 @@
* [Evaluate Postfix Notations](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/evaluate_postfix_notations.py)
* [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py)
* [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py)
* [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py)
* [Next Greater Element](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/next_greater_element.py)
* [Postfix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/postfix_evaluation.py)
* [Prefix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/prefix_evaluation.py)
* [Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack.py)
* [Stack Using Dll](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_using_dll.py)
* [Stack With Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_doubly_linked_list.py)
* [Stack With Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_singly_linked_list.py)
* [Stock Span Problem](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stock_span_problem.py)
* Trie
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
Expand Down Expand Up @@ -858,6 +859,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_301/sol1.py)
* Problem 551
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py)
* Problem 686
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_686/sol1.py)

## Quantum
* [Deutsch Jozsa](https://github.com/TheAlgorithms/Python/blob/master/quantum/deutsch_jozsa.py)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
""" A Stack using a linked list like structure """
from __future__ import annotations

from typing import Any
from collections.abc import Iterator
from typing import Generic, TypeVar

T = TypeVar("T")

class Node:
def __init__(self, data):

class Node(Generic[T]):
def __init__(self, data: T):
self.data = data
self.next = None
self.next: Node[T] | None = None

def __str__(self):
def __str__(self) -> str:
return f"{self.data}"


class LinkedStack:
class LinkedStack(Generic[T]):
"""
Linked List Stack implementing push (to top),
pop (from top) and is_empty
Expand Down Expand Up @@ -44,15 +47,15 @@ class LinkedStack:
"""

def __init__(self) -> None:
self.top: Node | None = None
self.top: Node[T] | None = None

def __iter__(self):
def __iter__(self) -> Iterator[T]:
node = self.top
while node:
yield node.data
node = node.next

def __str__(self):
def __str__(self) -> str:
"""
>>> stack = LinkedStack()
>>> stack.push("c")
Expand All @@ -63,7 +66,7 @@ def __str__(self):
"""
return "->".join([str(item) for item in self])

def __len__(self):
def __len__(self) -> int:
"""
>>> stack = LinkedStack()
>>> len(stack) == 0
Expand All @@ -87,7 +90,7 @@ def is_empty(self) -> bool:
"""
return self.top is None

def push(self, item: Any) -> None:
def push(self, item: T) -> None:
"""
>>> stack = LinkedStack()
>>> stack.push("Python")
Expand All @@ -101,7 +104,7 @@ def push(self, item: Any) -> None:
node.next = self.top
self.top = node

def pop(self) -> Any:
def pop(self) -> T:
"""
>>> stack = LinkedStack()
>>> stack.pop()
Expand All @@ -125,7 +128,7 @@ def pop(self) -> Any:
self.top = self.top.next
return pop_node.data

def peek(self) -> Any:
def peek(self) -> T:
"""
>>> stack = LinkedStack()
>>> stack.push("Java")
Expand Down