Skip to content

PYTHON-3907 add --disallow-untyped-defs for mypy #1351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ def has_c() -> bool:
return _USE_C


def _after_fork():
def _after_fork() -> None:
"""Releases the ObjectID lock child."""
if ObjectId._inc_lock.locked():
ObjectId._inc_lock.release()
Expand Down
2 changes: 1 addition & 1 deletion bson/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,5 @@ def __hash__(self) -> int:
def __ne__(self, other: Any) -> bool:
return not self == other

def __repr__(self):
def __repr__(self) -> str:
return f"Binary({bytes.__repr__(self)}, {self.__subtype})"
2 changes: 1 addition & 1 deletion bson/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def scope(self) -> Optional[Mapping[str, Any]]:
"""Scope dictionary for this instance or ``None``."""
return self.__scope

def __repr__(self):
def __repr__(self) -> str:
return f"Code({str.__repr__(self)}, {self.__scope!r})"

def __eq__(self, other: Any) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions bson/codec_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _validate_type_encoder(self, codec: _Codec) -> None:
)
raise TypeError(err_msg)

def __repr__(self):
def __repr__(self) -> str:
return "{}(type_codecs={!r}, fallback_encoder={!r})".format(
self.__class__.__name__,
self.__type_codecs,
Expand Down Expand Up @@ -465,7 +465,7 @@ def _options_dict(self) -> Dict[str, Any]:
"datetime_conversion": self.datetime_conversion,
}

def __repr__(self):
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._arguments_repr()})"

def with_options(self, **kwargs: Any) -> "CodecOptions":
Expand Down
4 changes: 2 additions & 2 deletions bson/datetime_ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ def __int__(self) -> int:
# Timezones are hashed by their offset, which is a timedelta
# and therefore there are more than 24 possible timezones.
@functools.lru_cache(maxsize=None)
def _min_datetime_ms(tz=datetime.timezone.utc):
def _min_datetime_ms(tz: datetime.timezone = datetime.timezone.utc) -> int:
return _datetime_to_millis(datetime.datetime.min.replace(tzinfo=tz))


@functools.lru_cache(maxsize=None)
def _max_datetime_ms(tz=datetime.timezone.utc):
def _max_datetime_ms(tz: datetime.timezone = datetime.timezone.utc) -> int:
return _datetime_to_millis(datetime.datetime.max.replace(tzinfo=tz))


Expand Down
2 changes: 1 addition & 1 deletion bson/dbref.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def as_doc(self) -> SON[str, Any]:
doc.update(self.__kwargs)
return doc

def __repr__(self):
def __repr__(self) -> str:
extra = "".join([f", {k}={v!r}" for k, v in self.__kwargs.items()])
if self.database is None:
return f"DBRef({self.collection!r}, {self.id!r}{extra})"
Expand Down
2 changes: 1 addition & 1 deletion bson/decimal128.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def __str__(self) -> str:
return "NaN"
return str(dec)

def __repr__(self):
def __repr__(self) -> str:
return f"Decimal128('{str(self)}')"

def __setstate__(self, value: Tuple[int, int]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion bson/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class JSONOptions(CodecOptions):
datetime_representation: int
strict_uuid: bool

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Any):
"""Encapsulates JSON options for :func:`dumps` and :func:`loads`.

:Parameters:
Expand Down
2 changes: 1 addition & 1 deletion bson/max_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ def __ge__(self, dummy: Any) -> bool:
def __gt__(self, other: Any) -> bool:
return not isinstance(other, MaxKey)

def __repr__(self):
def __repr__(self) -> str:
return "MaxKey()"
2 changes: 1 addition & 1 deletion bson/min_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ def __ge__(self, other: Any) -> bool:
def __gt__(self, dummy: Any) -> bool:
return False

def __repr__(self):
def __repr__(self) -> str:
return "MinKey()"
2 changes: 1 addition & 1 deletion bson/objectid.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def __setstate__(self, value: Any) -> None:
def __str__(self) -> str:
return binascii.hexlify(self.__id).decode()

def __repr__(self):
def __repr__(self) -> str:
return f"ObjectId('{str(self)}')"

def __eq__(self, other: Any) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion bson/raw_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def __eq__(self, other: Any) -> bool:
return self.__raw == other.raw
return NotImplemented

def __repr__(self):
def __repr__(self) -> str:
return "{}({!r}, codec_options={!r})".format(
self.__class__.__name__,
self.raw,
Expand Down
2 changes: 1 addition & 1 deletion bson/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __eq__(self, other: Any) -> bool:
def __ne__(self, other: Any) -> bool:
return not self == other

def __repr__(self):
def __repr__(self) -> str:
return f"Regex({self.pattern!r}, {self.flags!r})"

def try_compile(self) -> "Pattern[_T]":
Expand Down
2 changes: 1 addition & 1 deletion bson/son.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __new__(cls: Type["SON[_Key, _Value]"], *args: Any, **kwargs: Any) -> "SON[_
instance.__keys = []
return instance

def __repr__(self):
def __repr__(self) -> str:
result = []
for key in self.__keys:
result.append(f"({key!r}, {self[key]!r})")
Expand Down
2 changes: 1 addition & 1 deletion bson/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __ge__(self, other: Any) -> bool:
return (self.time, self.inc) >= (other.time, other.inc)
return NotImplemented

def __repr__(self):
def __repr__(self) -> str:
return f"Timestamp({self.__time}, {self.__inc})"

def as_datetime(self) -> datetime.datetime:
Expand Down
8 changes: 5 additions & 3 deletions pymongo/_csot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Internal helpers for CSOT."""

from __future__ import annotations

import functools
import time
from collections import deque
Expand Down Expand Up @@ -72,7 +74,7 @@ def __init__(self, timeout: Optional[float]):
self._timeout = timeout
self._tokens: Optional[Tuple[Token, Token, Token]] = None

def __enter__(self):
def __enter__(self) -> _TimeoutContext:
timeout_token = TIMEOUT.set(self._timeout)
prev_deadline = DEADLINE.get()
next_deadline = time.monotonic() + self._timeout if self._timeout else float("inf")
Expand All @@ -81,7 +83,7 @@ def __enter__(self):
self._tokens = (timeout_token, deadline_token, rtt_token)
return self

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
if self._tokens:
timeout_token, deadline_token, rtt_token = self._tokens
TIMEOUT.reset(timeout_token)
Expand All @@ -97,7 +99,7 @@ def apply(func: F) -> F:
"""Apply the client's timeoutMS to this operation."""

@functools.wraps(func)
def csot_wrapper(self, *args, **kwargs):
def csot_wrapper(self: Any, *args: Any, **kwargs: Any) -> F:
if get_timeout() is None:
timeout = self._timeout
if timeout is not None:
Expand Down
8 changes: 4 additions & 4 deletions pymongo/auth_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
except ImportError:

class AwsSaslContext: # type: ignore
def __init__(self, credentials):
def __init__(self, credentials: MongoCredential):
pass

_HAVE_MONGODB_AWS = False
Expand All @@ -35,11 +35,11 @@ def __init__(self, credentials):
set_use_cached_credentials(True)
except ImportError:

def set_cached_credentials(creds):
def set_cached_credentials(creds: Optional[AwsCredential]) -> None:
pass


from typing import TYPE_CHECKING, Any, Mapping
from typing import TYPE_CHECKING, Any, Mapping, Optional, Type

import bson
from bson.binary import Binary
Expand All @@ -54,7 +54,7 @@ def set_cached_credentials(creds):

class _AwsSaslContext(AwsSaslContext): # type: ignore
# Dependency injection:
def binary_type(self):
def binary_type(self) -> Type[Binary]:
"""Return the bson.binary.Binary type."""
return Binary

Expand Down
2 changes: 1 addition & 1 deletion pymongo/change_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def _run_aggregation_cmd(
cmd.get_cursor, self._target._read_preference_for(session), session
)

def _create_cursor(self):
def _create_cursor(self) -> CommandCursor:
with self._client._tmp_session(self._session, close=False) as s:
return self._run_aggregation_cmd(session=s, explicit_session=self._session is not None)

Expand Down
2 changes: 1 addition & 1 deletion pymongo/collation.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def document(self) -> Dict[str, Any]:
"""
return self.__document.copy()

def __repr__(self):
def __repr__(self) -> str:
document = self.document
return "Collation({})".format(", ".join(f"{key}={document[key]!r}" for key in document))

Expand Down
2 changes: 1 addition & 1 deletion pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2481,7 +2481,7 @@ def create_search_indexes(
if comment is not None:
kwargs["comment"] = comment

def gen_indexes():
def gen_indexes() -> Iterator[Mapping[str, Any]]:
for index in models:
if not isinstance(index, SearchIndexModel):
raise TypeError(
Expand Down
2 changes: 1 addition & 1 deletion pymongo/event_loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class ConnectionPoolLogger(monitoring.ConnectionPoolListener):
def pool_created(self, event: monitoring.PoolCreatedEvent) -> None:
logging.info(f"[pool {event.address}] pool created")

def pool_ready(self, event):
def pool_ready(self, event: monitoring.PoolReadyEvent) -> None:
logging.info(f"[pool {event.address}] pool ready")

def pool_cleared(self, event: monitoring.PoolClearedEvent) -> None:
Expand Down
13 changes: 9 additions & 4 deletions pymongo/ocsp_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from collections import namedtuple
from datetime import datetime as _datetime
from datetime import timezone
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any, Dict

from pymongo.lock import _create_lock

Expand All @@ -35,8 +35,8 @@ class _OCSPCache:
["hash_algorithm", "issuer_name_hash", "issuer_key_hash", "serial_number"],
)

def __init__(self):
self._data = {}
def __init__(self) -> None:
self._data: Dict[Any, OCSPResponse] = {}
# Hold this lock when accessing _data.
self._lock = _create_lock()

Expand Down Expand Up @@ -77,7 +77,10 @@ def __setitem__(self, key: OCSPRequest, value: OCSPResponse) -> None:
# Cache new response OR update cached response if new response
# has longer validity.
cached_value = self._data.get(cache_key, None)
if cached_value is None or cached_value.next_update < value.next_update:
if cached_value is None or (
cached_value.next_update is not None
and cached_value.next_update < value.next_update
):
self._data[cache_key] = value

def __getitem__(self, item: OCSPRequest) -> OCSPResponse:
Expand All @@ -92,6 +95,8 @@ def __getitem__(self, item: OCSPRequest) -> OCSPResponse:
value = self._data[cache_key]

# Return cached response if it is still valid.
assert value.this_update is not None
assert value.next_update is not None
if (
value.this_update
<= _datetime.now(tz=timezone.utc).replace(tzinfo=None)
Expand Down
2 changes: 1 addition & 1 deletion pymongo/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ def __repr__(self) -> str:
msg = "CLOSED "
return f"<{self.__class__.__name__} {msg}{self._description!r}>"

def eq_props(self):
def eq_props(self) -> Tuple[Tuple[_Address, ...], Optional[str], Optional[str], str]:
"""The properties to use for MongoClient/Topology equality checks."""
ts = self._settings
return (tuple(sorted(ts.seeds)), ts.replica_set_name, ts.fqdn, ts.srv_service_name)
Expand Down
4 changes: 2 additions & 2 deletions tools/ocsptest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
logging.basicConfig(format=FORMAT, level=logging.DEBUG)


def check_ocsp(host, port, capath):
def check_ocsp(host: str, port: int, capath: str) -> None:
ctx = get_ssl_context(
None, # certfile
None, # passphrase
Expand All @@ -47,7 +47,7 @@ def check_ocsp(host, port, capath):
s.close()


def main():
def main() -> None:
parser = argparse.ArgumentParser(description="Debug OCSP")
parser.add_argument("--host", type=str, required=True, help="Host to connect to")
parser.add_argument("-p", "--port", type=int, default=443, help="Port to connect to")
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ deps =
certifi; platform_system == "win32" or platform_system == "Darwin"
typing_extensions
commands =
mypy --install-types --non-interactive bson gridfs tools pymongo
mypy --install-types --non-interactive --disallow-untyped-defs bson gridfs tools pymongo
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/*.*" --exclude "test/conftest.py" test
mypy --install-types --non-interactive test/test_typing.py test/test_typing_strict.py

Expand Down