check for `ChromaDocumentStore` dataclass in-place mutation warning
After adding the dataclass in-place mutation warning in PR https://github.com/deepset-ai/haystack/pull/10650, running tests in core-integrations surfaces multiple in-repo usages where we still mutate dataclass instances in place.
We should replace these with dataclasses.replace (or other non-mutating patterns) to avoid warnings and unintended side effects.
Actions
- running tests
- catching warnings,
- fixing the mutating behaviour.
In production, we've dealt with similar issues while working with large-scale document stores and dataclasses. Mutating dataclass instances in-place can lead to subtle bugs and performance issues, especially when dealing with concurrent access. We've found that using dataclasses.replace helps maintain immutability and avoids unexpected side effects. Additionally, consider using tools like mypy with the dataclass_transform plugin to catch these issues early in the development process.
Here are a few steps to address this:
- Identify Mutations: Run tests and use static analysis tools to identify all instances where dataclass instances are mutated in-place.
- Replace Mutations: Use
dataclasses.replaceto create new instances with the desired changes. This ensures immutability and makes the code easier to reason about. - Update Tests: Ensure that all tests pass after making these changes. This includes both unit tests and integration tests.
- Document Changes: Update the documentation to reflect the immutability of dataclass instances and provide guidance on how to work with them.
Hi @davidsbatista
I'd like to work on the ChromaDocumentStore fix for this issue. I recently contributed to Haystack (PR #11175).
I'll follow the same pattern used in the other integrations: replacing in-place mutations with dataclasses.replace()
Could you confirm this is still available?
Hi @davidsbatista 👋
I investigated ChromaDocumentStore for in-place dataclass mutations. After checking all files in integrations/chroma/:
- No doc.meta[] = assignments found ✅
- No doc.embedding = assignments found ✅
- No doc.content = assignments found ✅
- Source code (document_store.py) also clean ✅
The only DeprecationWarnings found are from chromadb's own internal code (asyncio.iscoroutinefunction), not from Haystack's test code.
Could this issue be closed, or is there something specific I missed?
Hi @davidsbatista 👋
I ran the full Chroma test suite to investigate this issue. Here are my findings:
Test Results:
- 133 passed ✅
- 40 skipped
- 0 failures
Warnings Analysis: All DeprecationWarnings come from chromadb's own internal code, NOT from Haystack's ChromaDocumentStore:
asyncio.iscoroutinefunction→ chromadb telemetryDirect api_key configuration→ chromadb embeddings_TestEmbeddingFunction __init__→ chromadb APIEmbeddingFunction name()→ chromadb API
Code Investigation:
- No
doc.meta[x] =mutations found ✅ - No
doc.embedding =mutations found ✅ - No
doc.content =mutations found ✅ - Source code (document_store.py) clean ✅
Conclusion: ChromaDocumentStore has NO in-place dataclass mutations. This issue can be closed ✅
Happy to provide any additional details if needed!
Thanks for your contributions!