Skip to content

Commit d202fc9

Browse files
Detailed error message for better debugging
1 parent e9adb31 commit d202fc9

20 files changed

+94
-34
lines changed

bson/codec_options.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,22 @@ def __new__(
402402
)
403403
if not isinstance(unicode_decode_error_handler, str):
404404
raise ValueError(
405-
f"unicode_decode_error_handler must be a string, not {type(unicode_decode_error_handler)}")
405+
f"unicode_decode_error_handler must be a string, not {type(unicode_decode_error_handler)}"
406+
)
406407
if tzinfo is not None:
407408
if not isinstance(tzinfo, datetime.tzinfo):
408-
raise TypeError(f"tzinfo must be an instance of datetime.tzinfo, not {type(tzinfo)}")
409+
raise TypeError(
410+
f"tzinfo must be an instance of datetime.tzinfo, not {type(tzinfo)}"
411+
)
409412
if not tz_aware:
410413
raise ValueError("cannot specify tzinfo without also setting tz_aware=True")
411414

412415
type_registry = type_registry or TypeRegistry()
413416

414417
if not isinstance(type_registry, TypeRegistry):
415-
raise TypeError(f"type_registry must be an instance of TypeRegistry, not {type(type_registry)}")
418+
raise TypeError(
419+
f"type_registry must be an instance of TypeRegistry, not {type(type_registry)}"
420+
)
416421

417422
return tuple.__new__(
418423
cls,
@@ -483,6 +488,7 @@ def with_options(self, **kwargs: Any) -> CodecOptions:
483488
opts.update(kwargs)
484489
return CodecOptions(**opts)
485490

491+
486492
DEFAULT_CODEC_OPTIONS: CodecOptions[dict[str, Any]] = CodecOptions()
487493

488494

gridfs/asynchronous/grid_file.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,9 @@ def __init__(
10821082
:attr:`~pymongo.collection.AsyncCollection.write_concern`
10831083
"""
10841084
if not isinstance(root_collection, AsyncCollection):
1085-
raise TypeError(f"root_collection must be an instance of AsyncCollection, not {type(root_collection)}")
1085+
raise TypeError(
1086+
f"root_collection must be an instance of AsyncCollection, not {type(root_collection)}"
1087+
)
10861088

10871089
if not root_collection.write_concern.acknowledged:
10881090
raise ConfigurationError("root_collection must use acknowledged write_concern")
@@ -1436,7 +1438,9 @@ def __init__(
14361438
from the server. Metadata is fetched when first needed.
14371439
"""
14381440
if not isinstance(root_collection, AsyncCollection):
1439-
raise TypeError(f"root_collection must be an instance of AsyncCollection, not {type(root_collection)}")
1441+
raise TypeError(
1442+
f"root_collection must be an instance of AsyncCollection, not {type(root_collection)}"
1443+
)
14401444
_disallow_transactions(session)
14411445

14421446
root_collection = _clear_entity_type_registry(root_collection)

gridfs/synchronous/grid_file.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,9 @@ def __init__(
14261426
from the server. Metadata is fetched when first needed.
14271427
"""
14281428
if not isinstance(root_collection, Collection):
1429-
raise TypeError(f"root_collection must be an instance of Collection, not {type(root_collection)}")
1429+
raise TypeError(
1430+
f"root_collection must be an instance of Collection, not {type(root_collection)}"
1431+
)
14301432
_disallow_transactions(session)
14311433

14321434
root_collection = _clear_entity_type_registry(root_collection)

pymongo/asynchronous/auth_oidc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ def _get_access_token(self) -> Optional[str]:
213213
)
214214
resp = cb.fetch(context)
215215
if not isinstance(resp, OIDCCallbackResult):
216-
raise ValueError(f"Callback result must be of type OIDCCallbackResult, not {type(resp)}")
216+
raise ValueError(
217+
f"Callback result must be of type OIDCCallbackResult, not {type(resp)}"
218+
)
217219
self.refresh_token = resp.refresh_token
218220
self.access_token = resp.access_token
219221
self.token_gen_id += 1

pymongo/asynchronous/client_session.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ def __init__(
310310
)
311311
if max_commit_time_ms is not None:
312312
if not isinstance(max_commit_time_ms, int):
313-
raise TypeError("max_commit_time_ms must be an integer or None")
313+
raise TypeError(
314+
f"max_commit_time_ms must be an integer or None, not {type(max_commit_time_ms)}"
315+
)
314316

315317
@property
316318
def read_concern(self) -> Optional[ReadConcern]:
@@ -902,7 +904,9 @@ def advance_cluster_time(self, cluster_time: Mapping[str, Any]) -> None:
902904
another `AsyncClientSession` instance.
903905
"""
904906
if not isinstance(cluster_time, _Mapping):
905-
raise TypeError("cluster_time must be a subclass of collections.Mapping")
907+
raise TypeError(
908+
f"cluster_time must be a subclass of collections.Mapping, not {type(cluster_time)}"
909+
)
906910
if not isinstance(cluster_time.get("clusterTime"), Timestamp):
907911
raise ValueError("Invalid cluster_time")
908912
self._advance_cluster_time(cluster_time)
@@ -923,7 +927,9 @@ def advance_operation_time(self, operation_time: Timestamp) -> None:
923927
another `AsyncClientSession` instance.
924928
"""
925929
if not isinstance(operation_time, Timestamp):
926-
raise TypeError("operation_time must be an instance of bson.timestamp.Timestamp")
930+
raise TypeError(
931+
f"operation_time must be an instance of bson.timestamp.Timestamp, not {type(operation_time)}"
932+
)
927933
self._advance_operation_time(operation_time)
928934

929935
def _process_response(self, reply: Mapping[str, Any]) -> None:

pymongo/asynchronous/command_cursor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def __init__(
9494
self.batch_size(batch_size)
9595

9696
if not isinstance(max_await_time_ms, int) and max_await_time_ms is not None:
97-
raise TypeError(f"max_await_time_ms must be an integer or None, not {type(max_await_time_ms)}")
97+
raise TypeError(
98+
f"max_await_time_ms must be an integer or None, not {type(max_await_time_ms)}"
99+
)
98100

99101
def __del__(self) -> None:
100102
self._die_no_lock()

pymongo/asynchronous/cursor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,9 @@ def max_await_time_ms(self, max_await_time_ms: Optional[int]) -> AsyncCursor[_Do
543543
.. versionadded:: 3.2
544544
"""
545545
if not isinstance(max_await_time_ms, int) and max_await_time_ms is not None:
546-
raise TypeError(f"max_await_time_ms must be an integer or None, not {type(max_await_time_ms)}")
546+
raise TypeError(
547+
f"max_await_time_ms must be an integer or None, not {type(max_await_time_ms)}"
548+
)
547549
self._check_okay_to_chain()
548550

549551
# Ignore max_await_time_ms if not tailable or await_data is False.

pymongo/asynchronous/database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,9 @@ async def validate_collection(
13741374
name = name.name
13751375

13761376
if not isinstance(name, str):
1377-
raise TypeError(f"name_or_collection must be an instance of str or AsyncCollection, not {type(name)}")
1377+
raise TypeError(
1378+
f"name_or_collection must be an instance of str or AsyncCollection, not {type(name)}"
1379+
)
13781380
cmd = {"validate": name, "scandata": scandata, "full": full}
13791381
if comment is not None:
13801382
cmd["comment"] = comment

pymongo/asynchronous/encryption.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ async def insert_data_key(self, data_key: bytes) -> Binary:
322322
raw_doc = RawBSONDocument(data_key, _KEY_VAULT_OPTS)
323323
data_key_id = raw_doc.get("_id")
324324
if not isinstance(data_key_id, Binary) or data_key_id.subtype != UUID_SUBTYPE:
325-
raise TypeError(f"data_key _id must be Binary with a UUID subtype, not {type(data_key_id)}")
325+
raise TypeError(
326+
f"data_key _id must be Binary with a UUID subtype, not {type(data_key_id)}"
327+
)
326328

327329
assert self.key_vault_coll is not None
328330
await self.key_vault_coll.insert_one(raw_doc)
@@ -645,7 +647,8 @@ def __init__(
645647

646648
if not isinstance(codec_options, CodecOptions):
647649
raise TypeError(
648-
f"codec_options must be an instance of bson.codec_options.CodecOptions, not {type(codec_options)}")
650+
f"codec_options must be an instance of bson.codec_options.CodecOptions, not {type(codec_options)}"
651+
)
649652

650653
if not isinstance(key_vault_client, AsyncMongoClient):
651654
# This is for compatibility with mocked and subclassed types, such as in Motor.

pymongo/asynchronous/mongo_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,9 @@ async def _tmp_session(
20932093
"""If provided session is None, lend a temporary session."""
20942094
if session is not None:
20952095
if not isinstance(session, client_session.AsyncClientSession):
2096-
raise ValueError(f"'session' argument must be an AsyncClientSession or None, not {type(session)}.")
2096+
raise ValueError(
2097+
f"'session' argument must be an AsyncClientSession or None, not {type(session)}."
2098+
)
20972099
# Don't call end_session.
20982100
yield session
20992101
return
@@ -2247,7 +2249,9 @@ async def drop_database(
22472249
name = name.name
22482250

22492251
if not isinstance(name, str):
2250-
raise TypeError(f"name_or_database must be an instance of str or a AsyncDatabase, not {type(name)}")
2252+
raise TypeError(
2253+
f"name_or_database must be an instance of str or a AsyncDatabase, not {type(name)}"
2254+
)
22512255

22522256
async with await self._conn_for_writes(session, operation=_Op.DROP_DATABASE) as conn:
22532257
await self[name]._command(

0 commit comments

Comments
 (0)