Skip to content
Open
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
13 changes: 11 additions & 2 deletions guardrails/classes/generic/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

class Stack(List[T]):
def __init__(self, *args):
super().__init__(args)
# Avoid passing args as a tuple - unpack to construct the list directly for better performance and consistent behavior
super().__init__(
args
if len(args) != 1
else args[0]
if isinstance(args[0], (list, tuple))
else args
)

def empty(self) -> bool:
"""Tests if this stack is empty."""
return len(self) == 0
# Bypass len() for List classes: bool(self) is more efficient as it just checks __bool__ (inherited from list)
# and returns True if non-empty, False otherwise.
return not self

def peek(self) -> Optional[T]:
"""Looks at the object at the top (last/most recently added) of this
Expand Down