Skip to content

Commit 9f67111

Browse files
authored
[Cosmos] Fixed incorrect ID type error (#11798)
* Fixed incorrect ID type error * Fix pylint
1 parent 0ec1601 commit 9f67111

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.0.1 (Unreleased)
2+
3+
- Fixed error raised when a non string ID is used in an item. It now raises TypeError rather than AttributeError. Issue 11793 - thank you @Rabbit994.
4+
5+
16
## 4.0.0 (2020-05-20)
27

38
- Stable release.

sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from urllib3.util.retry import Retry
3232
from azure.core.paging import ItemPaged # type: ignore
3333
from azure.core import PipelineClient # type: ignore
34+
from azure.core.exceptions import raise_with_traceback # type: ignore
3435
from azure.core.pipeline.policies import ( # type: ignore
3536
HTTPPolicy,
3637
ContentDecodePolicy,
@@ -2480,11 +2481,14 @@ def __CheckAndUnifyQueryFormat(self, query_body):
24802481
def __ValidateResource(resource):
24812482
id_ = resource.get("id")
24822483
if id_:
2483-
if id_.find("/") != -1 or id_.find("\\") != -1 or id_.find("?") != -1 or id_.find("#") != -1:
2484-
raise ValueError("Id contains illegal chars.")
2485-
2486-
if id_[-1] == " ":
2487-
raise ValueError("Id ends with a space.")
2484+
try:
2485+
if id_.find("/") != -1 or id_.find("\\") != -1 or id_.find("?") != -1 or id_.find("#") != -1:
2486+
raise ValueError("Id contains illegal chars.")
2487+
2488+
if id_[-1] == " ":
2489+
raise ValueError("Id ends with a space.")
2490+
except AttributeError:
2491+
raise_with_traceback(TypeError, message="Id type must be a string.")
24882492

24892493
# Adds the partition key to options
24902494
def _AddPartitionKey(self, collection_link, document, options):

sdk/cosmos/azure-cosmos/azure/cosmos/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
2121

22-
VERSION = "4.0.0"
22+
VERSION = "4.0.1"

sdk/cosmos/azure-cosmos/samples/access_cosmos_with_resource_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# https://docs.microsoft.com/azure/cosmos-db/create-sql-api-python#create-a-database-account
1515
#
1616
# 2. Microsoft Azure Cosmos
17-
# pip install azure-cosmos==4.0.0
17+
# pip install azure-cosmos>=4.0.0
1818
# ----------------------------------------------------------------------------------------------------------
1919
# Sample - how to get and use resource token that allows restricted access to data
2020
# ----------------------------------------------------------------------------------------------------------

sdk/cosmos/azure-cosmos/test/test_crud.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,11 @@ def test_document_upsert(self):
939939
self.assertEqual(created_document['id'],
940940
document_definition['id'])
941941

942+
# test error for non-string id
943+
with pytest.raises(TypeError):
944+
document_definition['id'] = 7
945+
created_collection.upsert_item(body=document_definition)
946+
942947
# read documents after creation and verify updated count
943948
documents = list(created_collection.read_all_items())
944949
self.assertEqual(

0 commit comments

Comments
 (0)