Skip to content

Commit

Permalink
Fix mutating fixture causing race condition failure
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Jul 30, 2024
1 parent 798bf93 commit 0be9ce7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions griptape/mixins/activity_mixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import inspect
from copy import deepcopy
from typing import Callable, Optional

from attrs import Attribute, define, field
Expand All @@ -20,7 +21,7 @@ class ActivityMixin:

allowlist: Optional[list[str]] = field(default=None, kw_only=True)
denylist: Optional[list[str]] = field(default=None, kw_only=True)
extra_schema_properties: dict[str, dict] = field(default=None, kw_only=True)
extra_schema_properties: Optional[dict[str, dict]] = field(default=None, kw_only=True)

@allowlist.validator # pyright: ignore[reportAttributeAccessIssue]
def validate_allowlist(self, _: Attribute, allowlist: Optional[list[str]]) -> None:
Expand Down Expand Up @@ -89,12 +90,14 @@ def activity_schema(self, activity: Callable) -> Optional[Schema]:
if activity is None or not getattr(activity, "is_activity", False):
raise Exception("This method is not an activity.")
elif getattr(activity, "config")["schema"] is not None:
full_schema = {"values": getattr(activity, "config")["schema"]}
# Need to deepcopy to avoid modifying the original schema
config_schema = deepcopy(getattr(activity, "config")["schema"])
activity_name = self.activity_name(activity)

if self.extra_schema_properties is not None and activity_name in self.extra_schema_properties:
full_schema["values"].schema.update(self.extra_schema_properties[activity_name])
config_schema.schema.update(self.extra_schema_properties[activity_name])

return Schema(full_schema)
return Schema({"values": config_schema})
else:
return None

Expand Down
9 changes: 6 additions & 3 deletions tests/unit/mixins/test_activity_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ def test_enable_activities(self, tool):

assert len(tool.activities()) > 0

def test_extra_schema_properties(self, tool):
tool.extra_schema_properties = {"test": {Literal("new_property"): str, Optional("optional_property"): int}}

def test_extra_schema_properties(self):
tool = MockTool(
test_field="hello",
test_int=5,
extra_schema_properties={"test": {Literal("new_property"): str, Optional("optional_property"): int}},
)
schema = tool.activity_schema(tool.test).json_schema("InputSchema")

assert schema == {
Expand Down

0 comments on commit 0be9ce7

Please sign in to comment.