-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46812 - Distribute Type Hints #167
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
DOCSP-46812 - Distribute Type Hints #167
Conversation
✅ Deploy Preview for docs-pymongo ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
906a49d
to
0da250f
Compare
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.
A couple things:
source/connect/mongoclient.txt
Outdated
- ``bson.raw_bson.RawBSONDocument``. | ||
- A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``. | ||
If you specify ``bson.son.SON`` as the document class, you must also specify types | ||
for the key and value. | ||
- A subclass of the ``TypedDict`` type. To pass a ``TypedDict`` subclass for this | ||
parameter, you must also include the class in a type hint for your ``MongoClient`` | ||
object, as shown in the following example: |
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.
- ``bson.raw_bson.RawBSONDocument``. | |
- A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``. | |
If you specify ``bson.son.SON`` as the document class, you must also specify types | |
for the key and value. | |
- A subclass of the ``TypedDict`` type. To pass a ``TypedDict`` subclass for this | |
parameter, you must also include the class in a type hint for your ``MongoClient`` | |
object, as shown in the following example: | |
- ``bson.raw_bson.RawBSONDocument`` | |
- Subclasses of the ``collections.abc.Mapping`` type, such as ``bson.son.SON`` | |
If you specify ``bson.son.SON`` as the document class, you must also specify types | |
for the key and value. | |
- A subclass of the ``TypedDict`` type | |
To pass a ``TypedDict`` subclass to this parameter, you must also include the class in a type hint for your ``MongoClient`` | |
object, as shown in the following example: |
I think this would also benefit from an example of specifying the key and value types of a bson.son.SON
instance.
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.
I think we need the period at the end of the first item.
Makes more sense to me to say 'a subclass' than 'subclasses,' because you can only pass one type.
I'll add the SON example.
The ``TypedDict`` class is in the ``typing`` module, which | ||
is available only in Python 3.8 and later. To use the ``TypedDict`` class in | ||
earlier versions of Python, install the ``typing_extensions`` package. |
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.
Are there links available for TypeDict
and the typing_extensions
package?
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.
yeah, i went back and forth on including them. i'll add if you think they're helpful
If you use the generic form of the ``MongoClient`` class, your type checker might | ||
show an error similar to the following: |
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.
I'm a little weak on Python, but I think this is more specific:
If you use the generic form of the ``MongoClient`` class, your type checker might | |
show an error similar to the following: | |
If you do not specify a type when you instantiate the ``MongoClient`` class, your type checker might | |
show an error similar to the following: |
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.
I agree about keeping 'generic' as the term for 'type that accepts another type as an argument'
source/write/insert.txt
Outdated
As discussed above, if you don't specify the ``_id`` field, {+driver-short+} automatically | ||
inserts it into the document. | ||
You can retrieve the value of the ``_id`` field at runtime, but if you use MyPy or another | ||
tool to perform static type-checking, it won't find the ``_id`` field in your class and | ||
will show an error similar to the following: |
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.
I don't think many people are reading through the troubleshooting section, so we I think we should treat each section as though it will be read without broader context.
As discussed above, if you don't specify the ``_id`` field, {+driver-short+} automatically | |
inserts it into the document. | |
You can retrieve the value of the ``_id`` field at runtime, but if you use MyPy or another | |
tool to perform static type-checking, it won't find the ``_id`` field in your class and | |
will show an error similar to the following: | |
If you don't specify the ``_id`` field, {+driver-short+} automatically | |
inserts it into the document. | |
You can retrieve the value of the ``_id`` field at runtime, but if you use MyPy or another | |
tool to perform static type-checking, it won't find the ``_id`` field in your class and | |
will show an error similar to the following: |
source/write/insert.txt
Outdated
The ``NotRequired`` class is available only in Python 3.11 and later. | ||
To use ``NotRequired`` in earlier versions of Python, install the ``typing_extensions`` | ||
package instead. |
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.
Link to NotRequired
class and typing_extensions
package.
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
source/connect/mongoclient.txt
Outdated
|
||
If you specify ``bson.son.SON`` as the document class, you must also specify types | ||
for the key and value. | ||
- ``bson.raw_bson.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 this have an accompanying brief explanation of what a RawBSONDocument
is?
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.
From the docstring: "a representation of a BSON document that
provides access to the underlying raw BSON bytes. Only when a field is
accessed or modified within the document does RawBSONDocument decode
its bytes." Or maybe just a link?
source/run-command.txt
Outdated
of a specific class. To specify this class, construct a ``CodecOptions`` object and pass | ||
the class name. The class can be one of the following types: | ||
|
||
- ``bson.raw_bson.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.
Same question here.
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.
Sorry, which question?
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 link to or describe what RawBSONDocument
is here?
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.
ah, yep!
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.
I'll approve and resolve comments once you've replied.
source/connect/mongoclient.txt
Outdated
|
||
If you specify ``bson.son.SON`` as the document class, you must also specify types | ||
for the key and value. | ||
- ``bson.raw_bson.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.
From the docstring: "a representation of a BSON document that
provides access to the underlying raw BSON bytes. Only when a field is
accessed or modified within the document does RawBSONDocument decode
its bytes." Or maybe just a link?
source/connect/mongoclient.txt
Outdated
- ``bson.raw_bson.RawBSONDocument``. | ||
- A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``. | ||
If you specify ``bson.son.SON`` as the document class, you must also specify types | ||
for the key and value, as shown in the following example: |
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.
I just tried this with SON
without further subtypes. It worked fine. Maybe change "must" to "may".
c4 = MongoClient(document_class=SON)
clxn4 = c4["db"]["clxn"]
clxn4.find_one({})
Out[27]: SON([('_id', ObjectId('67b8ebf181dbbd497b835248')), ('i', 0)])
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.
Do the old docs need to be updated too, or did I misinterpret?
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.
The subtypes are still required depending on how strict the user has their type checking settings. I don't think we want to update either docs.
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.
@blink1073 Is it most accurate to say 'you may need to specify types' ?
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.
Yes I think that is fair
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 remove all references to bson.son.SON
here? Its use case for ordered Python dictionaries is no longe relevant now that OrderedDict
exists. Default dict
has also been ordered since Python 3.7.
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.
Updated to OrderedDict
and removed other references to SON
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.
A few comments, foremost is the question of why to distribute type hint discussion as opposed to having its own page.
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
0eae39a
to
301d38a
Compare
source/run-command.txt
Outdated
of a specific class. To specify this class, construct a ``CodecOptions`` object and pass | ||
the class name. The class can be one of the following types: | ||
|
||
- ``bson.raw_bson.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 link to or describe what RawBSONDocument
is here?
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.
still lgtm
source/connect/mongoclient.txt
Outdated
- ``bson.raw_bson.RawBSONDocument``. | ||
- A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``. | ||
If you specify ``bson.son.SON`` as the document class, you must also specify types | ||
for the key and value, as shown in the following example: |
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 remove all references to bson.son.SON
here? Its use case for ordered Python dictionaries is no longe relevant now that OrderedDict
exists. Default dict
has also been ordered since Python 3.7.
can specify the custom type as a type hint for your ``MongoClient`` object. This | ||
provides more accurate type information than the generic ``Dict[str, Any]`` type. | ||
|
||
The following example shows how to specify the ``Movie`` type as a type hint for a |
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 add the definition of Movie
here for clarity.
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.
happy to!
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.
Still missing the Movie
definition here.
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.
whoops
can specify the custom type as a type hint for your ``MongoClient`` object. This | ||
provides more accurate type information than the generic ``Dict[str, Any]`` type. | ||
|
||
The following example shows how to specify the ``Movie`` type as a type hint for a |
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.
Still missing the Movie
definition here.
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-v4.10 v4.10
# Navigate to the new working tree
cd .worktrees/backport-v4.10
# Create a new branch
git switch --create backport-167-to-v4.10
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 c4f389516721cd7d99b931e4eca342b99c0f8840
# Push it to GitHub
git push --set-upstream origin backport-167-to-v4.10
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-v4.10 Then, create a pull request where the |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-v4.9 v4.9
# Navigate to the new working tree
cd .worktrees/backport-v4.9
# Create a new branch
git switch --create backport-167-to-v4.9
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 c4f389516721cd7d99b931e4eca342b99c0f8840
# Push it to GitHub
git push --set-upstream origin backport-167-to-v4.9
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-v4.9 Then, create a pull request where the |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-v4.8 v4.8
# Navigate to the new working tree
cd .worktrees/backport-v4.8
# Create a new branch
git switch --create backport-167-to-v4.8
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 c4f389516721cd7d99b931e4eca342b99c0f8840
# Push it to GitHub
git push --set-upstream origin backport-167-to-v4.8
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-v4.8 Then, create a pull request where the |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-v4.7 v4.7
# Navigate to the new working tree
cd .worktrees/backport-v4.7
# Create a new branch
git switch --create backport-167-to-v4.7
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 c4f389516721cd7d99b931e4eca342b99c0f8840
# Push it to GitHub
git push --set-upstream origin backport-167-to-v4.7
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-v4.7 Then, create a pull request where the |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-v4.11 v4.11
# Navigate to the new working tree
cd .worktrees/backport-v4.11
# Create a new branch
git switch --create backport-167-to-v4.11
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 c4f389516721cd7d99b931e4eca342b99c0f8840
# Push it to GitHub
git push --set-upstream origin backport-167-to-v4.11
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-v4.11 Then, create a pull request where the |
(cherry picked from commit c4f3895)
Continuation of #156 (already approved). This PR moves content from the old Type Hints page to the relevant subject pages
Pull Request Info
PR Reviewing Guidelines
JIRA - https://jira.mongodb.org/browse/DOCSP-46812
Staging Links
Self-Review Checklist