Skip to content

Commit f0fb349

Browse files
Fixing copy and adding NOT_SPECIFIED check in task.py (#3690)
* Fixing copy and adding NOT_SPECIFIED check: * Fixed mypy issues * Added test Cases * added linting checks * Removed the docs bot folder * Fixed ruff checks * Remove secret_folder from tracking --------- Co-authored-by: Lorenze Jay <[email protected]>
1 parent bf2e2a4 commit f0fb349

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/crewai/task.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import warnings
88
from collections.abc import Callable
99
from concurrent.futures import Future
10-
from copy import copy
10+
from copy import copy as shallow_copy
1111
from hashlib import md5
1212
from pathlib import Path
1313
from typing import (
@@ -672,7 +672,9 @@ def copy( # type: ignore
672672
copied_data = {k: v for k, v in copied_data.items() if v is not None}
673673

674674
cloned_context = (
675-
[task_mapping[context_task.key] for context_task in self.context]
675+
self.context
676+
if self.context is NOT_SPECIFIED
677+
else [task_mapping[context_task.key] for context_task in self.context]
676678
if isinstance(self.context, list)
677679
else None
678680
)
@@ -681,7 +683,7 @@ def get_agent_by_role(role: str) -> Union["BaseAgent", None]:
681683
return next((agent for agent in agents if agent.role == role), None)
682684

683685
cloned_agent = get_agent_by_role(self.agent.role) if self.agent else None
684-
cloned_tools = copy(self.tools) if self.tools else []
686+
cloned_tools = shallow_copy(self.tools) if self.tools else []
685687

686688
return self.__class__(
687689
**copied_data,

tests/test_task.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ def test_create_directory_false():
12181218
assert not resolved_dir.exists()
12191219

12201220
with pytest.raises(
1221-
RuntimeError, match="Directory .* does not exist and create_directory is False"
1221+
RuntimeError, match=r"Directory .* does not exist and create_directory is False"
12221222
):
12231223
task._save_file("test content")
12241224

@@ -1635,3 +1635,48 @@ def test_task_interpolation_with_hyphens():
16351635
assert "say hello world" in task.prompt()
16361636

16371637
assert result.raw == "Hello, World!"
1638+
1639+
1640+
def test_task_copy_with_none_context():
1641+
original_task = Task(
1642+
description="Test task",
1643+
expected_output="Test output",
1644+
context=None
1645+
)
1646+
1647+
new_task = original_task.copy(agents=[], task_mapping={})
1648+
assert original_task.context is None
1649+
assert new_task.context is None
1650+
1651+
1652+
def test_task_copy_with_not_specified_context():
1653+
from crewai.utilities.constants import NOT_SPECIFIED
1654+
original_task = Task(
1655+
description="Test task",
1656+
expected_output="Test output",
1657+
)
1658+
1659+
new_task = original_task.copy(agents=[], task_mapping={})
1660+
assert original_task.context is NOT_SPECIFIED
1661+
assert new_task.context is NOT_SPECIFIED
1662+
1663+
1664+
def test_task_copy_with_list_context():
1665+
"""Test that copying a task with list context works correctly."""
1666+
task1 = Task(
1667+
description="Task 1",
1668+
expected_output="Output 1"
1669+
)
1670+
task2 = Task(
1671+
description="Task 2",
1672+
expected_output="Output 2",
1673+
context=[task1]
1674+
)
1675+
1676+
task_mapping = {task1.key: task1}
1677+
1678+
copied_task2 = task2.copy(agents=[], task_mapping=task_mapping)
1679+
1680+
assert isinstance(copied_task2.context, list)
1681+
assert len(copied_task2.context) == 1
1682+
assert copied_task2.context[0] is task1

0 commit comments

Comments
 (0)