Skip to content

[mypy] Fix type annotations for double ended queue in data structures #5549

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

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 18 additions & 11 deletions data_structures/queue/double_ended_queue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Implementation of double ended queue.
"""
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Iterable

Expand Down Expand Up @@ -51,8 +53,8 @@ class _Node:
"""

val: Any = None
next: "Deque._Node" = None
prev: "Deque._Node" = None
next: Deque._Node | None = None
prev: Deque._Node | None = None

class _Iterator:
"""
Expand All @@ -66,10 +68,10 @@ class _Iterator:

__slots__ = ["_cur"]

def __init__(self, cur: "Deque._Node") -> None:
def __init__(self, cur: Deque._Node | None) -> None:
self._cur = cur

def __iter__(self) -> "Deque._Iterator":
def __iter__(self) -> Deque._Iterator:
"""
>>> our_deque = Deque([1, 2, 3])
>>> iterator = iter(our_deque)
Expand All @@ -95,9 +97,10 @@ def __next__(self) -> Any:

return val

def __init__(self, iterable: Iterable = None) -> None:
self._front = self._back = None
self._len = 0
def __init__(self, iterable: Iterable[Any] | None = None) -> None:
self._front: Any = None
self._back: Any = None
self._len: int = 0

if iterable is not None:
# append every value to the deque
Expand Down Expand Up @@ -194,7 +197,7 @@ def appendleft(self, val: Any) -> None:
# make sure there were no errors
assert not self.is_empty(), "Error on appending value."

def extend(self, iter: Iterable) -> None:
def extend(self, iter: Iterable[Any]) -> None:
"""
Appends every value of iter to the end of the deque.
Time complexity: O(n)
Expand Down Expand Up @@ -226,7 +229,7 @@ def extend(self, iter: Iterable) -> None:
for val in iter:
self.append(val)

def extendleft(self, iter: Iterable) -> None:
def extendleft(self, iter: Iterable[Any]) -> None:
"""
Appends every value of iter to the beginning of the deque.
Time complexity: O(n)
Expand Down Expand Up @@ -379,7 +382,7 @@ def __len__(self) -> int:
"""
return self._len

def __eq__(self, other: "Deque") -> bool:
def __eq__(self, other: object) -> bool:
"""
Implements "==" operator. Returns if *self* is equal to *other*.
Time complexity: O(n)
Expand All @@ -406,6 +409,10 @@ def __eq__(self, other: "Deque") -> bool:
>>> (our_deque_1 == our_deque_3) == (deque_collections_1 == deque_collections_3)
True
"""

if not isinstance(other, Deque):
return NotImplemented

me = self._front
oth = other._front

Expand All @@ -422,7 +429,7 @@ def __eq__(self, other: "Deque") -> bool:

return True

def __iter__(self) -> "_Iterator":
def __iter__(self) -> Deque._Iterator:
"""
Implements iteration.
Time complexity: O(1)
Expand Down