Skip to content

Commit 5204e87

Browse files
authored
PYTHON-5002 Add guard to synchro hook to accidental overwrite (#2026)
1 parent dc34833 commit 5204e87

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ repos:
2424
entry: bash ./tools/synchro.sh
2525
language: python
2626
require_serial: true
27+
fail_fast: true
2728
additional_dependencies:
2829
- ruff==0.1.3
2930
- unasync

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ you are attempting to validate new spec tests in PyMongo.
246246

247247
Follow the [Python Driver Release Process Wiki](https://wiki.corp.mongodb.com/display/DRIVERS/Python+Driver+Release+Process).
248248

249+
## Asyncio considerations
250+
251+
PyMongo adds asyncio capability by modifying the source files in `*/asynchronous` to `*/synchronous` using
252+
[unasync](https://github.com/python-trio/unasync/) and some custom transforms.
253+
254+
Where possible, edit the code in `*/asynchronous/*.py` and not the synchronous files.
255+
You can run `pre-commit run --all-files synchro` before running tests if you are testing synchronous code.
256+
257+
To prevent the `synchro` hook from accidentally overwriting code, it first checks to see whether a sync version
258+
of a file is changing and not its async counterpart, and will fail.
259+
In the unlikely scenario that you want to override this behavior, first export `OVERRIDE_SYNCHRO_CHECK=1`.
260+
249261
## Converting a test to async
250262
The `tools/convert_test_to_async.py` script takes in an existing synchronous test file and outputs a
251263
partially-converted asynchronous version of the same name to the `test/asynchronous` directory.

tools/synchro.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
from __future__ import annotations
2121

22+
import os
2223
import re
24+
import sys
2325
from os import listdir
2426
from pathlib import Path
2527

@@ -356,6 +358,19 @@ def unasync_directory(files: list[str], src: str, dest: str, replacements: dict[
356358

357359

358360
def main() -> None:
361+
modified_files = [f"./{f}" for f in sys.argv[1:]]
362+
errored = False
363+
for fname in async_files + gridfs_files:
364+
# If the async file was modified, we don't need to check if the sync file was also modified.
365+
if str(fname) in modified_files:
366+
continue
367+
sync_name = str(fname).replace("asynchronous", "synchronous")
368+
if sync_name in modified_files and "OVERRIDE_SYNCHRO_CHECK" not in os.environ:
369+
print(f"Refusing to overwrite {sync_name}")
370+
errored = True
371+
if errored:
372+
raise ValueError("Aborting synchro due to errors")
373+
359374
unasync_directory(async_files, _pymongo_base, _pymongo_dest_base, replacements)
360375
unasync_directory(gridfs_files, _gridfs_base, _gridfs_dest_base, replacements)
361376
unasync_directory(test_files, _test_base, _test_dest_base, replacements)

tools/synchro.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#!/bin/bash -eu
1+
#!/bin/bash
22

3-
python ./tools/synchro.py
3+
set -eu
4+
5+
python ./tools/synchro.py "$@"
46
python -m ruff check pymongo/synchronous/ gridfs/synchronous/ test/ --fix --silent
57
python -m ruff format pymongo/synchronous/ gridfs/synchronous/ test/ --silent

0 commit comments

Comments
 (0)