Skip to content

[mypy] Add type annotations for linked queue in data structures #5533

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 3 commits into from
Oct 23, 2021
Merged
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
15 changes: 9 additions & 6 deletions data_structures/queue/linked_queue.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
""" A Queue using a linked list like structure """
from typing import Any
from __future__ import annotations

from typing import Any, Iterator


class Node:
def __init__(self, data: Any) -> None:
self.data = data
self.next = None
self.data: Any = data
self.next: Node | None = None
Copy link
Member

Choose a reason for hiding this comment

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

In general, I tend to use type hints for function parameters and return types where they are most valuable in finding bugs but there is nothing wrong with this usage. I find that sometimes overuse of type hints slows down those who are reading the code.


def __str__(self) -> str:
return f"{self.data}"
Expand Down Expand Up @@ -39,9 +41,10 @@ class LinkedQueue:
"""

def __init__(self) -> None:
self.front = self.rear = None
self.front: Node | None = None
self.rear: Node | None = None

def __iter__(self):
def __iter__(self) -> Iterator[Any]:
node = self.front
while node:
yield node.data
Expand Down Expand Up @@ -87,7 +90,7 @@ def is_empty(self) -> bool:
"""
return len(self) == 0

def put(self, item) -> None:
def put(self, item: Any) -> None:
"""
>>> queue = LinkedQueue()
>>> queue.get()
Expand Down