Skip to content

Commit f903926

Browse files
authored
fix: make Permissions hashable to match User and Group (#3154)
1 parent 1a1b35d commit f903926

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/agents/sandbox/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ def __eq__(self, other: object) -> bool:
124124
return NotImplemented
125125
return self.to_mode() == other.to_mode()
126126

127+
def __hash__(self) -> int:
128+
return hash(self.to_mode())
129+
127130

128131
class FileMode(IntEnum):
129132
ALL = 0o7

tests/sandbox/test_types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from agents.sandbox.types import Group, Permissions, User
2+
3+
4+
def test_permissions_is_hashable() -> None:
5+
# ``Permissions`` overrides ``__eq__``; without a matching ``__hash__`` Pydantic v2
6+
# would set ``__hash__ = None``, breaking sets and dict keys for what is otherwise
7+
# a value-like type. Sibling classes ``User`` and ``Group`` already define both.
8+
perms = Permissions.from_mode(0o755)
9+
other = Permissions.from_mode(0o755)
10+
different = Permissions.from_mode(0o644)
11+
12+
assert hash(perms) == hash(other)
13+
assert hash(perms) != hash(different)
14+
assert {perms, other, different} == {perms, different}
15+
assert {perms: "value"}[other] == "value"
16+
17+
18+
def test_user_and_group_remain_hashable() -> None:
19+
# Regression guard for the sibling classes whose hashability the Permissions fix
20+
# mirrors.
21+
assert hash(User(name="alice")) == hash(User(name="alice"))
22+
assert hash(Group(name="admin", users=[])) == hash(Group(name="admin", users=[]))

0 commit comments

Comments
 (0)