-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Python: Replace mutable default arguments and prevent unintended side effects #11849
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
Merged
eavanvalkenburg
merged 5 commits into
microsoft:main
from
KanchiShimono:replace-mutable-default-args
May 6, 2025
Merged
Python: Replace mutable default arguments and prevent unintended side effects #11849
eavanvalkenburg
merged 5 commits into
microsoft:main
from
KanchiShimono:replace-mutable-default-args
May 6, 2025
Conversation
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
2075d22 to
f70e026
Compare
moonbox3
approved these changes
May 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the improvements, @KanchiShimono.
Python Unit Test Overview
|
|
I’ve configured the project to ignore the Ruff rule ( |
dbb003c to
4ade63f
Compare
eavanvalkenburg
approved these changes
May 2, 2025
github-merge-queue bot
pushed a commit
that referenced
this pull request
May 6, 2025
… effects (#11849) ### Motivation and Context Some functions and methods were using mutable objects (typically `dict`) as default argument values. In Python, default arguments are evaluated only once at the time of function definition. This can lead to unexpected behavior when the default value is modified within the function, as subsequent calls to the function may reuse the modified object. As a result, calling such functions or methods multiple times can lead to unintended behavior. Reference: [8.7. Function definitions (Official Python Documentation)](https://docs.python.org/3.12/reference/compound_stmts.html#function-definitions) > Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter value is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default parameter value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.: ### Description - Replaced mutable types (e.g., `dict`, `list`) used as default argument values with `None`, and properly initialized them within the function body. - Reviewed and updated the parts of the code that performed destructive modifications on the arguments, to avoid unintentionally modifying variables from the caller’s scope. - ~Added the `flake8-bugbear` rule in Ruff to detect this kind of issue.~ - ~For now, errors in `tests` and `samples` directories are ignored due to the large number of violations.~ ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
jcruzmot-te
pushed a commit
to thousandeyes/aia-semantic-kernel
that referenced
this pull request
Sep 15, 2025
… effects (microsoft#11849) ### Motivation and Context Some functions and methods were using mutable objects (typically `dict`) as default argument values. In Python, default arguments are evaluated only once at the time of function definition. This can lead to unexpected behavior when the default value is modified within the function, as subsequent calls to the function may reuse the modified object. As a result, calling such functions or methods multiple times can lead to unintended behavior. Reference: [8.7. Function definitions (Official Python Documentation)](https://docs.python.org/3.12/reference/compound_stmts.html#function-definitions) > Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter value is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default parameter value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.: ### Description - Replaced mutable types (e.g., `dict`, `list`) used as default argument values with `None`, and properly initialized them within the function body. - Reviewed and updated the parts of the code that performed destructive modifications on the arguments, to avoid unintentionally modifying variables from the caller’s scope. - ~Added the `flake8-bugbear` rule in Ruff to detect this kind of issue.~ - ~For now, errors in `tests` and `samples` directories are ignored due to the large number of violations.~ ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation and Context
Some functions and methods were using mutable objects (typically
dict) as default argument values.In Python, default arguments are evaluated only once at the time of function definition.
This can lead to unexpected behavior when the default value is modified within the function, as subsequent calls to the function may reuse the modified object.
As a result, calling such functions or methods multiple times can lead to unintended behavior.
Reference:
8.7. Function definitions (Official Python Documentation)
Description
dict,list) used as default argument values withNone, and properly initialized them within the function body.Added theflake8-bugbearrule in Ruff to detect this kind of issue.For now, errors intestsandsamplesdirectories are ignored due to the large number of violations.Contribution Checklist