-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-3074 Add documentation for type hints #906
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
Conversation
except TypeError: | ||
if hasattr(value, "__origin__"): | ||
is_mapping = issubclass(value.__origin__, abc.MutableMapping) | ||
if not is_mapping and not issubclass(value, RawBSONDocument): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a proper test for this fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Client Document Type | ||
-------------------- | ||
|
||
:class:`~pymongo.mongo_client.MongoClient` is generic on the document type used to decode BSON documents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should mention that Client/Database/Collection are all generic too. For instance the TypedDict Movie example would probably make more sense at the collection level:
>>> from pymongo.collection import Collection
...
>>> client: MongoClient[Dict[str, Any]] = MongoClient()
>>> collection: Collection[Movie] = client.test.movies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one minor comment.
test/test_mypy.py
Outdated
@@ -309,6 +309,9 @@ def test_son_document_type(self) -> None: | |||
assert retreived is not None | |||
retreived["a"] = 1 | |||
|
|||
def test_son_document_type_runtime(self) -> None: | |||
client = MongoClient(document_class=SON[str, Any]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add connect=False to avoid starting threads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
doc/examples/type_hints.rst
Outdated
|
||
As of version 4.1, PyMongo ships with `type hints`_. | ||
|
||
With type hints, Python type checkers can easily find bugs before they reveal themselves in your code. If your IDE is configured to use type hints, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe put a link here for how to configure type hints in popular IDEs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
No description provided.