Skip to content

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

Merged
merged 11 commits into from
Mar 10, 2025

Conversation

mongoKart
Copy link
Collaborator

@mongoKart mongoKart commented Feb 6, 2025

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

  • aggregation/aggregation-tutorials/filtered-subset
  • connect/mongoclient
  • data-formats/bson
  • databases-collections
  • run-command
  • tools
  • write/bulk-write
  • write/insert
  • Self-Review Checklist

    • Is this free of any warnings or errors in the RST?
    • Did you run a spell-check?
    • Did you run a grammar-check?
    • Are all the links working?
    • Are the facets and meta keywords accurate?

    Copy link

    netlify bot commented Feb 6, 2025

    Deploy Preview for docs-pymongo ready!

    Name Link
    🔨 Latest commit 013530d
    🔍 Latest deploy log https://app.netlify.com/sites/docs-pymongo/deploys/67cf2a29641e410008b95d21
    😎 Deploy Preview https://deploy-preview-167--docs-pymongo.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    @mongoKart mongoKart force-pushed the docsp-46812-distribute-type-hints branch from 906a49d to 0da250f Compare February 6, 2025 16:54
    Copy link
    Collaborator

    @rachel-mack rachel-mack left a comment

    Choose a reason for hiding this comment

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

    A couple things:

    Comment on lines 137 to 150
    - ``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:
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Suggested change
    - ``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.

    Copy link
    Collaborator Author

    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.

    Comment on lines 1 to 3
    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.
    Copy link
    Collaborator

    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?

    Copy link
    Collaborator Author

    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

    Comment on lines 4 to 5
    If you use the generic form of the ``MongoClient`` class, your type checker might
    show an error similar to the following:
    Copy link
    Collaborator

    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:

    Suggested change
    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:

    Copy link
    Collaborator Author

    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'

    Comment on lines 236 to 216
    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:
    Copy link
    Collaborator

    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.

    Suggested change
    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:

    Comment on lines 344 to 324
    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.
    Copy link
    Collaborator

    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.

    Copy link
    Collaborator

    @rachel-mack rachel-mack left a comment

    Choose a reason for hiding this comment

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

    LGTM


    If you specify ``bson.son.SON`` as the document class, you must also specify types
    for the key and value.
    - ``bson.raw_bson.RawBSONDocument``.

    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?

    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?

    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``.

    Choose a reason for hiding this comment

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

    Same question here.

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    Sorry, which question?

    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?

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    ah, yep!

    Copy link

    @caseyclements caseyclements left a 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.


    If you specify ``bson.son.SON`` as the document class, you must also specify types
    for the key and value.
    - ``bson.raw_bson.RawBSONDocument``.

    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?

    - ``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:

    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)])
    

    Copy link
    Collaborator Author

    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?

    Copy link
    Member

    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.

    Copy link
    Collaborator Author

    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' ?

    Copy link
    Member

    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

    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.

    Copy link
    Collaborator Author

    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

    Copy link

    @caseyclements caseyclements left a 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.

    Copy link

    @caseyclements caseyclements left a comment

    Choose a reason for hiding this comment

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

    LGTM

    @mongoKart mongoKart force-pushed the docsp-46812-distribute-type-hints branch from 0eae39a to 301d38a Compare March 5, 2025 20:56
    @mongoKart mongoKart requested review from NoahStapp and caseyclements and removed request for caseyclements March 5, 2025 20:56
    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``.

    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?

    Copy link

    @caseyclements caseyclements left a comment

    Choose a reason for hiding this comment

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

    still lgtm

    - ``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:

    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

    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.

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    happy to!

    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.

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    whoops

    @mongoKart mongoKart requested a review from NoahStapp March 6, 2025 22:49
    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

    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.

    @mongoKart mongoKart requested a review from NoahStapp March 10, 2025 18:09
    @mongoKart mongoKart merged commit c4f3895 into mongodb:master Mar 10, 2025
    10 of 11 checks passed
    Copy link

    The backport to v4.10 failed:

    The process '/usr/bin/git' failed with exit code 1
    

    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 base branch is v4.10 and the compare/head branch is backport-167-to-v4.10.

    Copy link

    The backport to v4.9 failed:

    The process '/usr/bin/git' failed with exit code 1
    

    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 base branch is v4.9 and the compare/head branch is backport-167-to-v4.9.

    Copy link

    The backport to v4.8 failed:

    The process '/usr/bin/git' failed with exit code 1
    

    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 base branch is v4.8 and the compare/head branch is backport-167-to-v4.8.

    Copy link

    The backport to v4.7 failed:

    The process '/usr/bin/git' failed with exit code 1
    

    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 base branch is v4.7 and the compare/head branch is backport-167-to-v4.7.

    Copy link

    The backport to v4.11 failed:

    The process '/usr/bin/git' failed with exit code 1
    

    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 base branch is v4.11 and the compare/head branch is backport-167-to-v4.11.

    @mongoKart mongoKart deleted the docsp-46812-distribute-type-hints branch March 10, 2025 20:05
    mongoKart added a commit that referenced this pull request Mar 10, 2025
    mongoKart added a commit that referenced this pull request Mar 10, 2025
    (cherry picked from commit c4f3895)
    (cherry picked from commit 56ba281)
    mongoKart added a commit that referenced this pull request Mar 10, 2025
    (cherry picked from commit c4f3895)
    (cherry picked from commit 56ba281)
    mongoKart added a commit that referenced this pull request Mar 10, 2025
    (cherry picked from commit c4f3895)
    (cherry picked from commit 56ba281)
    mongoKart added a commit to mongoKart/docs-pymongo that referenced this pull request Mar 10, 2025
    (cherry picked from commit c4f3895)
    (cherry picked from commit 56ba281)
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    5 participants