Skip to content

Commit b3604a8

Browse files
authored
PYTHON-3171 Add usage of NoReturn annotation (#901)
1 parent 648a87e commit b3604a8

File tree

14 files changed

+43
-33
lines changed

14 files changed

+43
-33
lines changed

bson/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
List,
7777
Mapping,
7878
MutableMapping,
79+
NoReturn,
7980
Optional,
8081
Sequence,
8182
Tuple,
@@ -170,7 +171,7 @@ def get_data_and_view(data: Any) -> Tuple[Any, memoryview]:
170171
return view.tobytes(), view
171172

172173

173-
def _raise_unknown_type(element_type: int, element_name: str) -> None:
174+
def _raise_unknown_type(element_type: int, element_name: str) -> NoReturn:
174175
"""Unknown type helper."""
175176
raise InvalidBSON(
176177
"Detected unknown BSON type %r for fieldname '%s'. Are "

bson/objectid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
import threading
2525
import time
2626
from random import SystemRandom
27-
from typing import Any, Optional, Type, Union
27+
from typing import Any, NoReturn, Optional, Type, Union
2828

2929
from bson.errors import InvalidId
3030
from bson.tz_util import utc
3131

3232
_MAX_COUNTER_VALUE = 0xFFFFFF
3333

3434

35-
def _raise_invalid_id(oid: str) -> None:
35+
def _raise_invalid_id(oid: str) -> NoReturn:
3636
raise InvalidId(
3737
"%r is not a valid ObjectId, it must be a 12-byte input"
3838
" or a 24-character hex string" % oid

gridfs/grid_file.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import io
1818
import math
1919
import os
20-
from typing import Any, Iterable, List, Mapping, Optional
20+
from typing import Any, Iterable, List, Mapping, NoReturn, Optional
2121

2222
from bson.binary import Binary
2323
from bson.int64 import Int64
@@ -298,7 +298,7 @@ def __flush(self) -> Any:
298298
except DuplicateKeyError:
299299
self._raise_file_exists(self._id)
300300

301-
def _raise_file_exists(self, file_id: Any) -> None:
301+
def _raise_file_exists(self, file_id: Any) -> NoReturn:
302302
"""Raise a FileExists exception for the given file_id."""
303303
raise FileExists("file with _id %r already exists" % file_id)
304304

@@ -312,7 +312,7 @@ def close(self) -> None:
312312
self.__flush()
313313
object.__setattr__(self, "_closed", True)
314314

315-
def read(self, size: Optional[int] = -1) -> None:
315+
def read(self, size: int = -1) -> NoReturn:
316316
raise io.UnsupportedOperation("read")
317317

318318
def readable(self) -> bool:
@@ -682,10 +682,10 @@ def close(self) -> None:
682682
self.__chunk_iter = None
683683
super().close()
684684

685-
def write(self, value: Any) -> None:
685+
def write(self, value: Any) -> NoReturn:
686686
raise io.UnsupportedOperation("write")
687687

688-
def writelines(self, lines: Any) -> None:
688+
def writelines(self, lines: Any) -> NoReturn:
689689
raise io.UnsupportedOperation("writelines")
690690

691691
def writable(self) -> bool:
@@ -704,7 +704,7 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any:
704704
self.close()
705705
return False
706706

707-
def fileno(self) -> int:
707+
def fileno(self) -> NoReturn:
708708
raise io.UnsupportedOperation("fileno")
709709

710710
def flush(self) -> None:
@@ -714,7 +714,7 @@ def flush(self) -> None:
714714
def isatty(self) -> bool:
715715
return False
716716

717-
def truncate(self, size: Optional[int] = None) -> int:
717+
def truncate(self, size: Optional[int] = None) -> NoReturn:
718718
# See https://docs.python.org/3/library/io.html#io.IOBase.writable
719719
# for why truncate has to raise.
720720
raise io.UnsupportedOperation("truncate")
@@ -891,10 +891,10 @@ def next(self) -> GridOut:
891891

892892
__next__ = next
893893

894-
def add_option(self, *args: Any, **kwargs: Any) -> None: # type: ignore[override]
894+
def add_option(self, *args: Any, **kwargs: Any) -> NoReturn:
895895
raise NotImplementedError("Method does not exist for GridOutCursor")
896896

897-
def remove_option(self, *args: Any, **kwargs: Any) -> None: # type: ignore[override]
897+
def remove_option(self, *args: Any, **kwargs: Any) -> NoReturn:
898898
raise NotImplementedError("Method does not exist for GridOutCursor")
899899

900900
def _clone_base(self, session: ClientSession) -> "GridOutCursor":

pymongo/bulk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919
import copy
2020
from itertools import islice
21+
from typing import Any, NoReturn
2122

2223
from bson.objectid import ObjectId
2324
from bson.raw_bson import RawBSONDocument
@@ -128,7 +129,7 @@ def _merge_command(run, full_result, offset, result):
128129
full_result["writeConcernErrors"].append(wce)
129130

130131

131-
def _raise_bulk_write_error(full_result):
132+
def _raise_bulk_write_error(full_result: Any) -> NoReturn:
132133
"""Raise a BulkWriteError from the full bulk api result."""
133134
if full_result["writeErrors"]:
134135
full_result["writeErrors"].sort(key=lambda error: error["index"])

pymongo/client_session.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
ContextManager,
143143
Generic,
144144
Mapping,
145+
NoReturn,
145146
Optional,
146147
TypeVar,
147148
)
@@ -422,7 +423,7 @@ def __del__(self):
422423
self.sock_mgr = None
423424

424425

425-
def _reraise_with_unknown_commit(exc):
426+
def _reraise_with_unknown_commit(exc: Any) -> NoReturn:
426427
"""Re-raise an exception with the UnknownTransactionCommitResult label."""
427428
exc._add_error_label("UnknownTransactionCommitResult")
428429
raise
@@ -1003,7 +1004,7 @@ def _update_read_concern(self, cmd, sock_info):
10031004
if self._snapshot_time is not None:
10041005
rc["atClusterTime"] = self._snapshot_time
10051006

1006-
def __copy__(self):
1007+
def __copy__(self) -> NoReturn:
10071008
raise TypeError("A ClientSession cannot be copied, create a new session instead")
10081009

10091010

pymongo/collection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
List,
2424
Mapping,
2525
MutableMapping,
26+
NoReturn,
2627
Optional,
2728
Sequence,
2829
Tuple,
@@ -343,7 +344,7 @@ def __ne__(self, other: Any) -> bool:
343344
def __hash__(self) -> int:
344345
return hash((self.__database, self.__name))
345346

346-
def __bool__(self) -> bool:
347+
def __bool__(self) -> NoReturn:
347348
raise NotImplementedError(
348349
"Collection objects do not implement truth "
349350
"value testing or bool(). Please compare "
@@ -3143,12 +3144,12 @@ def find_one_and_update(
31433144
def __iter__(self) -> "Collection[_DocumentType]":
31443145
return self
31453146

3146-
def __next__(self) -> None:
3147+
def __next__(self) -> NoReturn:
31473148
raise TypeError("'Collection' object is not iterable")
31483149

31493150
next = __next__
31503151

3151-
def __call__(self, *args: Any, **kwargs: Any) -> None:
3152+
def __call__(self, *args: Any, **kwargs: Any) -> NoReturn:
31523153
"""This is only here so that some API misusages are easier to debug."""
31533154
if "." not in self.__name:
31543155
raise TypeError(

pymongo/command_cursor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""CommandCursor class to iterate over command results."""
1616

1717
from collections import deque
18-
from typing import TYPE_CHECKING, Any, Generic, Iterator, Mapping, Optional
18+
from typing import TYPE_CHECKING, Any, Generic, Iterator, Mapping, NoReturn, Optional
1919

2020
from bson import _convert_raw_document_lists_to_streams
2121
from pymongo.cursor import _CURSOR_CLOSED_ERRORS, _SocketManager
@@ -344,5 +344,5 @@ def _unpack_response(
344344
_convert_raw_document_lists_to_streams(raw_response[0])
345345
return raw_response
346346

347-
def __getitem__(self, index):
347+
def __getitem__(self, index: int) -> NoReturn:
348348
raise InvalidOperation("Cannot call __getitem__ on RawBatchCursor")

pymongo/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
List,
2626
Mapping,
2727
MutableMapping,
28+
NoReturn,
2829
Optional,
2930
Sequence,
3031
Tuple,
@@ -153,7 +154,7 @@ def clean_node(node: str) -> Tuple[str, int]:
153154
return host.lower(), port
154155

155156

156-
def raise_config_error(key: str, dummy: Any) -> None:
157+
def raise_config_error(key: str, dummy: Any) -> NoReturn:
157158
"""Raise ConfigurationError with the given key name."""
158159
raise ConfigurationError("Unknown option %s" % (key,))
159160

pymongo/cursor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Iterable,
2626
List,
2727
Mapping,
28+
NoReturn,
2829
Optional,
2930
Sequence,
3031
Tuple,
@@ -1339,5 +1340,5 @@ def explain(self) -> _DocumentType:
13391340
clone = self._clone(deepcopy=True, base=Cursor(self.collection))
13401341
return clone.explain()
13411342

1342-
def __getitem__(self, index: Any) -> "Cursor[_DocumentType]":
1343+
def __getitem__(self, index: Any) -> NoReturn:
13431344
raise InvalidOperation("Cannot call __getitem__ on RawBatchCursor")

pymongo/database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
List,
2222
Mapping,
2323
MutableMapping,
24+
NoReturn,
2425
Optional,
2526
Sequence,
2627
TypeVar,
@@ -1010,12 +1011,12 @@ def validate_collection(
10101011
def __iter__(self) -> "Database[_DocumentType]":
10111012
return self
10121013

1013-
def __next__(self) -> "Database[_DocumentType]":
1014+
def __next__(self) -> NoReturn:
10141015
raise TypeError("'Database' object is not iterable")
10151016

10161017
next = __next__
10171018

1018-
def __bool__(self) -> bool:
1019+
def __bool__(self) -> NoReturn:
10191020
raise NotImplementedError(
10201021
"Database objects do not implement truth "
10211022
"value testing or bool(). Please compare "

0 commit comments

Comments
 (0)