Skip to content

PYTHON-3568 Intellisense highlights multiple PyMongo methods because of CodecOptions #1139

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 16 commits into from
Jan 25, 2023

Conversation

blink1073
Copy link
Member

No description provided.

@@ -116,7 +117,7 @@ def __init__(
database: "Database[_DocumentType]",
name: str,
create: Optional[bool] = False,
codec_options: Optional[CodecOptions] = None,
codec_options: Optional["CodecOptions[Mapping[str, Any]]"] = None,
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to use a generic TypeVar for CodecOptions instead of hardcoding Mapping[str, Any] to avoid this failure?:

Found 1 error in 1 file (checked 83 source files)
pymongo/mongo_client.py:2054: error: Argument "codec_options" to "get_database"
of "MongoClient" has incompatible type "CodecOptions[MutableMapping[str, Any]]";
expected "Optional[CodecOptions[Mapping[str, Any]]]"  [arg-type]
                codec_options=DEFAULT_CODEC_OPTIONS,
                              ^~~~~~~~~~~~~~~~~~~~~

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually we can use [_DocumentType] everywhere and satisfy pyright in strict mode.

@blink1073
Copy link
Member Author

The link check failure is expected until we merge this PR, since the file path in the link changes.

@blink1073 blink1073 requested a review from ShaneHarvey January 23, 2023 22:02
@@ -394,7 +395,7 @@ def database(self) -> "Database[_DocumentType]":

def with_options(
self,
codec_options: Optional[CodecOptions] = None,
codec_options: Optional["bson.CodecOptions[_DocumentTypeArg]"] = None,
Copy link
Member

@ShaneHarvey ShaneHarvey Jan 23, 2023

Choose a reason for hiding this comment

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

What caused the change to add "bson." here? We don't need to do the same for ReadConcern/WriteConcern/etc..

Copy link
Member Author

Choose a reason for hiding this comment

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

This was needed to satisfy sphinx:

/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/collection.py:docstring of pymongo.collection.Collection.with_options:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions
/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/database.py:docstring of pymongo.database.Database:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions
/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/database.py:docstring of pymongo.database.Database.create_collection:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions
/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/database.py:docstring of pymongo.database.Database.get_collection:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions
/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/database.py:docstring of pymongo.database.Database.with_options:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions
/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/mongo_client.py:docstring of pymongo.mongo_client.MongoClient.get_default_database:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions
/home/docs/checkouts/readthedocs.org/user_builds/pymongo/checkouts/1139/pymongo/mongo_client.py:docstring of pymongo.mongo_client.MongoClient.get_database:: WARNING: more than one target found for cross-reference 'CodecOptions': bson.codec_options.CodecOptions, bson.CodecOptions

Copy link
Member

Choose a reason for hiding this comment

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

That's odd. I don't see those warning locally:

 python -m sphinx -b html doc doc/_build/4.4.0.dev0
Running Sphinx v4.5.0
loading pickled environment... failed
failed: build environment version not current
loading intersphinx inventory from https://www.gevent.org/objects.inv...
loading intersphinx inventory from https://docs.python.org/3/objects.inv...
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 83 source files that are out of date
updating environment: [new config] 83 added, 0 changed, 0 removed
reading sources... [100%] tutorial                                                                                                                      
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] tutorial                                                                                                                       
generating indices... genindex py-modindex done
writing additional pages... search done
copying images... [100%] static/periodic-executor-refs.png                                                                                              
copying static files... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded.

The HTML pages are in doc/_build/4.4.0.dev0.

It would be great to avoid changing the type signature just because of a sphinx issue (bug?) if possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

We needed to switch to strings because on Python 3.7 we were getting: TypeError: 'type' object is not subscriptable

@@ -22,7 +22,8 @@ you get the error: "TypeError: 'type' object is not subscriptable".
import datetime
import abc
import enum
from typing import Tuple, Generic, Optional, Mapping, Any, TypeVar, Type, Dict, Iterable, Tuple, MutableMapping, Callable, Union
from typing import Tuple, Generic, Optional, Mapping, Any, TypeVar, Type, Dict, Iterable, Tuple, Callable, Union
from pymongo.typings import _DocumentType, _DocumentTypeArg
Copy link
Member

@ShaneHarvey ShaneHarvey Jan 23, 2023

Choose a reason for hiding this comment

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

It's odd to have bson import from pymongo, even if it's just for typing. Can we reverse this so that _DocumentType and _DocumentTypeArg are defined in the bson package?

Copy link
Member Author

Choose a reason for hiding this comment

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

I created bson/typings.py and shuffled things around

@blink1073 blink1073 requested a review from ShaneHarvey January 24, 2023 03:01
Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

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

One last question.

@@ -116,7 +117,7 @@ def __init__(
database: "Database[_DocumentType]",
name: str,
create: Optional[bool] = False,
codec_options: Optional[CodecOptions] = None,
codec_options: Optional["CodecOptions[_DocumentTypeArg]"] = None,
Copy link
Member

Choose a reason for hiding this comment

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

How come this one can be "CodecOptions" not "bson.CodecOptions"?

Copy link
Member Author

@blink1073 blink1073 Jan 24, 2023

Choose a reason for hiding this comment

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

I can't make heads or tails of it, other than the fact that this a constructor. I tried removing the docstring from with_options and it still failed.

@blink1073 blink1073 requested a review from ShaneHarvey January 24, 2023 22:24
@blink1073 blink1073 merged commit a3720d9 into mongodb:master Jan 25, 2023
@blink1073 blink1073 deleted the PYTHON-3568 branch January 25, 2023 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants