From 9a86b616ddd882667e30f4fa882edd5a9500950a Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 11 Dec 2023 12:35:06 -0600 Subject: [PATCH] MOTOR-1221 Add pyi lint checks (#242) * MOTOR-1221 Add pyi lint checks * cleanup --- motor/core.pyi | 34 +++++++++++++++------------------- motor/motor_gridfs.pyi | 14 ++++++-------- pyproject.toml | 4 +++- tox.ini | 2 +- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/motor/core.pyi b/motor/core.pyi index 04e342e2..409309e1 100644 --- a/motor/core.pyi +++ b/motor/core.pyi @@ -12,10 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Framework-agnostic type stubs for Motor, an asynchronous driver for MongoDB.""" - -from __future__ import annotations - from asyncio import Future from typing import ( Any, @@ -38,13 +34,14 @@ import pymongo.common import pymongo.database import pymongo.errors import pymongo.mongo_client +import typing_extensions from bson import Binary, Code, CodecOptions, DBRef, Timestamp from bson.raw_bson import RawBSONDocument from pymongo import IndexModel, ReadPreference, WriteConcern from pymongo.change_stream import ChangeStream from pymongo.client_options import ClientOptions from pymongo.client_session import _T, ClientSession, SessionOptions, TransactionOptions -from pymongo.collection import ReturnDocument, _WriteOp +from pymongo.collection import ReturnDocument, _WriteOp # noqa: F401 from pymongo.command_cursor import CommandCursor, RawBatchCommandCursor from pymongo.cursor import Cursor, RawBatchCursor, _Hint, _Sort from pymongo.database import Database @@ -72,7 +69,7 @@ from pymongo.typings import ( try: from pymongo.operations import SearchIndexModel except ImportError: - SearchIndexModel = Any # type:ignore[misc,assignment] + SearchIndexModel: typing_extensions.TypeAlias = Any # type:ignore[no-redef] _WITH_TRANSACTION_RETRY_TIME_LIMIT: int @@ -84,9 +81,8 @@ def _max_time_expired_error(exc: Exception) -> bool: ... class AgnosticBase: delegate: Any - def __eq__(self, other: Any) -> bool: ... + def __eq__(self, other: object) -> bool: ... def __init__(self, delegate: Any) -> None: ... - def __repr__(self) -> str: ... class AgnosticBaseProperties(AgnosticBase, Generic[_DocumentType]): codec_options: CodecOptions[_DocumentType] @@ -188,7 +184,7 @@ class _MotorTransactionContext: def __init__(self, session: AgnosticClientSession): ... async def __aenter__(self) -> _MotorTransactionContext: ... - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + async def __aexit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... class AgnosticClientSession(AgnosticBase): __motor_class_name__: str @@ -225,9 +221,9 @@ class AgnosticClientSession(AgnosticBase): @property def client(self) -> AgnosticClient: ... async def __aenter__(self) -> AgnosticClientSession: ... - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + async def __aexit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... def __enter__(self) -> None: ... - def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + def __exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... class AgnosticDatabase(AgnosticBaseProperties[_DocumentType]): __motor_class_name__: str @@ -485,7 +481,7 @@ class AgnosticCollection(AgnosticBaseProperties[_DocumentType]): projection: Optional[Union[Mapping[str, Any], Iterable[str]]] = None, sort: Optional[_IndexList] = None, upsert: bool = False, - return_document: bool = ReturnDocument.BEFORE, + return_document: bool = ..., hint: Optional[_IndexKeyHint] = None, session: Optional[AgnosticClientSession] = None, let: Optional[Mapping[str, Any]] = None, @@ -499,7 +495,7 @@ class AgnosticCollection(AgnosticBaseProperties[_DocumentType]): projection: Optional[Union[Mapping[str, Any], Iterable[str]]] = None, sort: Optional[_IndexList] = None, upsert: bool = False, - return_document: bool = ReturnDocument.BEFORE, + return_document: bool = ..., array_filters: Optional[Sequence[Mapping[str, Any]]] = None, hint: Optional[_IndexKeyHint] = None, session: Optional[AgnosticClientSession] = None, @@ -572,7 +568,7 @@ class AgnosticCollection(AgnosticBaseProperties[_DocumentType]): collation: Optional[_CollationIn] = None, array_filters: Optional[Sequence[Mapping[str, Any]]] = None, hint: Optional[_IndexKeyHint] = None, - session: Union[Optional[AgnosticClientSession], Optional[AgnosticClientSession]] = None, + session: Optional[AgnosticClientSession] = None, let: Optional[Mapping[str, Any]] = None, comment: Optional[Any] = None, ) -> UpdateResult: ... @@ -676,7 +672,7 @@ class AgnosticBaseCursor(AgnosticBase): async def next(self) -> _DocumentType: ... __anext__ = next async def __aenter__(self) -> Any: ... - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any: ... + async def __aexit__(self, exc_type: object, exc_val: object, exc_tb: object) -> Any: ... def _get_more(self) -> int: ... @property def fetch_next(self) -> Future[Any]: ... @@ -786,10 +782,10 @@ class AgnosticChangeStream(AgnosticBase): def __aiter__(self) -> AgnosticChangeStream: ... __anext__ = next async def __aenter__(self) -> AgnosticChangeStream: ... - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + async def __aexit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... def get_io_loop(self) -> Any: ... def __enter__(self) -> None: ... - def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + def __exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... class AgnosticClientEncryption(AgnosticBase): __motor_class_name__: str @@ -849,9 +845,9 @@ class AgnosticClientEncryption(AgnosticBase): def io_loop(self) -> Any: ... def get_io_loop(self) -> Any: ... async def __aenter__(self) -> AgnosticClientEncryption: ... - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + async def __aexit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... def __enter__(self) -> NoReturn: ... - def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + def __exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... async def get_keys(self) -> AgnosticCursor: ... async def create_encrypted_collection( self, diff --git a/motor/motor_gridfs.pyi b/motor/motor_gridfs.pyi index ccd2469f..1cb1e85b 100644 --- a/motor/motor_gridfs.pyi +++ b/motor/motor_gridfs.pyi @@ -12,14 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""GridFS type stubs for Motor, an asynchronous driver for MongoDB.""" - import datetime import os from typing import Any, Iterable, Mapping, NoReturn, Optional from bson import ObjectId -from gridfs import DEFAULT_CHUNK_SIZE, GridFSBucket, GridIn, GridOut, GridOutCursor +from gridfs import DEFAULT_CHUNK_SIZE, GridFSBucket, GridIn, GridOut, GridOutCursor # noqa: F401 from pymongo import WriteConcern from pymongo.read_preferences import _ServerMode @@ -36,7 +34,7 @@ _SEEK_END = os.SEEK_END class AgnosticGridOutCursor(AgnosticCursor): __motor_class_name__: str - __delegate_class__ = type[GridOutCursor] + __delegate_class__: type[GridOutCursor] async def _Cursor__die(self, synchronous: bool = False) -> None: ... def next_object(self) -> AgnosticGridOutCursor: ... @@ -58,7 +56,7 @@ class AgnosticGridOut: def readable(self) -> bool: ... async def readchunk(self) -> bytes: ... async def readline(self, size: int = -1) -> bytes: ... - def seek(self, pos: int, whence: int = _SEEK_SET) -> int: ... + def seek(self, pos: int, whence: int = ...) -> int: ... def seekable(self) -> bool: ... def tell(self) -> int: ... def write(self, data: Any) -> None: ... @@ -98,7 +96,7 @@ class AgnosticGridIn: async def write(self, data: Any) -> None: ... def writeable(self) -> bool: ... async def writelines(self, sequence: Iterable[Any]) -> None: ... - async def _exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any: ... + async def _exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> Any: ... async def set(self, name: str, value: Any) -> None: ... def __init__( self, @@ -108,7 +106,7 @@ class AgnosticGridIn: **kwargs: Any, ) -> None: ... async def __aenter__(self) -> AgnosticGridIn: ... - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ... + async def __aexit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None: ... def get_io_loop(self) -> Any: ... class AgnosticGridFSBucket: @@ -172,7 +170,7 @@ class AgnosticGridFSBucket: self, database: AgnosticDatabase, bucket_name: str = "fs", - chunk_size_bytes: int = DEFAULT_CHUNK_SIZE, + chunk_size_bytes: int = ..., write_concern: Optional[WriteConcern] = None, read_preference: Optional[_ServerMode] = None, collection: Optional[AgnosticCollection] = None, diff --git a/pyproject.toml b/pyproject.toml index 35f8fc49..c1508f87 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -129,6 +129,7 @@ select = [ "G", # flake8-logging-format "PGH", # pygrep-hooks "PIE", # flake8-pie + "PYI", # flake8-pyi "PL", # pylint "PT", # flake8-pytest-style "PTH", # flake8-use-pathlib @@ -152,6 +153,7 @@ ignore = [ "ARG002", # Unused method argument: "S101", # Use of `assert` detected "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` + "PYI034", # `__aenter__` methods in classes like `AgnosticGridIn` usually return `self` at runtime ] unfixable = [ "RUF100", # Unused noqa @@ -167,7 +169,7 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?)|dummy.*)$" "test/*.py" = ["PT009", "ARG", "E402", "PT027", "UP031", "B904", "C405", "SIM", "PLR", "PTH", "B018", "C4", "S", "E501", "T201", "E731", "F841", "F811", - "B011", "PT015", "E721"] + "B011", "PT015", "E721", "PYI"] "synchro/__init__.py" = ["F403", "B904", "F401"] "doc/*.py" = [ "T201", "PTH"] "motor/docstrings.py" = [ "E501", ] diff --git a/tox.ini b/tox.ini index 38d60f2d..447ff323 100644 --- a/tox.ini +++ b/tox.ini @@ -161,7 +161,7 @@ description = run mypy to typecheck extras = test deps = - mypy==1.6.0 + mypy==1.7.0 typing_extensions setuptools==68.0.0 setenv =