Skip to content

PYTHON-4933 - Allow drivers to set bypassDocumentValidation: false on… #2227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pymongo/asynchronous/bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(
self,
collection: AsyncCollection[_DocumentType],
ordered: bool,
bypass_document_validation: bool,
bypass_document_validation: Optional[bool],
comment: Optional[str] = None,
let: Optional[Any] = None,
) -> None:
Expand Down Expand Up @@ -516,8 +516,8 @@ async def _execute_command(
if self.comment:
cmd["comment"] = self.comment
_csot.apply_write_concern(cmd, write_concern)
if self.bypass_doc_val:
cmd["bypassDocumentValidation"] = True
if self.bypass_doc_val is not None:
cmd["bypassDocumentValidation"] = self.bypass_doc_val
if self.let is not None and run.op_type in (_DELETE, _UPDATE):
cmd["let"] = self.let
if session:
Expand Down
25 changes: 13 additions & 12 deletions pymongo/asynchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ async def bulk_write(
self,
requests: Sequence[_WriteOp[_DocumentType]],
ordered: bool = True,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
let: Optional[Mapping] = None,
Expand Down Expand Up @@ -800,7 +800,7 @@ async def _insert_one(
ordered: bool,
write_concern: WriteConcern,
op_id: Optional[int],
bypass_doc_val: bool,
bypass_doc_val: Optional[bool],
session: Optional[AsyncClientSession],
comment: Optional[Any] = None,
) -> Any:
Expand All @@ -814,8 +814,8 @@ async def _insert_one(
async def _insert_command(
session: Optional[AsyncClientSession], conn: AsyncConnection, retryable_write: bool
) -> None:
if bypass_doc_val:
command["bypassDocumentValidation"] = True
if bypass_doc_val is not None:
command["bypassDocumentValidation"] = bypass_doc_val

result = await conn.command(
self._database.name,
Expand All @@ -840,7 +840,7 @@ async def _insert_command(
async def insert_one(
self,
document: Union[_DocumentType, RawBSONDocument],
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
) -> InsertOneResult:
Expand Down Expand Up @@ -906,7 +906,7 @@ async def insert_many(
self,
documents: Iterable[Union[_DocumentType, RawBSONDocument]],
ordered: bool = True,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
) -> InsertManyResult:
Expand Down Expand Up @@ -986,7 +986,7 @@ async def _update(
write_concern: Optional[WriteConcern] = None,
op_id: Optional[int] = None,
ordered: bool = True,
bypass_doc_val: Optional[bool] = False,
bypass_doc_val: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
Expand Down Expand Up @@ -1041,8 +1041,8 @@ async def _update(
if comment is not None:
command["comment"] = comment
# Update command.
if bypass_doc_val:
command["bypassDocumentValidation"] = True
if bypass_doc_val is not None:
command["bypassDocumentValidation"] = bypass_doc_val

# The command result has to be published for APM unmodified
# so we make a shallow copy here before adding updatedExisting.
Expand Down Expand Up @@ -1082,7 +1082,7 @@ async def _update_retryable(
write_concern: Optional[WriteConcern] = None,
op_id: Optional[int] = None,
ordered: bool = True,
bypass_doc_val: Optional[bool] = False,
bypass_doc_val: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
Expand Down Expand Up @@ -1128,7 +1128,7 @@ async def replace_one(
filter: Mapping[str, Any],
replacement: Mapping[str, Any],
upsert: bool = False,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional[AsyncClientSession] = None,
Expand Down Expand Up @@ -1237,7 +1237,7 @@ async def update_one(
filter: Mapping[str, Any],
update: Union[Mapping[str, Any], _Pipeline],
upsert: bool = False,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
Expand Down Expand Up @@ -2948,6 +2948,7 @@ async def aggregate(
returning aggregate results using a cursor.
- `collation` (optional): An instance of
:class:`~pymongo.collation.Collation`.
- `bypassDocumentValidation` (bool): If ``True``, allows the write to opt-out of document level validation.


:return: A :class:`~pymongo.asynchronous.command_cursor.AsyncCommandCursor` over the result
Expand Down
6 changes: 3 additions & 3 deletions pymongo/synchronous/bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(
self,
collection: Collection[_DocumentType],
ordered: bool,
bypass_document_validation: bool,
bypass_document_validation: Optional[bool],
comment: Optional[str] = None,
let: Optional[Any] = None,
) -> None:
Expand Down Expand Up @@ -516,8 +516,8 @@ def _execute_command(
if self.comment:
cmd["comment"] = self.comment
_csot.apply_write_concern(cmd, write_concern)
if self.bypass_doc_val:
cmd["bypassDocumentValidation"] = True
if self.bypass_doc_val is not None:
cmd["bypassDocumentValidation"] = self.bypass_doc_val
if self.let is not None and run.op_type in (_DELETE, _UPDATE):
cmd["let"] = self.let
if session:
Expand Down
25 changes: 13 additions & 12 deletions pymongo/synchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def bulk_write(
self,
requests: Sequence[_WriteOp[_DocumentType]],
ordered: bool = True,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
let: Optional[Mapping] = None,
Expand Down Expand Up @@ -799,7 +799,7 @@ def _insert_one(
ordered: bool,
write_concern: WriteConcern,
op_id: Optional[int],
bypass_doc_val: bool,
bypass_doc_val: Optional[bool],
session: Optional[ClientSession],
comment: Optional[Any] = None,
) -> Any:
Expand All @@ -813,8 +813,8 @@ def _insert_one(
def _insert_command(
session: Optional[ClientSession], conn: Connection, retryable_write: bool
) -> None:
if bypass_doc_val:
command["bypassDocumentValidation"] = True
if bypass_doc_val is not None:
command["bypassDocumentValidation"] = bypass_doc_val

result = conn.command(
self._database.name,
Expand All @@ -839,7 +839,7 @@ def _insert_command(
def insert_one(
self,
document: Union[_DocumentType, RawBSONDocument],
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> InsertOneResult:
Expand Down Expand Up @@ -905,7 +905,7 @@ def insert_many(
self,
documents: Iterable[Union[_DocumentType, RawBSONDocument]],
ordered: bool = True,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> InsertManyResult:
Expand Down Expand Up @@ -985,7 +985,7 @@ def _update(
write_concern: Optional[WriteConcern] = None,
op_id: Optional[int] = None,
ordered: bool = True,
bypass_doc_val: Optional[bool] = False,
bypass_doc_val: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
Expand Down Expand Up @@ -1040,8 +1040,8 @@ def _update(
if comment is not None:
command["comment"] = comment
# Update command.
if bypass_doc_val:
command["bypassDocumentValidation"] = True
if bypass_doc_val is not None:
command["bypassDocumentValidation"] = bypass_doc_val

# The command result has to be published for APM unmodified
# so we make a shallow copy here before adding updatedExisting.
Expand Down Expand Up @@ -1081,7 +1081,7 @@ def _update_retryable(
write_concern: Optional[WriteConcern] = None,
op_id: Optional[int] = None,
ordered: bool = True,
bypass_doc_val: Optional[bool] = False,
bypass_doc_val: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
Expand Down Expand Up @@ -1127,7 +1127,7 @@ def replace_one(
filter: Mapping[str, Any],
replacement: Mapping[str, Any],
upsert: bool = False,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional[ClientSession] = None,
Expand Down Expand Up @@ -1236,7 +1236,7 @@ def update_one(
filter: Mapping[str, Any],
update: Union[Mapping[str, Any], _Pipeline],
upsert: bool = False,
bypass_document_validation: bool = False,
bypass_document_validation: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
Expand Down Expand Up @@ -2941,6 +2941,7 @@ def aggregate(
returning aggregate results using a cursor.
- `collation` (optional): An instance of
:class:`~pymongo.collation.Collation`.
- `bypassDocumentValidation` (bool): If ``True``, allows the write to opt-out of document level validation.


:return: A :class:`~pymongo.command_cursor.CommandCursor` over the result
Expand Down
Loading
Loading