Skip to content

Commit 5bfecee

Browse files
authored
[Cosmos] refactor dict and list (#43244)
* refactor dict and list * fix CosmosDict, drop python 3.8 * fix cosmoslist * Update container.py * Update _container.py * pylint
1 parent de5bf8f commit 5bfecee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1308
-1348
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/_base.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import uuid
2929
import re
3030
import binascii
31-
from typing import Dict, Any, List, Mapping, Optional, Sequence, Union, Tuple, TYPE_CHECKING
31+
from typing import Any, Mapping, Optional, Sequence, Union, Tuple, TYPE_CHECKING
3232

3333
from urllib.parse import quote as urllib_quote
3434
from urllib.parse import unquote as urllib_unquote
@@ -79,7 +79,7 @@
7979
_VALID_COSMOS_RESOURCE = re.compile(r"^[^/\\#?\t\r\n]*$")
8080

8181

82-
def _get_match_headers(kwargs: Dict[str, Any]) -> Tuple[Optional[str], Optional[str]]:
82+
def _get_match_headers(kwargs: dict[str, Any]) -> Tuple[Optional[str], Optional[str]]:
8383
if_match = kwargs.pop('if_match', None)
8484
if_none_match = kwargs.pop('if_none_match', None)
8585
match_condition = kwargs.pop('match_condition', None)
@@ -104,7 +104,7 @@ def _get_match_headers(kwargs: Dict[str, Any]) -> Tuple[Optional[str], Optional[
104104
return if_match, if_none_match
105105

106106

107-
def build_options(kwargs: Dict[str, Any]) -> Dict[str, Any]:
107+
def build_options(kwargs: dict[str, Any]) -> dict[str, Any]:
108108
options = kwargs.pop('request_options', kwargs.pop('feed_options', {}))
109109
for key, value in _COMMON_OPTIONS.items():
110110
if key in kwargs:
@@ -128,7 +128,7 @@ def GetHeaders( # pylint: disable=too-many-statements,too-many-branches
128128
options: Mapping[str, Any],
129129
partition_key_range_id: Optional[str] = None,
130130
client_id: Optional[str] = None,
131-
) -> Dict[str, Any]:
131+
) -> dict[str, Any]:
132132
"""Gets HTTP request headers.
133133
134134
:param _cosmos_client_connection.CosmosClientConnection cosmos_client_connection:
@@ -712,7 +712,7 @@ def TrimBeginningAndEndingSlashes(path: str) -> str:
712712

713713

714714
# Parses the paths into a list of token each representing a property
715-
def ParsePaths(paths: List[str]) -> List[str]:
715+
def ParsePaths(paths: list[str]) -> list[str]:
716716
segmentSeparator = "/"
717717
tokens = []
718718
for path in paths:
@@ -791,7 +791,7 @@ def _validate_resource(resource: Mapping[str, Any]) -> None:
791791

792792

793793
def _stringify_auto_scale(offer: ThroughputProperties) -> str:
794-
auto_scale_params: Optional[Dict[str, Union[None, int, Dict[str, Any]]]] = None
794+
auto_scale_params: Optional[dict[str, Union[None, int, dict[str, Any]]]] = None
795795
max_throughput = offer.auto_scale_max_throughput
796796
increment_percent = offer.auto_scale_increment_percent
797797
auto_scale_params = {"maxThroughput": max_throughput}
@@ -801,7 +801,7 @@ def _stringify_auto_scale(offer: ThroughputProperties) -> str:
801801
return auto_scale_settings
802802

803803

804-
def _set_throughput_options(offer: Optional[Union[int, ThroughputProperties]], request_options: Dict[str, Any]) -> None:
804+
def _set_throughput_options(offer: Optional[Union[int, ThroughputProperties]], request_options: dict[str, Any]) -> None:
805805
if isinstance(offer, int):
806806
request_options["offerThroughput"] = offer
807807
elif offer is not None:
@@ -820,9 +820,9 @@ def _set_throughput_options(offer: Optional[Union[int, ThroughputProperties]], r
820820
raise TypeError("offer_throughput must be int or an instance of ThroughputProperties") from e
821821

822822

823-
def _deserialize_throughput(throughput: List[Dict[str, Dict[str, Any]]]) -> ThroughputProperties:
823+
def _deserialize_throughput(throughput: list[dict[str, dict[str, Any]]]) -> ThroughputProperties:
824824
properties = throughput[0]
825-
offer_autopilot: Optional[Dict[str, Any]] = properties['content'].get('offerAutopilotSettings')
825+
offer_autopilot: Optional[dict[str, Any]] = properties['content'].get('offerAutopilotSettings')
826826
if offer_autopilot and 'autoUpgradePolicy' in offer_autopilot:
827827
return ThroughputProperties(
828828
properties=properties,
@@ -842,7 +842,7 @@ def _deserialize_throughput(throughput: List[Dict[str, Dict[str, Any]]]) -> Thro
842842

843843
def _replace_throughput(
844844
throughput: Union[int, ThroughputProperties],
845-
new_throughput_properties: Dict[str, Any]
845+
new_throughput_properties: dict[str, Any]
846846
) -> None:
847847
if isinstance(throughput, int):
848848
new_throughput_properties["content"]["offerThroughput"] = throughput
@@ -873,15 +873,15 @@ def _internal_resourcetype(resource_type: str) -> str:
873873
return resource_type
874874

875875

876-
def _populate_batch_headers(current_headers: Dict[str, Any]) -> None:
876+
def _populate_batch_headers(current_headers: dict[str, Any]) -> None:
877877
current_headers[http_constants.HttpHeaders.IsBatchRequest] = True
878878
current_headers[http_constants.HttpHeaders.IsBatchAtomic] = True
879879
current_headers[http_constants.HttpHeaders.ShouldBatchContinueOnError] = False
880880

881881

882882
def _format_batch_operations(
883-
operations: Sequence[Union[Tuple[str, Tuple[Any, ...]], Tuple[str, Tuple[Any, ...], Dict[str, Any]]]]
884-
) -> List[Dict[str, Any]]:
883+
operations: Sequence[Union[Tuple[str, Tuple[Any, ...]], Tuple[str, Tuple[Any, ...], dict[str, Any]]]]
884+
) -> list[dict[str, Any]]:
885885
final_operations = []
886886
for index, batch_operation in enumerate(operations):
887887
try:
@@ -936,7 +936,7 @@ def _format_batch_operations(
936936
return final_operations
937937

938938

939-
def _build_properties_cache(properties: Dict[str, Any], container_link: str) -> Dict[str, Any]:
939+
def _build_properties_cache(properties: dict[str, Any], container_link: str) -> dict[str, Any]:
940940
return {
941941
"_self": properties.get("_self", None), "_rid": properties.get("_rid", None),
942942
"partitionKey": properties.get("partitionKey", None), "container_link": container_link

sdk/cosmos/azure-cosmos/azure/cosmos/_change_feed/aio/change_feed_fetcher.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import base64
2626
import json
2727
from abc import ABC, abstractmethod
28-
from typing import Dict, Any, List, Callable, Tuple, Awaitable, cast
28+
from typing import Any, Callable, Tuple, Awaitable, cast
2929

3030
from azure.cosmos import http_constants, exceptions
3131
from azure.cosmos._change_feed.change_feed_start_from import ChangeFeedStartFromType
@@ -38,7 +38,7 @@
3838
class ChangeFeedFetcher(ABC):
3939

4040
@abstractmethod
41-
async def fetch_next_block(self) -> List[Dict[str, Any]]:
41+
async def fetch_next_block(self) -> list[dict[str, Any]]:
4242
pass
4343

4444
class ChangeFeedFetcherV1(ChangeFeedFetcher):
@@ -51,8 +51,8 @@ def __init__(
5151
self,
5252
client,
5353
resource_link: str,
54-
feed_options: Dict[str, Any],
55-
fetch_function: Callable[[Dict[str, Any]], Awaitable[Tuple[List[Dict[str, Any]], Dict[str, Any]]]]
54+
feed_options: dict[str, Any],
55+
fetch_function: Callable[[dict[str, Any]], Awaitable[Tuple[list[dict[str, Any]], dict[str, Any]]]]
5656
) -> None:
5757

5858
self._client = client
@@ -66,7 +66,7 @@ def __init__(
6666
self._resource_link = resource_link
6767
self._fetch_function = fetch_function
6868

69-
async def fetch_next_block(self) -> List[Dict[str, Any]]:
69+
async def fetch_next_block(self) -> list[dict[str, Any]]:
7070
"""Returns a block of results.
7171
7272
:return: List of results.
@@ -77,7 +77,7 @@ async def callback():
7777

7878
return await _retry_utility_async.ExecuteAsync(self._client, self._client._global_endpoint_manager, callback)
7979

80-
async def fetch_change_feed_items(self) -> List[Dict[str, Any]]:
80+
async def fetch_change_feed_items(self) -> list[dict[str, Any]]:
8181
self._feed_options["changeFeedState"] = self._change_feed_state
8282

8383
self._change_feed_state.populate_feed_options(self._feed_options)
@@ -112,8 +112,8 @@ def __init__(
112112
self,
113113
client,
114114
resource_link: str,
115-
feed_options: Dict[str, Any],
116-
fetch_function: Callable[[Dict[str, Any]], Awaitable[Tuple[List[Dict[str, Any]], Dict[str, Any]]]]
115+
feed_options: dict[str, Any],
116+
fetch_function: Callable[[dict[str, Any]], Awaitable[Tuple[list[dict[str, Any]], dict[str, Any]]]]
117117
) -> None:
118118

119119
self._client = client
@@ -127,7 +127,7 @@ def __init__(
127127
self._resource_link = resource_link
128128
self._fetch_function = fetch_function
129129

130-
async def fetch_next_block(self) -> List[Dict[str, Any]]:
130+
async def fetch_next_block(self) -> list[dict[str, Any]]:
131131
"""Returns a block of results.
132132
133133
:return: List of results.
@@ -155,7 +155,7 @@ async def callback():
155155

156156
return await self.fetch_next_block()
157157

158-
async def fetch_change_feed_items(self) -> List[Dict[str, Any]]:
158+
async def fetch_change_feed_items(self) -> list[dict[str, Any]]:
159159
self._feed_options["changeFeedState"] = self._change_feed_state
160160

161161
self._change_feed_state.populate_feed_options(self._feed_options)

sdk/cosmos/azure-cosmos/azure/cosmos/_change_feed/aio/change_feed_iterable.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
"""Iterable change feed results in the Azure Cosmos database service.
2323
"""
24-
from typing import Dict, Any, Optional, Callable, Tuple, List, Awaitable, Union
24+
from typing import Any, Optional, Callable, Tuple, Awaitable, Union
2525

2626
from azure.core.async_paging import AsyncPageIterator
2727

@@ -40,8 +40,8 @@ class ChangeFeedIterable(AsyncPageIterator):
4040
def __init__(
4141
self,
4242
client,
43-
options: Dict[str, Any],
44-
fetch_function=Optional[Callable[[Dict[str, Any]], Awaitable[Tuple[List[Dict[str, Any]], Dict[str, Any]]]]],
43+
options: dict[str, Any],
44+
fetch_function=Optional[Callable[[dict[str, Any]], Awaitable[Tuple[list[dict[str, Any]], dict[str, Any]]]]],
4545
collection_link=Optional[str],
4646
continuation_token=Optional[str],
4747
) -> None:
@@ -92,8 +92,8 @@ def __init__(
9292

9393
async def _unpack(
9494
self,
95-
block: List[Dict[str, Any]]
96-
) -> Tuple[Optional[str], List[Dict[str, Any]]]:
95+
block: list[dict[str, Any]]
96+
) -> Tuple[Optional[str], list[dict[str, Any]]]:
9797
continuation: Optional[str] = None
9898
if self._client.last_response_headers:
9999
continuation = self._client.last_response_headers.get('etag')
@@ -102,7 +102,7 @@ async def _unpack(
102102
self._did_a_call_already = False
103103
return continuation, block
104104

105-
async def _fetch_next(self, *args) -> List[Dict[str, Any]]: # pylint: disable=unused-argument
105+
async def _fetch_next(self, *args) -> list[dict[str, Any]]: # pylint: disable=unused-argument
106106
"""Return a block of results with respecting retry policy.
107107
108108
:param Any args:
@@ -145,7 +145,7 @@ async def _initialize_change_feed_fetcher(self) -> None:
145145
self._fetch_function
146146
)
147147

148-
def _validate_change_feed_state_context(self, change_feed_state_context: Dict[str, Any]) -> None:
148+
def _validate_change_feed_state_context(self, change_feed_state_context: dict[str, Any]) -> None:
149149

150150
if change_feed_state_context.get("continuationPkRangeId") is not None:
151151
# if continuation token is in v1 format, throw exception if feed_range is set

sdk/cosmos/azure-cosmos/azure/cosmos/_change_feed/change_feed_fetcher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import base64
2626
import json
2727
from abc import ABC, abstractmethod
28-
from typing import Dict, Any, List, Callable, Tuple, cast
28+
from typing import Any, Callable, Tuple, cast
2929

3030
from azure.cosmos import _retry_utility, http_constants, exceptions
3131
from azure.cosmos._change_feed.change_feed_start_from import ChangeFeedStartFromType
@@ -50,8 +50,8 @@ def __init__(
5050
self,
5151
client,
5252
resource_link: str,
53-
feed_options: Dict[str, Any],
54-
fetch_function: Callable[[Dict[str, Any]], Tuple[List[Dict[str, Any]], Dict[str, Any]]]
53+
feed_options: dict[str, Any],
54+
fetch_function: Callable[[dict[str, Any]], Tuple[list[dict[str, Any]], dict[str, Any]]]
5555
) -> None:
5656

5757
self._client = client
@@ -65,7 +65,7 @@ def __init__(
6565
self._resource_link = resource_link
6666
self._fetch_function = fetch_function
6767

68-
def fetch_next_block(self) -> List[Dict[str, Any]]:
68+
def fetch_next_block(self) -> list[dict[str, Any]]:
6969
"""Returns a block of results.
7070
7171
:return: List of results.
@@ -76,7 +76,7 @@ def callback():
7676

7777
return _retry_utility.Execute(self._client, self._client._global_endpoint_manager, callback)
7878

79-
def fetch_change_feed_items(self) -> List[Dict[str, Any]]:
79+
def fetch_change_feed_items(self) -> list[dict[str, Any]]:
8080
self._feed_options["changeFeedState"] = self._change_feed_state
8181

8282
self._change_feed_state.populate_feed_options(self._feed_options)
@@ -111,8 +111,8 @@ def __init__(
111111
self,
112112
client,
113113
resource_link: str,
114-
feed_options: Dict[str, Any],
115-
fetch_function: Callable[[Dict[str, Any]], Tuple[List[Dict[str, Any]], Dict[str, Any]]]):
114+
feed_options: dict[str, Any],
115+
fetch_function: Callable[[dict[str, Any]], Tuple[list[dict[str, Any]], dict[str, Any]]]):
116116

117117
self._client = client
118118
self._feed_options = feed_options
@@ -125,7 +125,7 @@ def __init__(
125125
self._resource_link = resource_link
126126
self._fetch_function = fetch_function
127127

128-
def fetch_next_block(self) -> List[Dict[str, Any]]:
128+
def fetch_next_block(self) -> list[dict[str, Any]]:
129129
"""Returns a block of results.
130130
131131
:return: List of results.
@@ -150,7 +150,7 @@ def callback():
150150

151151
return self.fetch_next_block()
152152

153-
def fetch_change_feed_items(self) -> List[Dict[str, Any]]:
153+
def fetch_change_feed_items(self) -> list[dict[str, Any]]:
154154
self._feed_options["changeFeedState"] = self._change_feed_state
155155

156156
self._change_feed_state.populate_feed_options(self._feed_options)

sdk/cosmos/azure-cosmos/azure/cosmos/_change_feed/change_feed_iterable.py

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

2222
"""Iterable change feed results in the Azure Cosmos database service.
2323
"""
24-
from typing import Dict, Any, Tuple, List, Optional, Callable, cast, Union
24+
from typing import Any, Tuple, Optional, Callable, cast, Union
2525

2626
from azure.core.paging import PageIterator
2727

@@ -38,8 +38,8 @@ class ChangeFeedIterable(PageIterator):
3838
def __init__(
3939
self,
4040
client,
41-
options: Dict[str, Any],
42-
fetch_function=Optional[Callable[[Dict[str, Any]], Tuple[List[Dict[str, Any]], Dict[str, Any]]]],
41+
options: dict[str, Any],
42+
fetch_function=Optional[Callable[[dict[str, Any]], Tuple[list[dict[str, Any]], dict[str, Any]]]],
4343
collection_link=Optional[str],
4444
continuation_token=Optional[str],
4545
) -> None:
@@ -87,7 +87,7 @@ def __init__(
8787
self._unpack, # type: ignore[arg-type]
8888
continuation_token=continuation_token)
8989

90-
def _unpack(self, block: List[Dict[str, Any]]) -> Tuple[Optional[str], List[Dict[str, Any]]]:
90+
def _unpack(self, block: list[dict[str, Any]]) -> Tuple[Optional[str], list[dict[str, Any]]]:
9191
continuation: Optional[str] = None
9292
if self._client.last_response_headers:
9393
continuation = self._client.last_response_headers.get('etag')
@@ -96,7 +96,7 @@ def _unpack(self, block: List[Dict[str, Any]]) -> Tuple[Optional[str], List[Dict
9696
self._did_a_call_already = False
9797
return continuation, block
9898

99-
def _fetch_next(self, *args) -> List[Dict[str, Any]]: # pylint: disable=unused-argument
99+
def _fetch_next(self, *args) -> list[dict[str, Any]]: # pylint: disable=unused-argument
100100
"""Return a block of results with respecting retry policy.
101101
102102
:param Any args:
@@ -138,7 +138,7 @@ def _initialize_change_feed_fetcher(self) -> None:
138138
self._fetch_function
139139
)
140140

141-
def _validate_change_feed_state_context(self, change_feed_state_context: Dict[str, Any]) -> None:
141+
def _validate_change_feed_state_context(self, change_feed_state_context: dict[str, Any]) -> None:
142142

143143
if change_feed_state_context.get("continuationPkRangeId") is not None:
144144
# if continuation token is in v1 format, throw exception if feed_range is set

0 commit comments

Comments
 (0)