Skip to content

[mypy] Fix type annotations in data_structures/heap/randomized_heap.py #5704

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 31, 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
21 changes: 14 additions & 7 deletions data_structures/heap/randomized_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from __future__ import annotations

import random
from typing import Generic, Iterable, TypeVar
from typing import Any, Generic, Iterable, TypeVar

T = TypeVar("T")
T = TypeVar("T", bound=bool)


class RandomizedHeapNode(Generic[T]):
Expand Down Expand Up @@ -76,8 +76,10 @@ def __init__(self, data: Iterable[T] | None = ()) -> None:
[1, 3, 3, 7]
"""
self._root: RandomizedHeapNode[T] | None = None
for item in data:
self.insert(item)

if data:
for item in data:
self.insert(item)

def insert(self, value: T) -> None:
"""
Expand All @@ -93,7 +95,7 @@ def insert(self, value: T) -> None:
"""
self._root = RandomizedHeapNode.merge(self._root, RandomizedHeapNode(value))

def pop(self) -> T:
def pop(self) -> T | None:
"""
Pop the smallest value from the heap and return it.
Expand All @@ -111,7 +113,12 @@ def pop(self) -> T:
...
IndexError: Can't get top element for the empty heap.
"""

result = self.top()

if self._root is None:
return None

self._root = RandomizedHeapNode.merge(self._root.left, self._root.right)

return result
Expand All @@ -138,7 +145,7 @@ def top(self) -> T:
raise IndexError("Can't get top element for the empty heap.")
return self._root.value

def clear(self):
def clear(self) -> None:
"""
Clear the heap.
Expand All @@ -151,7 +158,7 @@ def clear(self):
"""
self._root = None

def to_sorted_list(self) -> list[T]:
def to_sorted_list(self) -> list[Any]:
"""
Returns sorted list containing all the values in the heap.
Expand Down