Skip to content

Conversation

@n1lanjan
Copy link
Contributor

This PR fixes usage of mutable defaults in a some methods. The usage of mutable defaults does not have any adverse impact currently since the object initial_session is not updated in the methods as of now, but any change in the future may introduce bugs due to this and hence a good practice not to use mutable defaults and instead initialise the mutable object inside the function body.

Why It’s Dangerous

  • Unintended Persistence: Changes to the default argument persist between calls, violating the expectation of a "fresh" default.
  • Subtle Bugs: The behaviour is counterintuitive and can lead to hard-to-debug issues.
  • Scope Risks: Even if unused, mutable defaults risk accidental mutation in complex functions.

For eg.

def f(a, L=[]):
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))

will print

[1]
[1, 2]
[1, 2, 3]

To further see why it is bad, please see

@hangfei
Copy link
Collaborator

hangfei commented May 30, 2025

Thanks for spotting this! I think this is already fixed recently. Please contribute more to this repo in the future!

@hangfei hangfei closed this May 30, 2025
@hangfei hangfei self-requested a review May 30, 2025 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants