Repository navigation
Python: fix: optimize function_copy to avoid unnecessary deepcopy #13599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nima Nikoonazar (nimanikoo)
wants to merge
21
commits into
microsoft:main
Choose a base branch
from
nimanikoo:fix/optimize-function-copy-deepcopy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5d65776
fix: optimize function_copy to avoid unnecessary deepcopy
nimanikoo 9e93880
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo 2e65593
Update python/semantic_kernel/functions/kernel_function.py
nimanikoo 7e062b6
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo e50fbd0
Update python/semantic_kernel/functions/kernel_function.py
nimanikoo 9fa1db6
Update python/tests/unit/functions/test_function_copy_optimization.py
nimanikoo a7bcbbc
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo 4bb0c42
Update python/tests/unit/functions/test_function_copy_optimization.py
nimanikoo e84eb17
Update python/tests/unit/functions/test_function_copy_optimization.py
nimanikoo 8ef520b
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo 8296e42
Fixup errors on pre commit
nimanikoo a3d3d71
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo 083b49f
Apply ruff formatting fixes
nimanikoo 56934fd
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo e4f3ef7
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo 1d829d6
Fixup
nimanikoo 9fafdf8
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo 337d621
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo bbbfd84
Merge branch 'main' into fix/optimize-function-copy-deepcopy
eavanvalkenburg c420c52
fix: preserve plugin_name validation in function_copy
nimanikoo cfa771a
Merge branch 'main' into fix/optimize-function-copy-deepcopy
nimanikoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
python/tests/unit/functions/test_function_copy_optimization.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Tests for function_copy metadata copy behavior.""" | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from semantic_kernel.functions import kernel_function | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_function(): | ||
| @kernel_function | ||
| def test_func(input: str) -> str: | ||
| return f"Result: {input}" | ||
|
|
||
| from semantic_kernel.functions.kernel_function_from_method import KernelFunctionFromMethod | ||
|
|
||
| return KernelFunctionFromMethod(method=test_func, plugin_name="test_plugin") | ||
|
|
||
|
|
||
| class TestFunctionCopyOptimization: | ||
| """Test suite for function_copy metadata isolation.""" | ||
|
|
||
| def test_function_copy_same_plugin_creates_independent_metadata(self, sample_function): | ||
| """Test that function_copy isolates metadata even when plugin_name is unchanged.""" | ||
| original_plugin = sample_function.plugin_name | ||
|
|
||
| # Case 1: No plugin_name provided (metadata should still be copied) | ||
| copy1 = sample_function.function_copy() | ||
| assert copy1.metadata is not sample_function.metadata | ||
| assert copy1.plugin_name == original_plugin | ||
|
|
||
| # Case 2: Same plugin_name explicitly provided (metadata should still be copied) | ||
| copy2 = sample_function.function_copy(original_plugin) | ||
| assert copy2.metadata is not sample_function.metadata | ||
| assert copy2.plugin_name == original_plugin | ||
|
|
||
| def test_function_copy_different_plugin_creates_copy(self, sample_function): | ||
| """Test that function_copy does create a shallow copy when plugin_name changes. | ||
|
|
||
| This tests that when we actually need to change the plugin_name, | ||
| a shallow copy is created. | ||
| """ | ||
| new_plugin_name = "new_plugin" | ||
| copy = sample_function.function_copy(new_plugin_name) | ||
|
|
||
| # Metadata should be different object (copied) | ||
| assert copy.metadata is not sample_function.metadata | ||
| # But should have the new plugin_name | ||
| assert copy.metadata.plugin_name == new_plugin_name | ||
| # Original should be unchanged | ||
| assert sample_function.metadata.plugin_name != new_plugin_name | ||
|
|
||
| def test_function_copy_preserves_function_behavior(self, sample_function): | ||
| """Test that copied function still works correctly.""" | ||
| copy = sample_function.function_copy() | ||
|
|
||
| # Verify function metadata is preserved | ||
| assert copy.name == sample_function.name | ||
| assert copy.description == sample_function.description | ||
| # Verify function is callable (indirectly through having same underlying function) | ||
| assert hasattr(copy, "invoke") | ||
|
|
||
| @patch( | ||
| "semantic_kernel.functions.kernel_function.deepcopy", | ||
| side_effect=AssertionError("deepcopy should not be called"), | ||
| ) | ||
| def test_function_copy_no_module_deepcopy_usage(self, mock_deepcopy, sample_function): | ||
| """Test that function_copy does not call the module-level deepcopy helper.""" | ||
| original_metadata = sample_function.metadata | ||
| copy = sample_function.function_copy() | ||
|
|
||
| assert copy.metadata is not original_metadata | ||
| mock_deepcopy.assert_not_called() | ||
|
|
||
| def test_function_copy_rejects_invalid_plugin_name(self, sample_function): | ||
| """Test that function_copy still validates the plugin name on assignment. | ||
|
|
||
| model_copy(update=...) silently skips validation, so this guards against | ||
| that regression by asserting an invalid plugin name is rejected. | ||
| """ | ||
| from pydantic import ValidationError | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| sample_function.function_copy("bad-name") | ||
|
|
||
| # Original metadata must remain untouched by the failed copy attempt. | ||
| assert sample_function.metadata.plugin_name == "test_plugin" | ||
|
|
||
| def test_function_copy_rejects_empty_plugin_name(self, sample_function): | ||
| """Test that an empty string plugin name is rejected, not silently accepted.""" | ||
| from pydantic import ValidationError | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| sample_function.function_copy("") | ||
|
|
||
| def test_function_copy_multiple_calls_same_plugin_are_isolated(self, sample_function): | ||
| """Test that multiple copies with same plugin keep independent metadata.""" | ||
| copy1 = sample_function.function_copy() | ||
| copy2 = sample_function.function_copy() | ||
| copy3 = sample_function.function_copy(sample_function.plugin_name) | ||
|
|
||
| assert copy1.metadata is not sample_function.metadata | ||
| assert copy2.metadata is not sample_function.metadata | ||
| assert copy3.metadata is not sample_function.metadata | ||
| assert copy1.metadata is not copy2.metadata | ||
| assert copy2.metadata is not copy3.metadata | ||
| assert copy1.metadata is not copy3.metadata | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.