config: T0000: fix mutable default argument in config API methods#5075
config: T0000: fix mutable default argument in config API methods#5075
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
|
👍 |
There was a problem hiding this comment.
Pull request overview
This PR updates the vyos.config.Config API to avoid using mutable list literals as default argument values in four list-returning helper methods.
Changes:
- Changed
default=[]todefault=Noneforreturn_values(),list_nodes(),return_effective_values(), andlist_effective_nodes(). - Added an
if default is None: default = []guard in each method to preserve previous behavior while preventing mutable-default pitfalls.
Comments suppressed due to low confidence (1)
python/vyos/config.py:493
- The
defaultparameter is part of the public signature here, but it isn't documented in the docstringArgssection, and theReturnssection implies[]is always returned when missing. Please documentdefault(and that it's returned when the node is missing/empty) so callers understand the behavior.
def return_values(self, path, default=None):
"""
Retrieve all values of a multi-value leaf node in the running or proposed config
Args:
path (str): Configuration tree path
Returns:
str list: Node values, if it has any
[]: if node does not exist
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -629,6 +635,8 @@ def list_effective_nodes(self, path, default=[]): | |||
| Returns: | |||
| str list: child node names | |||
| """ | |||
There was a problem hiding this comment.
default is part of the signature but isn't documented. Please add it to the docstring and clarify that the method returns default.copy() when no nodes are found.
| def return_values(self, path, default=None): | ||
| """ |
There was a problem hiding this comment.
The PR description says mutable defaults were fixed for the Config API methods listed, but this file still contains mutable default arguments (e.g., get_config_dict(self, path=...) and get_config_defaults(self, path=...) use path=[]). Either include those in this change set or adjust the PR description/scope so it’s not misleading.
| @@ -519,6 +521,8 @@ def list_nodes(self, path, default=[]): | |||
| string list: child node names | |||
|
|
|||
| """ | |||
There was a problem hiding this comment.
default is accepted by this method but not described in the docstring. Please add it to Args and clarify what is returned when the path doesn't exist (currently default.copy() when no nodes are found).
| @@ -606,6 +610,8 @@ def return_effective_values(self, path, default=[]): | |||
| Returns: | |||
| str list: A list of values | |||
| """ | |||
There was a problem hiding this comment.
This docstring doesn't mention the default parameter even though it's part of the signature, and it doesn't explain the fallback behavior when the node is missing/empty. Please document default and the exact return semantics.
|
CI integration ❌ failed! Details
|
Summary
default=[]) in four Config class methods:return_values,list_nodes,return_effective_values, andlist_effective_nodesdefault=[]parameter todefault=Nonewith an explicitif default is None: default = []guard inside the function bodyTest plan
defaultargument still receive an empty list as beforedefaultvalue still receive that value when the node is missing🤖 Generated with Claude Code