Skip to content

PYTHON-3737 Use __future__ annotations for forward reference type hints #1234

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 2 commits into from
Jun 15, 2023

Conversation

NoahStapp
Copy link
Contributor

The test/test_comment.py changes are due to __future__ annotations explicitly not evaluating all type annotations, causing the test to fail due to comparing the string "Optional[Any]" to the evaluated value Union[Any, None]. If we want to keep the original, evaluated comparison, we could explicitly evaluate the type annotation as described here: https://docs.python.org/3/library/typing.html#typing.get_type_hints.

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! I think the test file changes are fine.

@@ -96,7 +97,7 @@ class ChangeStream(Generic[_DocumentType]):
def __init__(
self,
target: Union[
"MongoClient[_DocumentType]", "Database[_DocumentType]", "Collection[_DocumentType]"
Copy link
Member

Choose a reason for hiding this comment

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

Is this allowed even though the MongoClient import is under if TYPE_CHECKING:?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, because it is converted to a string before it is interpreted.

Copy link
Member

Choose a reason for hiding this comment

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

Neat!

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.

LGTM with one ask

@@ -2000,10 +2001,10 @@ def get_default_database(
def get_database(
self,
name: Optional[str] = None,
codec_options: Optional["bson.CodecOptions[_DocumentTypeArg]"] = None,
codec_options: Optional[bson.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.

I notice that bson.CodecOptions doesn't render as a link: https://pymongo.readthedocs.io/en/latest/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient.get_database

Could you open a new ticket to address that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue being that bson.CodecOptions doesn't render as a type in the docs, right?

Copy link
Member

Choose a reason for hiding this comment

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

Right. Not immediately obvious why it doesn't render.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NoahStapp NoahStapp merged commit 601d1ec into mongodb:master Jun 15, 2023
@NoahStapp NoahStapp deleted the autotyping branch June 15, 2023 19:54
sleepyStick pushed a commit to sleepyStick/mongo-python-driver that referenced this pull request Jun 16, 2023
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.

3 participants