fix: replace mutable default arguments with None sentinels (B006)#5302
Merged
kartik-mem0 merged 1 commit intoJun 4, 2026
Merged
Conversation
Fixes 2 mutable default arguments — the textbook Python anti-pattern
flagged by ruff B006 and pylint W0102:
- `mem0/proxy/main.py` — `Completions.create(messages: List = [])`.
Every call that didn't pass `messages` shared the same module-level
list. `_prepare_messages(messages)` and the surrounding mutation
paths could leak state between unrelated calls.
- `mem0/configs/embeddings/base.py` — `BaseEmbedderConfig.__init__(
azure_kwargs: Optional[AzureConfig] = {})`. The default `{}` was
also a type mismatch with the declared `Optional[AzureConfig]` and
is now `None`, with a fresh `{}` produced inside `__init__` when
needed.
## Regression test
`tests/test_proxy.py::test_completions_create_messages_default_does_not_leak_between_calls`
asserts via `inspect.signature` that `Completions.create.messages`
defaults to `None`, so a future revert to the mutable default would
fail in CI.
## Behavior
Zero behavior change. Callers that previously omitted `messages` still
receive a fresh empty list inside the method, and `azure_kwargs`
construction is unchanged from the caller's point of view.
[B006]: https://docs.astral.sh/ruff/rules/mutable-argument-default/
kartik-mem0
approved these changes
Jun 4, 2026
This was referenced Jun 4, 2026
7 tasks
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.
Linked Issue
Closes #5301
Description
Fixes 2 mutable default arguments — the textbook Python anti-pattern flagged by ruff B006 and pylint
W0102. The samelist/dictobject is shared between every call that uses the default; any in-place mutation leaks across calls.Changes
mem0/proxy/main.py—Completions.create(messages: List = [])→Optional[List] = None, withif messages is None: messages = []normalization at the top of the method. Every call that previously omittedmessagesaliased the same module-level list; the surrounding_prepare_messages(messages)and related mutation paths could leak state between unrelated calls.mem0/configs/embeddings/base.py—BaseEmbedderConfig.__init__(azure_kwargs: Optional[AzureConfig] = {})→= None. The{}default was also a type mismatch with the declaredOptional[AzureConfig]; the body'sAzureConfig(**azure_kwargs)call is wrapped with(azure_kwargs or {})so the caller-visible behavior is unchanged.Regression test
tests/test_proxy.py::test_completions_create_messages_default_does_not_leak_between_callsasserts viainspect.signaturethatCompletions.create.messagesdefaults toNone, so any future revert to the mutable default would fail in CI.Type of Change
Breaking Changes
N/A — zero behavior change. Callers that previously omitted
messagesstill receive a fresh empty list inside the method, andazure_kwargsconstruction is unchanged from the caller's point of view.Test Coverage
Checklist