fix: replace mutable default arguments in Config methods#131
Open
AaryanCode69 wants to merge 1 commit intoreactome:mainfrom
Open
fix: replace mutable default arguments in Config methods#131AaryanCode69 wants to merge 1 commit intoreactome:mainfrom
AaryanCode69 wants to merge 1 commit intoreactome:mainfrom
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
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.
Summary
Replace mutable default arguments (
{}and[]) withNonein twoConfigmethods to prevent potential shared-state issues across calls.In Python, default argument values are evaluated once at function definition time, meaning mutable defaults can be shared between calls if mutated.
Reference: https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects
Problem
Two methods in
src/util/config_yml/__init__.pyuse mutable default arguments:If these objects are ever mutated in-place (e.g.,
dict[key] = valueorlist.append()), the mutation persists across subsequent calls, which may lead to unintended shared state.This pattern is commonly flagged by Python linters:
pylint→W0102flake8-bugbear→B006ruff→B006Solution
Use
Noneas the default value and instantiate a fresh object inside the function body.def get_messages( self, user_id: str | None = None, event: TriggerEvent | None = None, after_messages: int | None = None, - last_messages: dict[str, str] = {}, + last_messages: dict[str, str] | None = None, ) -> dict[str, str]: + if last_messages is None: + last_messages = {}def get_message_rate_usage_limited( self, user_id: str | None = None, - message_times_queue: list[str] = [], + message_times_queue: list[str] | None = None, ) -> MessageRate | None: + if message_times_queue is None: + message_times_queue = []Impact
Files Changed
src/util/config_yml/__init__.pyFixes #130