-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-3454 Specifying a generic type for a collection does not correctly enforce type safety when inserting data #1081
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
Changes from all commits
92188dd
ab64c72
ea41432
6e21760
2a86839
d4ef6b8
3a063b8
8322387
f9ba0c7
1bb46a5
8139724
4c2c265
91f3666
6cfe733
671eea7
3ab850e
7c1dab2
3869d8d
327fe90
2d58a21
75e0dc5
e0d1d98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,22 +14,20 @@ | |
|
|
||
| """Test that each file in mypy_fails/ actually fails mypy, and test some | ||
| sample client code that uses PyMongo typings.""" | ||
|
|
||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List | ||
|
|
||
| try: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to keep this in a try/except so that |
||
| from typing import TypedDict # type: ignore[attr-defined] | ||
| from typing_extensions import TypedDict | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's revert these changes and go back to using typing.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need to keep using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can you explain? How was this fixed?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We added a new invocation in GitHub Actions in this PR to run this file without disabling several error checks.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see thanks. |
||
| # Not available in Python 3.7 | ||
| class Movie(TypedDict): # type: ignore[misc] | ||
| name: str | ||
| year: int | ||
|
|
||
| except ImportError: | ||
| TypeDict = None | ||
| TypedDict = None | ||
|
|
||
|
|
||
| try: | ||
|
|
@@ -304,6 +302,28 @@ def test_typeddict_document_type(self) -> None: | |
| assert retreived["year"] == 1 | ||
| assert retreived["name"] == "a" | ||
|
|
||
| @only_type_check | ||
| def test_typeddict_document_type_insertion(self) -> None: | ||
| client: MongoClient[Movie] = MongoClient() | ||
| coll = client.test.test | ||
| mov = {"name": "THX-1138", "year": 1971} | ||
| movie = Movie(name="THX-1138", year=1971) | ||
| coll.insert_one(mov) # type: ignore[arg-type] | ||
| coll.insert_one({"name": "THX-1138", "year": 1971}) # This will work because it is in-line. | ||
| coll.insert_one(movie) | ||
| coll.insert_many([mov]) # type: ignore[list-item] | ||
| coll.insert_many([movie]) | ||
| bad_mov = {"name": "THX-1138", "year": "WRONG TYPE"} | ||
| bad_movie = Movie(name="THX-1138", year="WRONG TYPE") # type: ignore[typeddict-item] | ||
| coll.insert_one(bad_mov) # type:ignore[arg-type] | ||
| coll.insert_one({"name": "THX-1138", "year": "WRONG TYPE"}) # type: ignore[typeddict-item] | ||
| coll.insert_one(bad_movie) | ||
| coll.insert_many([bad_mov]) # type: ignore[list-item] | ||
| coll.insert_many( | ||
| [{"name": "THX-1138", "year": "WRONG TYPE"}] # type: ignore[typeddict-item] | ||
| ) | ||
| coll.insert_many([bad_movie]) | ||
|
|
||
| @only_type_check | ||
| def test_raw_bson_document_type(self) -> None: | ||
| client = MongoClient(document_class=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.
We should add a mypy test for
insert_manytoo.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.
And bulkWrite InsertOne if possible?
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.