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 1 commit
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] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be bypass_document_validation: Optional[bool], the arg should still be required.

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
35 changes: 21 additions & 14 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,8 +800,8 @@ async def _insert_one(
ordered: bool,
write_concern: WriteConcern,
op_id: Optional[int],
bypass_doc_val: bool,
session: Optional[AsyncClientSession],
bypass_doc_val: Optional[bool] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be bypass_doc_val: Optional[bool],. It should still be required positional arg in all our internal APIs since the default is never used anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our current internal APIs expect it to be a keyword argument and not positional. Is changing it to a positional argument intentional here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously it was positional:

        write_concern: WriteConcern,
        op_id: Optional[int],
        bypass_doc_val: bool,
        session: Optional[AsyncClientSession],

There's no need to change it to have a default of None because we always pass a value anyway so the default will never be used.

Copy link
Contributor Author

@NoahStapp NoahStapp Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like_insert_one is positional, _update and _update_retryable are keyword arguments. We should unify those so they are consistent. I'd prefer them to all be keyword arguments for clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just keep this one as positional and leave the rest.

comment: Optional[Any] = None,
) -> Any:
"""Internal helper for inserting a single document."""
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,11 +2948,12 @@ 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
set.

.. versionchanged:: 4.1
Added ``comment`` parameter.
Added ``let`` parameter.
Expand Down Expand Up @@ -3356,7 +3357,13 @@ async def find_one_and_delete(
if comment is not None:
kwargs["comment"] = comment
return await self._find_and_modify(
filter, projection, sort, let=let, hint=hint, session=session, **kwargs
filter,
projection,
sort,
let=let,
hint=hint,
session=session,
**kwargs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change intentional? It seems unneeded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intended, good catch.

)

async def find_one_and_replace(
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] = None,
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
35 changes: 21 additions & 14 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,8 +799,8 @@ def _insert_one(
ordered: bool,
write_concern: WriteConcern,
op_id: Optional[int],
bypass_doc_val: bool,
session: Optional[ClientSession],
bypass_doc_val: Optional[bool] = None,
comment: Optional[Any] = None,
) -> Any:
"""Internal helper for inserting a single document."""
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,11 +2941,12 @@ 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
set.

.. versionchanged:: 4.1
Added ``comment`` parameter.
Added ``let`` parameter.
Expand Down Expand Up @@ -3349,7 +3350,13 @@ def find_one_and_delete(
if comment is not None:
kwargs["comment"] = comment
return self._find_and_modify(
filter, projection, sort, let=let, hint=hint, session=session, **kwargs
filter,
projection,
sort,
let=let,
hint=hint,
session=session,
**kwargs,
)

def find_one_and_replace(
Expand Down
Loading
Loading