Skip to content

Commit 3ef565f

Browse files
authored
PYTHON-4796 Update type checkers and handle with_options typing (mongodb#1880)
1 parent 1e395de commit 3ef565f

File tree

20 files changed

+132
-33
lines changed

20 files changed

+132
-33
lines changed

bson/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ def decode_iter(
13241324
elements = data[position : position + obj_size]
13251325
position += obj_size
13261326

1327-
yield _bson_to_dict(elements, opts) # type:ignore[misc, type-var]
1327+
yield _bson_to_dict(elements, opts) # type:ignore[misc]
13281328

13291329

13301330
@overload
@@ -1370,7 +1370,7 @@ def decode_file_iter(
13701370
raise InvalidBSON("cut off in middle of objsize")
13711371
obj_size = _UNPACK_INT_FROM(size_data, 0)[0] - 4
13721372
elements = size_data + file_obj.read(max(0, obj_size))
1373-
yield _bson_to_dict(elements, opts) # type:ignore[type-var, arg-type, misc]
1373+
yield _bson_to_dict(elements, opts) # type:ignore[arg-type, misc]
13741374

13751375

13761376
def is_valid(bson: bytes) -> bool:

bson/decimal128.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def __init__(self, value: _VALUE_OPTIONS) -> None:
223223
"from list or tuple. Must have exactly 2 "
224224
"elements."
225225
)
226-
self.__high, self.__low = value # type: ignore
226+
self.__high, self.__low = value
227227
else:
228228
raise TypeError(f"Cannot convert {value!r} to Decimal128")
229229

bson/json_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def __new__(
324324
"JSONOptions.datetime_representation must be one of LEGACY, "
325325
"NUMBERLONG, or ISO8601 from DatetimeRepresentation."
326326
)
327-
self = cast(JSONOptions, super().__new__(cls, *args, **kwargs)) # type:ignore[arg-type]
327+
self = cast(JSONOptions, super().__new__(cls, *args, **kwargs))
328328
if json_mode not in (JSONMode.LEGACY, JSONMode.RELAXED, JSONMode.CANONICAL):
329329
raise ValueError(
330330
"JSONOptions.json_mode must be one of LEGACY, RELAXED, "

bson/son.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(
6868
self.update(kwargs)
6969

7070
def __new__(cls: Type[SON[_Key, _Value]], *args: Any, **kwargs: Any) -> SON[_Key, _Value]:
71-
instance = super().__new__(cls, *args, **kwargs) # type: ignore[type-var]
71+
instance = super().__new__(cls, *args, **kwargs)
7272
instance.__keys = []
7373
return instance
7474

hatch.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ features = ["docs","test"]
1313
test = "sphinx-build -E -b doctest doc ./doc/_build/doctest"
1414

1515
[envs.typing]
16-
features = ["encryption", "ocsp", "zstd", "aws"]
17-
dependencies = ["mypy==1.2.0","pyright==1.1.290", "certifi", "typing_extensions"]
16+
pre-install-commands = [
17+
"pip install -q -r requirements/typing.txt",
18+
]
1819
[envs.typing.scripts]
1920
check-mypy = [
2021
"mypy --install-types --non-interactive bson gridfs tools pymongo",

pymongo/_csot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@ def __init__(self, timeout: Optional[float]):
7575
self._timeout = timeout
7676
self._tokens: Optional[tuple[Token[Optional[float]], Token[float], Token[float]]] = None
7777

78-
def __enter__(self) -> _TimeoutContext:
78+
def __enter__(self) -> None:
7979
timeout_token = TIMEOUT.set(self._timeout)
8080
prev_deadline = DEADLINE.get()
8181
next_deadline = time.monotonic() + self._timeout if self._timeout else float("inf")
8282
deadline_token = DEADLINE.set(min(prev_deadline, next_deadline))
8383
rtt_token = RTT.set(0.0)
8484
self._tokens = (timeout_token, deadline_token, rtt_token)
85-
return self
8685

8786
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
8887
if self._tokens:

pymongo/asynchronous/collection.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
TypeVar,
3636
Union,
3737
cast,
38+
overload,
3839
)
3940

4041
from bson.codec_options import DEFAULT_CODEC_OPTIONS, CodecOptions
@@ -332,13 +333,33 @@ def database(self) -> AsyncDatabase[_DocumentType]:
332333
"""
333334
return self._database
334335

336+
@overload
337+
def with_options(
338+
self,
339+
codec_options: None = None,
340+
read_preference: Optional[_ServerMode] = ...,
341+
write_concern: Optional[WriteConcern] = ...,
342+
read_concern: Optional[ReadConcern] = ...,
343+
) -> AsyncCollection[_DocumentType]:
344+
...
345+
346+
@overload
347+
def with_options(
348+
self,
349+
codec_options: bson.CodecOptions[_DocumentTypeArg],
350+
read_preference: Optional[_ServerMode] = ...,
351+
write_concern: Optional[WriteConcern] = ...,
352+
read_concern: Optional[ReadConcern] = ...,
353+
) -> AsyncCollection[_DocumentTypeArg]:
354+
...
355+
335356
def with_options(
336357
self,
337358
codec_options: Optional[bson.CodecOptions[_DocumentTypeArg]] = None,
338359
read_preference: Optional[_ServerMode] = None,
339360
write_concern: Optional[WriteConcern] = None,
340361
read_concern: Optional[ReadConcern] = None,
341-
) -> AsyncCollection[_DocumentType]:
362+
) -> AsyncCollection[_DocumentType] | AsyncCollection[_DocumentTypeArg]:
342363
"""Get a clone of this collection changing the specified settings.
343364
344365
>>> coll1.read_preference

pymongo/asynchronous/database.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,33 @@ def name(self) -> str:
146146
"""The name of this :class:`AsyncDatabase`."""
147147
return self._name
148148

149+
@overload
150+
def with_options(
151+
self,
152+
codec_options: None = None,
153+
read_preference: Optional[_ServerMode] = ...,
154+
write_concern: Optional[WriteConcern] = ...,
155+
read_concern: Optional[ReadConcern] = ...,
156+
) -> AsyncDatabase[_DocumentType]:
157+
...
158+
159+
@overload
160+
def with_options(
161+
self,
162+
codec_options: bson.CodecOptions[_DocumentTypeArg],
163+
read_preference: Optional[_ServerMode] = ...,
164+
write_concern: Optional[WriteConcern] = ...,
165+
read_concern: Optional[ReadConcern] = ...,
166+
) -> AsyncDatabase[_DocumentTypeArg]:
167+
...
168+
149169
def with_options(
150170
self,
151171
codec_options: Optional[CodecOptions[_DocumentTypeArg]] = None,
152172
read_preference: Optional[_ServerMode] = None,
153173
write_concern: Optional[WriteConcern] = None,
154174
read_concern: Optional[ReadConcern] = None,
155-
) -> AsyncDatabase[_DocumentType]:
175+
) -> AsyncDatabase[_DocumentType] | AsyncDatabase[_DocumentTypeArg]:
156176
"""Get a clone of this database changing the specified settings.
157177
158178
>>> db1.read_preference

pymongo/asynchronous/pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ async def _configured_socket(
913913
and not options.tls_allow_invalid_hostnames
914914
):
915915
try:
916-
ssl.match_hostname(ssl_sock.getpeercert(), hostname=host)
916+
ssl.match_hostname(ssl_sock.getpeercert(), hostname=host) # type:ignore[attr-defined]
917917
except _CertificateError:
918918
ssl_sock.close()
919919
raise

pymongo/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ def get_normed_key(x: str) -> str:
850850
return x
851851

852852
def get_setter_key(x: str) -> str:
853-
return options.cased_key(x) # type: ignore[attr-defined]
853+
return options.cased_key(x)
854854

855855
else:
856856
validated_options = {}

0 commit comments

Comments
 (0)