Skip to content

Commit 13be40c

Browse files
authored
[Cosmos] make PartitionKeyType public, mark excluded locations as sequence, make InternalException private (#43235)
* make PartitionKeyType public, mark excluded locations as sequence * Update http_constants.py * remove re-declaring of partitionkeytype * unused imports * rename Internal exception, make private * missing bits
1 parent 19e06dc commit 13be40c

15 files changed

+106
-118
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
_Undefined,
7878
_Empty,
7979
_PartitionKeyKind,
80-
_PartitionKeyType,
80+
PartitionKeyType,
8181
_SequentialPartitionKeyType,
8282
_return_undefined_or_empty_partition_key,
8383
)
@@ -1054,7 +1054,7 @@ def DeletePermission(
10541054
def read_items(
10551055
self,
10561056
collection_link: str,
1057-
items: Sequence[Tuple[str, _PartitionKeyType]],
1057+
items: Sequence[Tuple[str, PartitionKeyType]],
10581058
options: Optional[Mapping[str, Any]] = None,
10591059
*,
10601060
executor: Optional[ThreadPoolExecutor] = None,
@@ -1119,7 +1119,7 @@ def QueryItems(
11191119
database_or_container_link: str,
11201120
query: Optional[Union[str, Dict[str, Any]]],
11211121
options: Optional[Mapping[str, Any]] = None,
1122-
partition_key: Optional[_PartitionKeyType] = None,
1122+
partition_key: Optional[PartitionKeyType] = None,
11231123
response_hook: Optional[Callable[[Mapping[str, Any], Dict[str, Any]], None]] = None,
11241124
**kwargs: Any
11251125
) -> ItemPaged[Dict[str, Any]]:
@@ -3330,7 +3330,8 @@ def __GetBodiesFromQueryResult(result: Dict[str, Any]) -> List[Dict[str, Any]]:
33303330

33313331
return __GetBodiesFromQueryResult(result), last_response_headers
33323332

3333-
def _GetQueryPlanThroughGateway(self, query: str, resource_link: str, excluded_locations: Optional[str] = None,
3333+
def _GetQueryPlanThroughGateway(self, query: str, resource_link: str,
3334+
excluded_locations: Optional[Sequence[str]] = None,
33343335
**kwargs: Any) -> List[Dict[str, Any]]:
33353336
supported_query_features = (documents._QueryFeature.Aggregate + "," +
33363337
documents._QueryFeature.CompositeAggregate + "," +

sdk/cosmos/azure-cosmos/azure/cosmos/_partition_health_tracker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ def get_unhealthy_locations(
191191
request: RequestObject,
192192
pk_range_wrapper: PartitionKeyRangeWrapper
193193
) -> List[str]:
194-
excluded_locations = []
194+
unhealthy_locations = []
195195
if pk_range_wrapper in self.pk_range_wrapper_to_health_info:
196196
for location, partition_health_info in self.pk_range_wrapper_to_health_info[pk_range_wrapper].items():
197197
if (partition_health_info.unavailability_info and
198198
not (request.healthy_tentative_location and request.healthy_tentative_location == location)):
199199
health_status = partition_health_info.unavailability_info[HEALTH_STATUS]
200200
if health_status in (UNHEALTHY_TENTATIVE, UNHEALTHY) :
201-
excluded_locations.append(location)
202-
return excluded_locations
201+
unhealthy_locations.append(location)
202+
return unhealthy_locations
203203

204204
def add_failure(
205205
self,

sdk/cosmos/azure-cosmos/azure/cosmos/_query_builder.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from azure.cosmos.partition_key import _Undefined, _Empty, NonePartitionKeyValue
2727
if TYPE_CHECKING:
28-
from azure.cosmos._cosmos_client_connection import _PartitionKeyType
28+
from azure.cosmos._cosmos_client_connection import PartitionKeyType
2929

3030

3131
class _QueryBuilder:
@@ -49,7 +49,7 @@ def _get_field_expression(path: str) -> str:
4949

5050
@staticmethod
5151
def is_id_partition_key_query(
52-
items: Sequence[Tuple[str, "_PartitionKeyType"]],
52+
items: Sequence[Tuple[str, "PartitionKeyType"]],
5353
partition_key_definition: Dict[str, Any]
5454
) -> bool:
5555
"""Check if we can use the optimized ID IN query.
@@ -71,7 +71,7 @@ def is_id_partition_key_query(
7171

7272
@staticmethod
7373
def is_single_logical_partition_query(
74-
items: Sequence[Tuple[str, "_PartitionKeyType"]]
74+
items: Sequence[Tuple[str, "PartitionKeyType"]]
7575
) -> bool:
7676
"""Check if all items in a chunk belong to the same logical partition.
7777
@@ -88,7 +88,7 @@ def is_single_logical_partition_query(
8888

8989
@staticmethod
9090
def build_pk_and_id_in_query(
91-
items: Sequence[Tuple[str, "_PartitionKeyType"]],
91+
items: Sequence[Tuple[str, "PartitionKeyType"]],
9292
partition_key_definition: Dict[str, Any]
9393
) -> Dict[str, Any]:
9494
"""Build a query for items in a single logical partition using an IN clause for IDs.
@@ -114,7 +114,7 @@ def build_pk_and_id_in_query(
114114
return {"query": query_text, "parameters": parameters}
115115

116116
@staticmethod
117-
def build_id_in_query(items: Sequence[Tuple[str, "_PartitionKeyType"]]) -> Dict[str, Any]:
117+
def build_id_in_query(items: Sequence[Tuple[str, "PartitionKeyType"]]) -> Dict[str, Any]:
118118
"""Build optimized query using ID IN clause when ID equals partition key.
119119
120120
:param Sequence[tuple[str, any]] items: The list of items to build the query for.
@@ -131,7 +131,7 @@ def build_id_in_query(items: Sequence[Tuple[str, "_PartitionKeyType"]]) -> Dict[
131131

132132
@staticmethod
133133
def build_parameterized_query_for_items(
134-
items_by_partition: Dict[str, Sequence[Tuple[str, "_PartitionKeyType"]]],
134+
items_by_partition: Dict[str, Sequence[Tuple[str, "PartitionKeyType"]]],
135135
partition_key_definition: Dict[str, Any]
136136
) -> Dict[str, Any]:
137137
"""Builds a parameterized SQL query for reading multiple items.

sdk/cosmos/azure-cosmos/azure/cosmos/_read_items_helper.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from azure.cosmos.partition_key import _get_partition_key_from_partition_key_definition
3232
from azure.cosmos import CosmosList
3333
if TYPE_CHECKING:
34-
from azure.cosmos._cosmos_client_connection import _PartitionKeyType , CosmosClientConnection
34+
from azure.cosmos._cosmos_client_connection import PartitionKeyType , CosmosClientConnection
3535

3636

3737

@@ -43,7 +43,7 @@ def __init__(
4343
self,
4444
client: 'CosmosClientConnection',
4545
collection_link: str,
46-
items: Sequence[Tuple[str, "_PartitionKeyType"]],
46+
items: Sequence[Tuple[str, "PartitionKeyType"]],
4747
options: Optional[Mapping[str, Any]],
4848
partition_key_definition: Dict[str, Any],
4949
*,
@@ -87,7 +87,7 @@ def read_items(self) -> CosmosList:
8787
def _execute_with_executor(
8888
self,
8989
executor: ThreadPoolExecutor,
90-
query_chunks: List[Dict[str, List[Tuple[int, str, "_PartitionKeyType"]]]]
90+
query_chunks: List[Dict[str, List[Tuple[int, str, "PartitionKeyType"]]]]
9191
) -> CosmosList:
9292
"""Execute the queries using the provided executor with improved error handling.
9393
@@ -141,7 +141,7 @@ def _execute_with_executor(
141141

142142
return cosmos_list
143143

144-
def _partition_items_by_range(self) -> Dict[str, List[Tuple[int, str, "_PartitionKeyType"]]]:
144+
def _partition_items_by_range(self) -> Dict[str, List[Tuple[int, str, "PartitionKeyType"]]]:
145145
# pylint: disable=protected-access
146146
"""Groups items by their partition key range ID efficiently while preserving original order.
147147
@@ -150,10 +150,10 @@ def _partition_items_by_range(self) -> Dict[str, List[Tuple[int, str, "_Partitio
150150
"""
151151
collection_rid = _base.GetResourceIdOrFullNameFromLink(self.collection_link)
152152
partition_key = _get_partition_key_from_partition_key_definition(self.partition_key_definition)
153-
items_by_partition: Dict[str, List[Tuple[int, str, "_PartitionKeyType"]]] = {}
153+
items_by_partition: Dict[str, List[Tuple[int, str, "PartitionKeyType"]]] = {}
154154

155155
# Group items by logical partition key first to avoid redundant range lookups
156-
items_by_pk_value: Dict[Any, List[Tuple[int, str, "_PartitionKeyType"]]] = {}
156+
items_by_pk_value: Dict[Any, List[Tuple[int, str, "PartitionKeyType"]]] = {}
157157
for idx, (item_id, pk_value) in enumerate(self.items):
158158
# Convert list to tuple to use as a dictionary key, as lists are unhashable
159159
key = tuple(pk_value) if isinstance(pk_value, list) else pk_value
@@ -180,8 +180,8 @@ def _partition_items_by_range(self) -> Dict[str, List[Tuple[int, str, "_Partitio
180180

181181
def _create_query_chunks(
182182
self,
183-
items_by_partition: Dict[str, List[Tuple[int, str, "_PartitionKeyType"]]]
184-
) -> List[Dict[str, List[Tuple[int, str, "_PartitionKeyType"]]]]:
183+
items_by_partition: Dict[str, List[Tuple[int, str, "PartitionKeyType"]]]
184+
) -> List[Dict[str, List[Tuple[int, str, "PartitionKeyType"]]]]:
185185
"""Create query chunks for concurrency control while preserving original indices.
186186
187187
:param items_by_partition: A dictionary mapping partition key range IDs to lists of items with indices.
@@ -198,7 +198,7 @@ def _create_query_chunks(
198198
return query_chunks
199199

200200
def _execute_query_chunk_worker(
201-
self, partition_id: str, chunk_partition_items: Sequence[Tuple[int, str, "_PartitionKeyType"]]
201+
self, partition_id: str, chunk_partition_items: Sequence[Tuple[int, str, "PartitionKeyType"]]
202202
) -> Tuple[List[Tuple[int, Dict[str, Any]]], float]:
203203
"""Synchronous worker to build and execute a query for a chunk of items.
204204
@@ -231,7 +231,7 @@ def _execute_query_chunk_worker(
231231
def _execute_query(
232232
self,
233233
partition_id: str,
234-
items_for_query: Sequence[Tuple[str, "_PartitionKeyType"]],
234+
items_for_query: Sequence[Tuple[str, "PartitionKeyType"]],
235235
id_to_idx: Dict[str, int],
236236
request_kwargs: Dict[str, Any]
237237
) -> Tuple[List[Tuple[int, Any]], CaseInsensitiveDict]:
@@ -281,7 +281,7 @@ def local_response_hook(hook_headers, _):
281281
def _execute_point_read(
282282
self,
283283
item_id: str,
284-
pk_value: "_PartitionKeyType",
284+
pk_value: "PartitionKeyType",
285285
request_kwargs: Dict[str, Any]
286286
) -> Tuple[Optional[Any], CaseInsensitiveDict]:
287287
"""

sdk/cosmos/azure-cosmos/azure/cosmos/_retry_utility.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def Execute(client, global_endpoint_manager, function, *args, **kwargs): # pylin
137137
not result[0]['Offers'] and request.method == 'POST':
138138
# Grab the link used for getting throughput properties to add to message.
139139
link = json.loads(request.body)["parameters"][0]["value"]
140-
response = exceptions.InternalException(status_code=StatusCodes.NOT_FOUND,
141-
headers={HttpHeaders.SubStatus:
140+
response = exceptions._InternalCosmosException(status_code=StatusCodes.NOT_FOUND,
141+
headers={HttpHeaders.SubStatus:
142142
SubStatusCodes.THROUGHPUT_OFFER_NOT_FOUND})
143143
e_offer = exceptions.CosmosResourceNotFoundError(
144144
status_code=StatusCodes.NOT_FOUND,

0 commit comments

Comments
 (0)