Skip to content

Commit d4ef6b8

Browse files
committed
fix the tests
1 parent 2a86839 commit d4ef6b8

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
# Test overshadowed codec_options.py file
6868
mypy --install-types --non-interactive bson/codec_options.py
6969
mypy --install-types --non-interactive --disable-error-code var-annotated --disable-error-code attr-defined --disable-error-code union-attr --disable-error-code assignment --disable-error-code no-redef --disable-error-code index --allow-redefinition --allow-untyped-globals --exclude "test/mypy_fails/*.*" test
70+
mypy --install-types --non-interactive test/test_mypy.py
7071
7172
linkcheck:
7273
name: Check Links

pymongo/client_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def _max_time_expired_error(exc):
435435

436436
# From the transactions spec, all the retryable writes errors plus
437437
# WriteConcernFailed.
438-
_UNKNOWN_COMMIT_ERROR_CODES = _RETRYABLE_ERROR_CODES | frozenset(
438+
_UNKNOWN_COMMIT_ERROR_CODES: frozenset = _RETRYABLE_ERROR_CODES | frozenset(
439439
[
440440
64, # WriteConcernFailed
441441
50, # MaxTimeMSExpired

pymongo/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# From the SDAM spec, the "not primary" error codes are combined with the
4545
# "node is recovering" error codes (of which the "node is shutting down"
4646
# errors are a subset).
47-
_NOT_PRIMARY_CODES = (
47+
_NOT_PRIMARY_CODES: frozenset = (
4848
frozenset(
4949
[
5050
10058, # LegacyNotPrimary <=3.2 "not primary" error code
@@ -58,7 +58,7 @@
5858
| _SHUTDOWN_CODES
5959
)
6060
# From the retryable writes spec.
61-
_RETRYABLE_ERROR_CODES = _NOT_PRIMARY_CODES | frozenset(
61+
_RETRYABLE_ERROR_CODES: frozenset = _NOT_PRIMARY_CODES | frozenset(
6262
[
6363
7, # HostNotFound
6464
6, # HostUnreachable

pymongo/monitoring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def register(listener: _EventListener) -> None:
528528
# Note - to avoid bugs from forgetting which if these is all lowercase and
529529
# which are camelCase, and at the same time avoid having to add a test for
530530
# every command, use all lowercase here and test against command_name.lower().
531-
_SENSITIVE_COMMANDS = set(
531+
_SENSITIVE_COMMANDS: set = set(
532532
[
533533
"authenticate",
534534
"saslstart",

test/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343
HAVE_IPADDRESS = True
4444
except ImportError:
4545
HAVE_IPADDRESS = False
46-
4746
from contextlib import contextmanager
4847
from functools import wraps
4948
from test.version import Version
50-
from typing import Callable, Dict, Generator, no_type_check
49+
from typing import Any, Callable, Dict, Generator, no_type_check
5150
from unittest import SkipTest
5251
from urllib.parse import quote_plus
5352

@@ -331,7 +330,9 @@ def hello(self):
331330

332331
def _connect(self, host, port, **kwargs):
333332
kwargs.update(self.default_client_options)
334-
client = pymongo.MongoClient(host, port, serverSelectionTimeoutMS=5000, **kwargs)
333+
client: MongoClient = pymongo.MongoClient(
334+
host, port, serverSelectionTimeoutMS=5000, **kwargs
335+
)
335336
try:
336337
try:
337338
client.admin.command(HelloCompat.LEGACY_CMD) # Can we connect?
@@ -356,7 +357,7 @@ def _init_client(self):
356357
if self.client is not None:
357358
# Return early when connected to dataLake as mongohoused does not
358359
# support the getCmdLineOpts command and is tested without TLS.
359-
build_info = self.client.admin.command("buildInfo")
360+
build_info: Any = self.client.admin.command("buildInfo")
360361
if "dataLake" in build_info:
361362
self.is_data_lake = True
362363
self.auth_enabled = True
@@ -521,14 +522,16 @@ def has_secondaries(self):
521522
@property
522523
def storage_engine(self):
523524
try:
524-
return self.server_status.get("storageEngine", {}).get("name")
525+
return self.server_status.get("storageEngine", {}).get(
526+
"name"
527+
) # type:ignore[union-attr]
525528
except AttributeError:
526529
# Raised if self.server_status is None.
527530
return None
528531

529532
def _check_user_provided(self):
530533
"""Return True if db_user/db_password is already an admin user."""
531-
client = pymongo.MongoClient(
534+
client: MongoClient = pymongo.MongoClient(
532535
host,
533536
port,
534537
username=db_user,
@@ -694,7 +697,7 @@ def supports_secondary_read_pref(self):
694697
if self.has_secondaries:
695698
return True
696699
if self.is_mongos:
697-
shard = self.client.config.shards.find_one()["host"]
700+
shard = self.client.config.shards.find_one()["host"] # type:ignore[index]
698701
num_members = shard.count(",") + 1
699702
return num_members > 1
700703
return False
@@ -1015,12 +1018,12 @@ def fork(
10151018
"""
10161019

10171020
def _print_threads(*args: object) -> None:
1018-
if _print_threads.called:
1021+
if _print_threads.called: # type:ignore[attr-defined]
10191022
return
1020-
_print_threads.called = True
1023+
_print_threads.called = True # type:ignore[attr-defined]
10211024
print_thread_tracebacks()
10221025

1023-
_print_threads.called = False
1026+
_print_threads.called = False # type:ignore[attr-defined]
10241027

10251028
def _target() -> None:
10261029
signal.signal(signal.SIGUSR1, _print_threads)

test/test_mypy.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@
2828
# Not available in Python 3.7
2929
class Movie(TypedDict): # type: ignore[misc]
3030
_id: ObjectId
31-
idiot: ObjectId
31+
name: str
32+
year: int
33+
34+
class ImplicitMovie(TypedDict): # type: ignore[misc]
35+
_id: NotRequired[ObjectId]
36+
name: str
37+
year: int
38+
39+
class EmptyMovie(TypedDict): # type: ignore[misc]
3240
name: str
3341
year: int
3442

@@ -329,6 +337,31 @@ def test_typeddict_document_type_insertion(self) -> None:
329337
assert out["foo"] # type:ignore[typeddict-item]
330338
assert type(out["_id"]) == ObjectId
331339

340+
# This should work the same as the test above, but this time using NotRequired to allow
341+
# automatic insertion of the _id field by insert_one.
342+
@only_type_check
343+
def test_typeddict_document_type_not_required(self) -> None:
344+
client: MongoClient[ImplicitMovie] = MongoClient()
345+
coll: Collection[ImplicitMovie] = client.test.test
346+
insert = coll.insert_one(ImplicitMovie(name="THX-1138", year=1971))
347+
out = coll.find_one({"name": "THX-1138"})
348+
assert out is not None
349+
# This should fail because the output is a Movie.
350+
assert out["foo"] # type:ignore[typeddict-item]
351+
assert type(out["_id"]) == ObjectId
352+
353+
@only_type_check
354+
def test_typeddict_document_type_empty(self) -> None:
355+
client: MongoClient[EmptyMovie] = MongoClient()
356+
coll: Collection[EmptyMovie] = client.test.test
357+
insert = coll.insert_one(EmptyMovie(name="THX-1138", year=1971))
358+
out = coll.find_one({"name": "THX-1138"})
359+
assert out is not None
360+
# This should fail because the output is a Movie.
361+
assert out["foo"] # type:ignore[typeddict-item]
362+
# This should fail because _id is not included in our TypedDict definition.
363+
assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item]
364+
332365
@only_type_check
333366
def test_raw_bson_document_type(self) -> None:
334367
client = MongoClient(document_class=RawBSONDocument)

test/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from collections import abc, defaultdict
3030
from functools import partial
3131
from test import client_context, db_pwd, db_user
32+
from typing import Any
3233

3334
from bson import json_util
3435
from bson.objectid import ObjectId
@@ -601,7 +602,7 @@ def ensure_all_connected(client: MongoClient) -> None:
601602
Depending on the use-case, the caller may need to clear any event listeners
602603
that are configured on the client.
603604
"""
604-
hello = client.admin.command(HelloCompat.LEGACY_CMD)
605+
hello: Any = client.admin.command(HelloCompat.LEGACY_CMD)
605606
if "setName" not in hello:
606607
raise ConfigurationError("cluster is not a replica set")
607608

@@ -612,7 +613,7 @@ def ensure_all_connected(client: MongoClient) -> None:
612613
def discover():
613614
i = 0
614615
while i < 100 and connected_host_list != target_host_list:
615-
hello = client.admin.command(
616+
hello: Any = client.admin.command(
616617
HelloCompat.LEGACY_CMD, read_preference=ReadPreference.SECONDARY
617618
)
618619
connected_host_list.update([hello["me"]])

0 commit comments

Comments
 (0)