diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 89d9830e82..6d5f26c503 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -59,8 +59,8 @@ jobs: cache-dependency-path: 'setup.py' - name: Install dependencies run: | - python -m pip install -U pip mypy==0.942 - pip install -e ".[zstd, srv]" + python -m pip install -U pip mypy + pip install -e ".[zstd, srv, encryption, ocsp]" - name: Run mypy run: | mypy --install-types --non-interactive bson gridfs tools pymongo diff --git a/bson/__init__.py b/bson/__init__.py index cc0850709e..2db1fb5d0b 100644 --- a/bson/__init__.py +++ b/bson/__init__.py @@ -61,8 +61,8 @@ import struct import sys import uuid -from codecs import utf_8_decode as _utf_8_decode # type: ignore[attr-defined] -from codecs import utf_8_encode as _utf_8_encode # type: ignore[attr-defined] +from codecs import utf_8_decode as _utf_8_decode +from codecs import utf_8_encode as _utf_8_encode from collections import abc as _abc from typing import ( IO, @@ -621,7 +621,7 @@ def _make_c_string_check(string: Union[str, bytes]) -> bytes: else: if "\x00" in string: raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character") - return cast(bytes, _utf_8_encode(string)[0]) + b"\x00" + return _utf_8_encode(string)[0] + b"\x00" def _make_c_string(string: Union[str, bytes]) -> bytes: @@ -633,7 +633,7 @@ def _make_c_string(string: Union[str, bytes]) -> bytes: except UnicodeError: raise InvalidStringData("strings in documents must be valid UTF-8: %r" % string) else: - return cast(bytes, _utf_8_encode(string)[0]) + b"\x00" + return _utf_8_encode(string)[0] + b"\x00" def _make_name(string: str) -> bytes: @@ -641,7 +641,7 @@ def _make_name(string: str) -> bytes: # Keys can only be text in python 3. if "\x00" in string: raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character") - return cast(bytes, _utf_8_encode(string)[0]) + b"\x00" + return _utf_8_encode(string)[0] + b"\x00" def _encode_float(name: bytes, value: float, dummy0: Any, dummy1: Any) -> bytes: @@ -1308,7 +1308,7 @@ def encode( """ return cls(encode(document, check_keys, codec_options)) - def decode(self, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> _DocumentType: # type: ignore[override] + def decode(self, codec_options: "CodecOptions[_DocumentType]" = DEFAULT_CODEC_OPTIONS) -> _DocumentType: # type: ignore[override,assignment] """Decode this BSON data. By default, returns a BSON document represented as a Python diff --git a/doc/conf.py b/doc/conf.py index 1e18eb29bf..f66de3868a 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -192,6 +192,6 @@ intersphinx_mapping = { - "gevent": ("http://www.gevent.org/", None), + "gevent": ("https://www.gevent.org/", None), "py": ("https://docs.python.org/3/", None), } diff --git a/pymongo/pyopenssl_context.py b/pymongo/pyopenssl_context.py index 758a741b6f..2d9c904bb3 100644 --- a/pymongo/pyopenssl_context.py +++ b/pymongo/pyopenssl_context.py @@ -135,7 +135,7 @@ def recv(self, *args, **kwargs): def recv_into(self, *args, **kwargs): try: - return self._call(super(_sslConn, self).recv_into, *args, **kwargs) # type: ignore + return self._call(super(_sslConn, self).recv_into, *args, **kwargs) except _SSL.SysCallError as exc: # Suppress ragged EOFs to match the stdlib. if self.suppress_ragged_eofs and _ragged_eof(exc): @@ -146,12 +146,9 @@ def sendall(self, buf, flags=0): view = memoryview(buf) total_length = len(buf) total_sent = 0 - sent = 0 while total_sent < total_length: try: - sent = self._call( - super(_sslConn, self).send, view[total_sent:], flags # type: ignore - ) + sent = self._call(super(_sslConn, self).send, view[total_sent:], flags) # XXX: It's not clear if this can actually happen. PyOpenSSL # doesn't appear to have any interrupt handling, nor any interrupt # errors for OpenSSL connections. @@ -162,7 +159,7 @@ def sendall(self, buf, flags=0): # https://github.com/pyca/pyopenssl/blob/19.1.0/src/OpenSSL/SSL.py#L1756 # https://www.openssl.org/docs/man1.0.2/man3/SSL_write.html if sent <= 0: - raise Exception("Connection closed") + raise OSError("connection closed") total_sent += sent diff --git a/test/test_auth.py b/test/test_auth.py index 69ed27bda0..20d53ef24b 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -329,8 +329,8 @@ def auth_string(user, password): bad_user = MongoClient(auth_string("not-user", SASL_PASS)) bad_pwd = MongoClient(auth_string(SASL_USER, "not-pwd")) # OperationFailure raised upon connecting. - self.assertRaises(OperationFailure, bad_user.admin.command, "ping") - self.assertRaises(OperationFailure, bad_pwd.admin.command, "ping") + self.assertRaises(OperationFailure, bad_user.admin.command, "ping") # type: ignore[arg-type] + self.assertRaises(OperationFailure, bad_pwd.admin.command, "ping") # type: ignore[arg-type] class TestSCRAMSHA1(IntegrationTest): diff --git a/test/test_bson.py b/test/test_bson.py index 8ad65f3412..aa77954fa2 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -117,7 +117,8 @@ def tzname(self, dt): class TestBSON(unittest.TestCase): def assertInvalid(self, data): - self.assertRaises(InvalidBSON, decode, data) + # Remove type ignore after: https://github.com/python/mypy/issues/13220 + self.assertRaises(InvalidBSON, decode, data) # type: ignore[arg-type] def check_encode_then_decode(self, doc_class=dict, decoder=decode, encoder=encode): @@ -1025,11 +1026,17 @@ def test_unicode_decode_error_handler(self): # Ensure that strict mode raises an error. for invalid in [invalid_key, invalid_val, invalid_both]: + # Remove type ignore after: https://github.com/python/mypy/issues/13220 self.assertRaises( - InvalidBSON, decode, invalid, CodecOptions(unicode_decode_error_handler="strict") + InvalidBSON, + decode, # type: ignore[arg-type] + invalid, + CodecOptions(unicode_decode_error_handler="strict"), ) - self.assertRaises(InvalidBSON, decode, invalid, CodecOptions()) - self.assertRaises(InvalidBSON, decode, invalid) + self.assertRaises( + InvalidBSON, decode, invalid, CodecOptions() # type: ignore[arg-type] + ) + self.assertRaises(InvalidBSON, decode, invalid) # type: ignore[arg-type] # Test all other error handlers. for handler in ["replace", "backslashreplace", "surrogateescape", "ignore"]: @@ -1046,8 +1053,12 @@ def test_unicode_decode_error_handler(self): dec = decode(enc, CodecOptions(unicode_decode_error_handler="junk")) self.assertEqual(dec, {"keystr": "foobar"}) + # Remove type ignore after: https://github.com/python/mypy/issues/13220 self.assertRaises( - InvalidBSON, decode, invalid_both, CodecOptions(unicode_decode_error_handler="junk") + InvalidBSON, + decode, # type: ignore[arg-type] + invalid_both, + CodecOptions(unicode_decode_error_handler="junk"), ) def round_trip_pickle(self, obj, pickled_with_older): diff --git a/test/test_change_stream.py b/test/test_change_stream.py index 11ed2895ac..b5b260086d 100644 --- a/test/test_change_stream.py +++ b/test/test_change_stream.py @@ -1084,8 +1084,9 @@ def setFailPoint(self, scenario_dict): fail_cmd = SON([("configureFailPoint", "failCommand")]) fail_cmd.update(fail_point) client_context.client.admin.command(fail_cmd) + # Remove type ignore after: https://github.com/python/mypy/issues/13220 self.addCleanup( - client_context.client.admin.command, + client_context.client.admin.command, # type: ignore[arg-type] "configureFailPoint", fail_cmd["configureFailPoint"], mode="off", diff --git a/test/test_collection.py b/test/test_collection.py index bea2ed6ca6..37f1b1eae2 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -19,7 +19,7 @@ import contextlib import re import sys -from codecs import utf_8_decode # type: ignore +from codecs import utf_8_decode from collections import defaultdict from typing import Iterable, no_type_check diff --git a/test/test_database.py b/test/test_database.py index d49ac8324f..a1c0439089 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -604,13 +604,14 @@ def test_command_max_time_ms(self): try: db = self.client.pymongo_test db.command("count", "test") - self.assertRaises(ExecutionTimeout, db.command, "count", "test", maxTimeMS=1) + # Remove type ignore after: https://github.com/python/mypy/issues/13220 + self.assertRaises(ExecutionTimeout, db.command, "count", "test", maxTimeMS=1) # type: ignore[arg-type] pipeline = [{"$project": {"name": 1, "count": 1}}] # Database command helper. db.command("aggregate", "test", pipeline=pipeline, cursor={}) self.assertRaises( ExecutionTimeout, - db.command, + db.command, # type: ignore[arg-type] "aggregate", "test", pipeline=pipeline,