Skip to content
Merged
Show file tree
Hide file tree
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: 20 additions & 1 deletion burr/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@
import inspect
import logging
from functools import cached_property
from typing import Any, Callable, Dict, Generic, Iterator, Mapping, Optional, TypeVar, Union
from typing import (
Any,
Callable,
Dict,
Generic,
Iterator,
List,
Mapping,
Optional,
TypeVar,
Union,
)

from burr.core import serde
from burr.core.typing import DictBasedTypingSystem, TypingSystem
Expand Down Expand Up @@ -460,6 +471,14 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[Any]:
return iter(self._state)

def keys(self):
"""Returns a list of the state keys only (without values for cleaner display).

Returns:
list: A list of state keys
"""
return list(self._state)

def __repr__(self):
return self.get_all().__repr__() # quick hack

Expand Down
14 changes: 14 additions & 0 deletions tests/core/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,17 @@ def test_state_apply_keeps_typing_system():
state = State({"foo": "bar"}, typing_system=SimpleTypingSystem())
assert state.update(foo="baz").typing_system is state.typing_system
assert state.subset("foo").typing_system is state.typing_system


def test_state_keys_returns_list():
"""Test that State.keys() returns a list (fixes #409)"""
state = State({"a": 1, "b": 2, "c": 3})
keys = state.keys()

# Should return a list with the correct keys
assert isinstance(keys, list)
assert keys == ["a", "b", "c"]

# Test with empty state
empty_state = State()
assert empty_state.keys() == []
Loading