Skip to content

fix: replace mutable default arguments with None sentinels (B006)#5302

Merged
kartik-mem0 merged 1 commit into
mem0ai:mainfrom
kratos0718:fix/mutable-default-arguments-b006
Jun 4, 2026
Merged

fix: replace mutable default arguments with None sentinels (B006)#5302
kartik-mem0 merged 1 commit into
mem0ai:mainfrom
kratos0718:fix/mutable-default-arguments-b006

Conversation

@kratos0718

@kratos0718 kratos0718 commented May 29, 2026

Copy link
Copy Markdown
Contributor

Linked Issue

Closes #5301

Description

Fixes 2 mutable default arguments — the textbook Python anti-pattern flagged by ruff B006 and pylint W0102. The same list/dict object is shared between every call that uses the default; any in-place mutation leaks across calls.

Changes

  • mem0/proxy/main.pyCompletions.create(messages: List = [])Optional[List] = None, with if messages is None: messages = [] normalization at the top of the method. Every call that previously omitted messages aliased the same module-level list; the surrounding _prepare_messages(messages) and related mutation paths could leak state between unrelated calls.

  • mem0/configs/embeddings/base.pyBaseEmbedderConfig.__init__(azure_kwargs: Optional[AzureConfig] = {})= None. The {} default was also a type mismatch with the declared Optional[AzureConfig]; the body's AzureConfig(**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_calls asserts via inspect.signature that Completions.create.messages defaults to None, so any future revert to the mutable default would fail in CI.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Breaking Changes

N/A — 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.

Test Coverage

  • I added/updated unit tests
  • I added/updated integration tests
  • I tested manually (describe below)
  • No tests needed (explain why)

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed

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/
@CLAassistant

CLAassistant commented May 29, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

[Bug] Mutable default arguments (B006) in Completions.create and BaseEmbedderConfig.__init__

3 participants