Official Python driver for MongoDB.
- Build/env:
hatch— usehatch run test:testandhatch run test:test-async, notpytestdirectly - Task runner:
just(Justfile at repo root) - Linter/formatter:
ruffvia pre-commit - Type checker:
mypy(strict) - Docs: Sphinx / reStructuredText; use Sphinx docstring format for all docstrings
Never run
pip install bson. PyMongo ships its ownbson; the PyPIbsonpackage silently breaks things.
pymongo/synchronous/— sync driver (MongoClient, Collection, Database, etc.) generated frompymongo/asynchronous/using unasyncpymongo/asynchronous/— async API, mirrorspymongo/synchronous/bson/— BSON implementationgridfs/— GridFS APItest/— sync test suitetest/asynchronous/— async test suite, mirrorstest/test/unified_format/— cross-driver spec tests (Unified Test Format)tools/convert_test_to_async.py— generates async test stubs from sync tests
just install # set up venv + pre-commit hook
just typing-mypy # type check
just run lint-manual # ruff linter
hatch run test:test # run sync tests
hatch run test:test-async # run async tests
MONGODB_VERSION=8.0 just run-server # start a local MongoDB serverTests require a live MongoDB server (just run-server above).
Never modify pymongo/synchronous code first; update the async counterpart and run our tools/synchro.py script:
- Make changes in
pymongo/asynchronousor top-levelpymongo/files andtest/. - Run
tools/convert_test_to_async.pyon the sync test file to generate a starting-point async version. - Manually complete the async version in
test/asynchronous/.
Do not edit files in pymongo/synchronous/ or mirrored files in test/ directly — they are auto-generated by tools/synchro.py from pymongo/asynchronous/ and test/asynchronous/. Only edit them if the change includes a _IS_SYNC statement.
Async functions must not call blocking I/O.
New features need integration tests. Bug fixes need regression tests.
- Prefix commits and PR titles with the JIRA ticket:
PYTHON-1234 Fix retryable write on mongos - Backport PRs: append the target branch in brackets —
PYTHON-1234 [v4.9] Fix retryable write - Merge via Squash and Merge only
See .github/copilot-instructions.md for more details.