Skip to content

PYTHON-3821 use overload pattern for _DocumentType #1352

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 3 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
52 changes: 46 additions & 6 deletions bson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,9 +1106,21 @@ def _decode_all(
_decode_all = _cbson._decode_all # noqa: F811


@overload
def decode_all(data: "_ReadableBuffer", codec_options: None = None) -> "List[Dict[str, Any]]":
...


@overload
def decode_all(
data: "_ReadableBuffer", codec_options: "Optional[CodecOptions[_DocumentType]]" = None
data: "_ReadableBuffer", codec_options: "CodecOptions[_DocumentType]"
) -> "List[_DocumentType]":
...


def decode_all(
data: "_ReadableBuffer", codec_options: "Optional[CodecOptions[_DocumentType]]" = None
) -> "Union[List[Dict[str, Any]], List[_DocumentType]]":
"""Decode BSON data to multiple documents.

`data` must be a bytes-like object implementing the buffer protocol that
Expand All @@ -1131,11 +1143,13 @@ def decode_all(
Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with
`codec_options`.
"""
opts = codec_options or DEFAULT_CODEC_OPTIONS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was wrong (according to mypy) with this code? I find the new code less readable since it calls _decode_all in multiple places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting the following mypy error:

typecheck-mypy: commands[0]> mypy --install-types --non-interactive bson gridfs tools pymongo
bson/__init__.py: note: In function "decode_all":
bson/__init__.py:1150: error: Cannot infer type argument 1 of "_decode_all"  [misc]
        return _decode_all(data, opts)
               ^~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 85 source files)
typecheck-mypy: exit 1 (2.96 seconds) /Users/iris.ho/GitHub/mongo-python-driver> mypy --install-types --non-interactive bson gridfs tools pymongo pid=40888
.pkg: _exit> python /Users/iris.ho/Library/Python/3.9/lib/python/site-packages/pyproject_api/_backend.py True setuptools.build_meta
  typecheck-mypy: FAIL code 1 (10.28=setup[7.32]+cmd[2.96] seconds)
  evaluation failed :( (10.35 seconds)

if not isinstance(opts, CodecOptions):
if codec_options is None:
return _decode_all(data, DEFAULT_CODEC_OPTIONS)

if not isinstance(codec_options, CodecOptions):
raise _CODEC_OPTIONS_TYPE_ERROR

return _decode_all(data, opts) # type:ignore[arg-type]
return _decode_all(data, codec_options)


def _decode_selective(rawdoc: Any, fields: Any, codec_options: Any) -> Mapping[Any, Any]:
Expand Down Expand Up @@ -1242,9 +1256,21 @@ def _decode_all_selective(data: Any, codec_options: CodecOptions, fields: Any) -
]


@overload
def decode_iter(data: bytes, codec_options: None = None) -> "Iterator[Dict[str, Any]]":
...


@overload
def decode_iter(
data: bytes, codec_options: "Optional[CodecOptions[_DocumentType]]" = None
data: bytes, codec_options: "CodecOptions[_DocumentType]"
) -> "Iterator[_DocumentType]":
...


def decode_iter(
data: bytes, codec_options: "Optional[CodecOptions[_DocumentType]]" = None
) -> "Union[Iterator[Dict[str, Any]], Iterator[_DocumentType]]":
"""Decode BSON data to multiple documents as a generator.

Works similarly to the decode_all function, but yields one document at a
Expand Down Expand Up @@ -1278,9 +1304,23 @@ def decode_iter(
yield _bson_to_dict(elements, opts)


@overload
def decode_file_iter(
file_obj: Union[BinaryIO, IO], codec_options: "Optional[CodecOptions[_DocumentType]]" = None
file_obj: Union[BinaryIO, IO], codec_options: None = None
) -> "Iterator[Dict[str, Any]]":
...


@overload
def decode_file_iter(
file_obj: Union[BinaryIO, IO], codec_options: "CodecOptions[_DocumentType]"
) -> "Iterator[_DocumentType]":
...


def decode_file_iter(
file_obj: Union[BinaryIO, IO], codec_options: "Optional[CodecOptions[_DocumentType]]" = None
) -> "Union[Iterator[Dict[str, Any]], Iterator[_DocumentType]]":
"""Decode bson data from a file to multiple documents as a generator.

Works similarly to the decode_all function, but reads from the file object
Expand Down
18 changes: 0 additions & 18 deletions pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,24 +427,6 @@ def database(self) -> Database[_DocumentType]:
"""
return self.__database

# @overload
# def with_options(
# self,
# codec_options: None = None,
# read_preference: Optional[_ServerMode] = None,
# write_concern: Optional[WriteConcern] = None,
# read_concern: Optional[ReadConcern] = None,
# ) -> Collection[Dict[str, Any]]: ...

# @overload
# def with_options(
# self,
# codec_options: bson.CodecOptions[_DocumentType],
# read_preference: Optional[_ServerMode] = None,
# write_concern: Optional[WriteConcern] = None,
# read_concern: Optional[ReadConcern] = None,
# ) -> Collection[_DocumentType]: ...

def with_options(
self,
codec_options: Optional[bson.CodecOptions[_DocumentTypeArg]] = None,
Expand Down
25 changes: 25 additions & 0 deletions test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ def foo(self):
rt_document3 = decode(bsonbytes2, codec_options=codec_options2)
assert rt_document3.raw

def test_bson_decode_no_codec_option(self) -> None:
doc = decode_all(encode({"a": 1}))
assert doc
doc[0]["a"] = 2

def test_bson_decode_all(self) -> None:
doc = {"_id": 1}
bsonbytes = encode(doc)
Expand All @@ -266,6 +271,15 @@ def foo(self):
rt_documents3 = decode_all(bsonbytes3, codec_options3)
assert rt_documents3[0].raw

def test_bson_decode_all_no_codec_option(self) -> None:
docs = decode_all(b"")
docs.append({"new": 1})

docs = decode_all(encode({"a": 1}))
assert docs
docs[0]["a"] = 2
docs.append({"new": 1})

def test_bson_decode_iter(self) -> None:
doc = {"_id": 1}
bsonbytes = encode(doc)
Expand All @@ -290,6 +304,11 @@ def foo(self):
rt_documents3 = decode_iter(bsonbytes3, codec_options3)
assert next(rt_documents3).raw

def test_bson_decode_iter_no_codec_option(self) -> None:
doc = next(decode_iter(encode({"a": 1})))
assert doc
doc["a"] = 2

def make_tempfile(self, content: bytes) -> Any:
fileobj = tempfile.TemporaryFile()
fileobj.write(content)
Expand Down Expand Up @@ -324,6 +343,12 @@ def foo(self):
rt_documents3 = decode_file_iter(fileobj3, codec_options3)
assert next(rt_documents3).raw

def test_bson_decode_file_iter_none_codec_option(self) -> None:
fileobj = self.make_tempfile(encode({"new": 1}))
doc = next(decode_file_iter(fileobj))
assert doc
doc["a"] = 2


class TestDocumentType(unittest.TestCase):
@only_type_check
Expand Down