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

* [mypy] Add/fix type annotations for linked queue in data_structures

* add return type annotation to __iter__

* Add more readable syntax
This commit is contained in:
Dylan Buchi 2021-10-23 06:56:58 -03:00 committed by GitHub
parent 11ec2fd3fb
commit 20e09c3ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
def __str__(self) -> str:
return f"{self.data}"
@ -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
@ -87,7 +90,7 @@ class LinkedQueue:
"""
return len(self) == 0
def put(self, item) -> None:
def put(self, item: Any) -> None:
"""
>>> queue = LinkedQueue()
>>> queue.get()