Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

refactor: return frozen dataclasses from two swap-prone tuple helpers (#73582)

Arno Hillen5bfe1eafdbb9
diff --git a/ee/hogai/chat_agent/taxonomy/agent.py b/ee/hogai/chat_agent/taxonomy/agent.py
index 73d8f5a18781..a17d4f429370 100644
--- a/ee/hogai/chat_agent/taxonomy/agent.py
+++ b/ee/hogai/chat_agent/taxonomy/agent.py
@@ -33,7 +33,7 @@ class TaxonomyAgent(
@property
def state_type(self) -> type[StateType]:
# Extract the State type from the generic parameter
- state_type, _ = self._get_state_class(TaxonomyAgent)
+ state_type = self._get_state_class(TaxonomyAgent).state_class
return cast("type[StateType]", state_type)
@property
diff --git a/ee/hogai/chat_agent/taxonomy/nodes.py b/ee/hogai/chat_agent/taxonomy/nodes.py
index d1bd0d0b91a7..2103f0112642 100644
--- a/ee/hogai/chat_agent/taxonomy/nodes.py
+++ b/ee/hogai/chat_agent/taxonomy/nodes.py
@@ -50,7 +50,9 @@ class TaxonomyAgentNode(
def __init__(self, team: Team, user: User, toolkit_class: type["TaxonomyAgentToolkit"]):
super().__init__(team, user)
self._toolkit = toolkit_class(team=team, user=user)
- self._state_class, self._partial_state_class = self._get_state_class(TaxonomyAgentNode)
+ state_classes = self._get_state_class(TaxonomyAgentNode)
+ self._state_class = state_classes.state_class
+ self._partial_state_class = state_classes.partial_state_class
@cached_property
def _team_group_types(self) -> list[str]:
@@ -168,7 +170,9 @@ class TaxonomyAgentToolsNode(
def __init__(self, team: Team, user: User, toolkit_class: type["TaxonomyAgentToolkit"]):
super().__init__(team, user)
self._toolkit = toolkit_class(team=team, user=user)
- self._state_class, self._partial_state_class = self._get_state_class(TaxonomyAgentToolsNode)
+ state_classes = self._get_state_class(TaxonomyAgentToolsNode)
+ self._state_class = state_classes.state_class
+ self._partial_state_class = state_classes.partial_state_class
async def arun(self, state: TaxonomyStateType, config: RunnableConfig) -> TaxonomyPartialStateType:
intermediate_steps = state.intermediate_steps or []
diff --git a/ee/hogai/chat_agent/taxonomy/test/test_agent.py b/ee/hogai/chat_agent/taxonomy/test/test_agent.py
index 3f8a05b62a6d..1da7363f9fdd 100644
--- a/ee/hogai/chat_agent/taxonomy/test/test_agent.py
+++ b/ee/hogai/chat_agent/taxonomy/test/test_agent.py
@@ -61,9 +61,9 @@ class TestTaxonomyAgent(BaseTest):
self.assertFalse(self.agent._has_start_node)
def test_get_state_class(self):
- state_class, partial_state_class = self.agent._get_state_class(TaxonomyAgent)
- self.assertEqual(state_class, TaxonomyAgentState)
- self.assertEqual(partial_state_class, TaxonomyAgentState)
+ state_classes = self.agent._get_state_class(TaxonomyAgent)
+ self.assertEqual(state_classes.state_class, TaxonomyAgentState)
+ self.assertEqual(state_classes.partial_state_class, TaxonomyAgentState)
def test_get_state_class_no_generic(self):
# Create an agent without proper generic typing to test error case
diff --git a/ee/hogai/chat_agent/taxonomy/test/test_nodes.py b/ee/hogai/chat_agent/taxonomy/test/test_nodes.py
index be06a3b1ae64..e83c3336ba6e 100644
--- a/ee/hogai/chat_agent/taxonomy/test/test_nodes.py
+++ b/ee/hogai/chat_agent/taxonomy/test/test_nodes.py
@@ -67,9 +67,9 @@ class TestTaxonomyAgentNode(BaseTest):
self.assertIsInstance(self.node._toolkit, MockTaxonomyAgentToolkit)
def test_get_state_class(self):
- state_class, partial_state_class = self.node._get_state_class(TaxonomyAgentNode)
- self.assertEqual(state_class, TaxonomyAgentState)
- self.assertEqual(partial_state_class, TaxonomyAgentState)
+ state_classes = self.node._get_state_class(TaxonomyAgentNode)
+ self.assertEqual(state_classes.state_class, TaxonomyAgentState)
+ self.assertEqual(state_classes.partial_state_class, TaxonomyAgentState)
def test_get_system_prompt_concrete_implementation(self):
prompts = self.node._get_system_prompt()
@@ -213,9 +213,9 @@ class TestTaxonomyAgentToolsNode(BaseTest):
self.assertEqual(self.node.MAX_ITERATIONS, 10)
def test_get_state_class(self):
- state_class, partial_state_class = self.node._get_state_class(TaxonomyAgentToolsNode)
- self.assertEqual(state_class, TaxonomyAgentState)
- self.assertEqual(partial_state_class, TaxonomyAgentState)
+ state_classes = self.node._get_state_class(TaxonomyAgentToolsNode)
+ self.assertEqual(state_classes.state_class, TaxonomyAgentState)
+ self.assertEqual(state_classes.partial_state_class, TaxonomyAgentState)
def test_get_state_class_no_generic_error(self):
# Test error case for non-generic class
diff --git a/ee/hogai/core/mixins.py b/ee/hogai/core/mixins.py
index 282113161d83..4e50610cd7e3 100644
--- a/ee/hogai/core/mixins.py
+++ b/ee/hogai/core/mixins.py
@@ -1,5 +1,6 @@
import datetime
from abc import ABC, abstractmethod
+from dataclasses import dataclass
from typing import Any, get_args, get_origin
from uuid import UUID
@@ -146,10 +147,18 @@ class AssistantContextMixin(ABC):
return False
+@dataclass(frozen=True)
+class StateClasses:
+ """The state types extracted from a class's generic parameters."""
+
+ state_class: type
+ partial_state_class: type
+
+
class StateClassMixin:
"""Mixin to extract state types from generic class parameters."""
- def _get_state_class(self, target_class: type) -> tuple[type, type]:
+ def _get_state_class(self, target_class: type) -> StateClasses:
"""Extract the State type from the class's generic parameters."""
# Check if this class has generic arguments
if hasattr(self.__class__, "__orig_bases__"):
@@ -157,7 +166,8 @@ class StateClassMixin:
if get_origin(base) is target_class:
args = get_args(base)
if args:
- return args[0], args[1] # State is the first argument and PartialState is the second argument
+ # State is the first argument and PartialState is the second argument
+ return StateClasses(state_class=args[0], partial_state_class=args[1])
# No generic type found - this shouldn't happen in proper usage
raise ValueError(