Skip to content

Commit 864812d

Browse files
authored
PYTHON-3366 Support mypy 0.971 and test with latest version (#1021)
PYTHON-3369 Use https://www.gevent.org
1 parent f5ac946 commit 864812d

File tree

9 files changed

+36
-26
lines changed

9 files changed

+36
-26
lines changed

.github/workflows/test-python.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ jobs:
5959
cache-dependency-path: 'setup.py'
6060
- name: Install dependencies
6161
run: |
62-
python -m pip install -U pip mypy==0.942
63-
pip install -e ".[zstd, srv]"
62+
python -m pip install -U pip mypy
63+
pip install -e ".[zstd, srv, encryption, ocsp]"
6464
- name: Run mypy
6565
run: |
6666
mypy --install-types --non-interactive bson gridfs tools pymongo

bson/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
import struct
6262
import sys
6363
import uuid
64-
from codecs import utf_8_decode as _utf_8_decode # type: ignore[attr-defined]
65-
from codecs import utf_8_encode as _utf_8_encode # type: ignore[attr-defined]
64+
from codecs import utf_8_decode as _utf_8_decode
65+
from codecs import utf_8_encode as _utf_8_encode
6666
from collections import abc as _abc
6767
from typing import (
6868
IO,
@@ -621,7 +621,7 @@ def _make_c_string_check(string: Union[str, bytes]) -> bytes:
621621
else:
622622
if "\x00" in string:
623623
raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character")
624-
return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
624+
return _utf_8_encode(string)[0] + b"\x00"
625625

626626

627627
def _make_c_string(string: Union[str, bytes]) -> bytes:
@@ -633,15 +633,15 @@ def _make_c_string(string: Union[str, bytes]) -> bytes:
633633
except UnicodeError:
634634
raise InvalidStringData("strings in documents must be valid UTF-8: %r" % string)
635635
else:
636-
return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
636+
return _utf_8_encode(string)[0] + b"\x00"
637637

638638

639639
def _make_name(string: str) -> bytes:
640640
"""Make a 'C' string suitable for a BSON key."""
641641
# Keys can only be text in python 3.
642642
if "\x00" in string:
643643
raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character")
644-
return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
644+
return _utf_8_encode(string)[0] + b"\x00"
645645

646646

647647
def _encode_float(name: bytes, value: float, dummy0: Any, dummy1: Any) -> bytes:
@@ -1308,7 +1308,7 @@ def encode(
13081308
"""
13091309
return cls(encode(document, check_keys, codec_options))
13101310

1311-
def decode(self, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> _DocumentType: # type: ignore[override]
1311+
def decode(self, codec_options: "CodecOptions[_DocumentType]" = DEFAULT_CODEC_OPTIONS) -> _DocumentType: # type: ignore[override,assignment]
13121312
"""Decode this BSON data.
13131313
13141314
By default, returns a BSON document represented as a Python

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,6 @@
192192

193193

194194
intersphinx_mapping = {
195-
"gevent": ("http://www.gevent.org/", None),
195+
"gevent": ("https://www.gevent.org/", None),
196196
"py": ("https://docs.python.org/3/", None),
197197
}

pymongo/pyopenssl_context.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def recv(self, *args, **kwargs):
135135

136136
def recv_into(self, *args, **kwargs):
137137
try:
138-
return self._call(super(_sslConn, self).recv_into, *args, **kwargs) # type: ignore
138+
return self._call(super(_sslConn, self).recv_into, *args, **kwargs)
139139
except _SSL.SysCallError as exc:
140140
# Suppress ragged EOFs to match the stdlib.
141141
if self.suppress_ragged_eofs and _ragged_eof(exc):
@@ -146,12 +146,9 @@ def sendall(self, buf, flags=0):
146146
view = memoryview(buf)
147147
total_length = len(buf)
148148
total_sent = 0
149-
sent = 0
150149
while total_sent < total_length:
151150
try:
152-
sent = self._call(
153-
super(_sslConn, self).send, view[total_sent:], flags # type: ignore
154-
)
151+
sent = self._call(super(_sslConn, self).send, view[total_sent:], flags)
155152
# XXX: It's not clear if this can actually happen. PyOpenSSL
156153
# doesn't appear to have any interrupt handling, nor any interrupt
157154
# errors for OpenSSL connections.
@@ -162,7 +159,7 @@ def sendall(self, buf, flags=0):
162159
# https://github.com/pyca/pyopenssl/blob/19.1.0/src/OpenSSL/SSL.py#L1756
163160
# https://www.openssl.org/docs/man1.0.2/man3/SSL_write.html
164161
if sent <= 0:
165-
raise Exception("Connection closed")
162+
raise OSError("connection closed")
166163
total_sent += sent
167164

168165

test/test_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ def auth_string(user, password):
329329
bad_user = MongoClient(auth_string("not-user", SASL_PASS))
330330
bad_pwd = MongoClient(auth_string(SASL_USER, "not-pwd"))
331331
# OperationFailure raised upon connecting.
332-
self.assertRaises(OperationFailure, bad_user.admin.command, "ping")
333-
self.assertRaises(OperationFailure, bad_pwd.admin.command, "ping")
332+
self.assertRaises(OperationFailure, bad_user.admin.command, "ping") # type: ignore[arg-type]
333+
self.assertRaises(OperationFailure, bad_pwd.admin.command, "ping") # type: ignore[arg-type]
334334

335335

336336
class TestSCRAMSHA1(IntegrationTest):

test/test_bson.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def tzname(self, dt):
117117

118118
class TestBSON(unittest.TestCase):
119119
def assertInvalid(self, data):
120-
self.assertRaises(InvalidBSON, decode, data)
120+
# Remove type ignore after: https://github.com/python/mypy/issues/13220
121+
self.assertRaises(InvalidBSON, decode, data) # type: ignore[arg-type]
121122

122123
def check_encode_then_decode(self, doc_class=dict, decoder=decode, encoder=encode):
123124

@@ -1025,11 +1026,17 @@ def test_unicode_decode_error_handler(self):
10251026

10261027
# Ensure that strict mode raises an error.
10271028
for invalid in [invalid_key, invalid_val, invalid_both]:
1029+
# Remove type ignore after: https://github.com/python/mypy/issues/13220
10281030
self.assertRaises(
1029-
InvalidBSON, decode, invalid, CodecOptions(unicode_decode_error_handler="strict")
1031+
InvalidBSON,
1032+
decode, # type: ignore[arg-type]
1033+
invalid,
1034+
CodecOptions(unicode_decode_error_handler="strict"),
10301035
)
1031-
self.assertRaises(InvalidBSON, decode, invalid, CodecOptions())
1032-
self.assertRaises(InvalidBSON, decode, invalid)
1036+
self.assertRaises(
1037+
InvalidBSON, decode, invalid, CodecOptions() # type: ignore[arg-type]
1038+
)
1039+
self.assertRaises(InvalidBSON, decode, invalid) # type: ignore[arg-type]
10331040

10341041
# Test all other error handlers.
10351042
for handler in ["replace", "backslashreplace", "surrogateescape", "ignore"]:
@@ -1046,8 +1053,12 @@ def test_unicode_decode_error_handler(self):
10461053
dec = decode(enc, CodecOptions(unicode_decode_error_handler="junk"))
10471054
self.assertEqual(dec, {"keystr": "foobar"})
10481055

1056+
# Remove type ignore after: https://github.com/python/mypy/issues/13220
10491057
self.assertRaises(
1050-
InvalidBSON, decode, invalid_both, CodecOptions(unicode_decode_error_handler="junk")
1058+
InvalidBSON,
1059+
decode, # type: ignore[arg-type]
1060+
invalid_both,
1061+
CodecOptions(unicode_decode_error_handler="junk"),
10511062
)
10521063

10531064
def round_trip_pickle(self, obj, pickled_with_older):

test/test_change_stream.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,9 @@ def setFailPoint(self, scenario_dict):
10841084
fail_cmd = SON([("configureFailPoint", "failCommand")])
10851085
fail_cmd.update(fail_point)
10861086
client_context.client.admin.command(fail_cmd)
1087+
# Remove type ignore after: https://github.com/python/mypy/issues/13220
10871088
self.addCleanup(
1088-
client_context.client.admin.command,
1089+
client_context.client.admin.command, # type: ignore[arg-type]
10891090
"configureFailPoint",
10901091
fail_cmd["configureFailPoint"],
10911092
mode="off",

test/test_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import contextlib
2020
import re
2121
import sys
22-
from codecs import utf_8_decode # type: ignore
22+
from codecs import utf_8_decode
2323
from collections import defaultdict
2424
from typing import Iterable, no_type_check
2525

test/test_database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,14 @@ def test_command_max_time_ms(self):
604604
try:
605605
db = self.client.pymongo_test
606606
db.command("count", "test")
607-
self.assertRaises(ExecutionTimeout, db.command, "count", "test", maxTimeMS=1)
607+
# Remove type ignore after: https://github.com/python/mypy/issues/13220
608+
self.assertRaises(ExecutionTimeout, db.command, "count", "test", maxTimeMS=1) # type: ignore[arg-type]
608609
pipeline = [{"$project": {"name": 1, "count": 1}}]
609610
# Database command helper.
610611
db.command("aggregate", "test", pipeline=pipeline, cursor={})
611612
self.assertRaises(
612613
ExecutionTimeout,
613-
db.command,
614+
db.command, # type: ignore[arg-type]
614615
"aggregate",
615616
"test",
616617
pipeline=pipeline,

0 commit comments

Comments
 (0)