Skip to content

Commit b4d65d3

Browse files
authored
[Cosmos] Public API cleanup for query_items API (#43240)
* Fix missing return types for query_items * Fix overload definition of query_items to be more clear * Fix overload definition of query_items for async
1 parent dbf1475 commit b4d65d3

File tree

2 files changed

+170
-11
lines changed

2 files changed

+170
-11
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ def query_items(
505505
max_integrated_cache_staleness_in_ms: Optional[int] = None,
506506
max_item_count: Optional[int] = None,
507507
parameters: Optional[List[Dict[str, object]]] = None,
508-
partition_key: Optional[PartitionKeyType] = None,
508+
partition_key: PartitionKeyType,
509509
populate_index_metrics: Optional[bool] = None,
510510
populate_query_metrics: Optional[bool] = None,
511511
priority: Optional[Literal["High", "Low"]] = None,
512512
response_hook: Optional[Callable[[Mapping[str, str], Dict[str, Any]], None]] = None,
513513
session_token: Optional[str] = None,
514514
throughput_bucket: Optional[int] = None,
515515
**kwargs: Any
516-
):
516+
) -> AsyncItemPaged[Dict[str, Any]]:
517517
"""Return all results matching the given `query`.
518518
519519
You can use any value for the container name in the FROM clause, but
@@ -557,7 +557,7 @@ def query_items(
557557
:keyword str session_token: Token for use with Session consistency.
558558
:keyword int throughput_bucket: The desired throughput bucket for the client.
559559
:returns: An Iterable of items (dicts).
560-
:rtype: ItemPaged[Dict[str, Any]]
560+
:rtype: AsyncItemPaged[Dict[str, Any]]
561561
562562
.. admonition:: Example:
563563
@@ -584,7 +584,7 @@ def query_items(
584584
*,
585585
continuation_token_limit: Optional[int] = None,
586586
enable_scan_in_query: Optional[bool] = None,
587-
feed_range: Optional[Dict[str, Any]] = None,
587+
feed_range: Dict[str, Any],
588588
initial_headers: Optional[Dict[str, str]] = None,
589589
max_integrated_cache_staleness_in_ms: Optional[int] = None,
590590
max_item_count: Optional[int] = None,
@@ -596,7 +596,7 @@ def query_items(
596596
session_token: Optional[str] = None,
597597
throughput_bucket: Optional[int] = None,
598598
**kwargs: Any
599-
):
599+
) -> AsyncItemPaged[Dict[str, Any]]:
600600
"""Return all results matching the given `query`.
601601
602602
You can use any value for the container name in the FROM clause, but
@@ -614,6 +614,7 @@ def query_items(
614614
in this list are specified as the names of the Azure Cosmos locations like, 'West US', 'East US' and so on.
615615
If all preferred locations were excluded, primary/hub location will be used.
616616
This excluded_location will override existing excluded_locations in client level.
617+
:keyword Dict[str, Any] feed_range: The feed range that is used to define the scope.
617618
:keyword Dict[str, str] initial_headers: Initial headers to be sent as part of the request.
618619
:keyword int max_integrated_cache_staleness_in_ms: The max cache staleness for the integrated cache in
619620
milliseconds. For accounts configured to use the integrated cache, using Session or Eventual consistency,
@@ -635,7 +636,84 @@ def query_items(
635636
:keyword str session_token: Token for use with Session consistency.
636637
:keyword int throughput_bucket: The desired throughput bucket for the client.
637638
:returns: An Iterable of items (dicts).
638-
:rtype: ItemPaged[Dict[str, Any]]
639+
:rtype: AsyncItemPaged[Dict[str, Any]]
640+
641+
.. admonition:: Example:
642+
643+
.. literalinclude:: ../samples/examples_async.py
644+
:start-after: [START query_items]
645+
:end-before: [END query_items]
646+
:language: python
647+
:dedent: 0
648+
:caption: Get all products that have not been discontinued:
649+
650+
.. literalinclude:: ../samples/examples_async.py
651+
:start-after: [START query_items_param]
652+
:end-before: [END query_items_param]
653+
:language: python
654+
:dedent: 0
655+
:caption: Parameterized query to get all products that have been discontinued:
656+
"""
657+
...
658+
659+
@overload
660+
def query_items(
661+
self,
662+
query: str,
663+
*,
664+
continuation_token_limit: Optional[int] = None,
665+
enable_scan_in_query: Optional[bool] = None,
666+
initial_headers: Optional[Dict[str, str]] = None,
667+
max_integrated_cache_staleness_in_ms: Optional[int] = None,
668+
max_item_count: Optional[int] = None,
669+
parameters: Optional[List[Dict[str, object]]] = None,
670+
populate_index_metrics: Optional[bool] = None,
671+
populate_query_metrics: Optional[bool] = None,
672+
priority: Optional[Literal["High", "Low"]] = None,
673+
response_hook: Optional[Callable[[Mapping[str, str], Dict[str, Any]], None]] = None,
674+
session_token: Optional[str] = None,
675+
throughput_bucket: Optional[int] = None,
676+
**kwargs: Any
677+
) -> AsyncItemPaged[Dict[str, Any]]:
678+
"""Return all results matching the given `query`.
679+
680+
You can use any value for the container name in the FROM clause, but
681+
often the container name is used. In the examples below, the container
682+
name is "products," and is aliased as "p" for easier referencing in
683+
the WHERE clause.
684+
685+
:param str query: The Azure Cosmos DB SQL query to execute.
686+
:keyword int continuation_token_limit: The size limit in kb of the response continuation token in the query
687+
response. Valid values are positive integers.
688+
A value of 0 is the same as not passing a value (default no limit).
689+
:keyword bool enable_scan_in_query: Allow scan on the queries which couldn't be served as
690+
indexing was opted out on the requested paths.
691+
:keyword list[str] excluded_locations: Excluded locations to be skipped from preferred locations. The locations
692+
in this list are specified as the names of the Azure Cosmos locations like, 'West US', 'East US' and so on.
693+
If all preferred locations were excluded, primary/hub location will be used.
694+
This excluded_location will override existing excluded_locations in client level.
695+
:keyword Dict[str, str] initial_headers: Initial headers to be sent as part of the request.
696+
:keyword int max_integrated_cache_staleness_in_ms: The max cache staleness for the integrated cache in
697+
milliseconds. For accounts configured to use the integrated cache, using Session or Eventual consistency,
698+
responses are guaranteed to be no staler than this value.
699+
:keyword int max_item_count: Max number of items to be returned in the enumeration operation.
700+
:keyword parameters: Optional array of parameters to the query.
701+
Each parameter is a dict() with 'name' and 'value' keys.
702+
Ignored if no query is provided.
703+
:paramtype parameters: [List[Dict[str, object]]]
704+
:keyword bool populate_index_metrics: Used to obtain the index metrics to understand how the query engine used
705+
existing indexes and how it could use potential new indexes. Please note that this option will incur
706+
overhead, so it should be enabled only when debugging slow queries.
707+
:keyword bool populate_query_metrics: Enable returning query metrics in response headers.
708+
:keyword Literal["High", "Low"] priority: Priority based execution allows users to set a priority for each
709+
request. Once the user has reached their provisioned throughput, low priority requests are throttled
710+
before high priority requests start getting throttled. Feature must first be enabled at the account level.
711+
:keyword response_hook: A callable invoked with the response metadata.
712+
:paramtype response_hook: Callable[[Mapping[str, str], Dict[str, Any]], None]
713+
:keyword str session_token: Token for use with Session consistency.
714+
:keyword int throughput_bucket: The desired throughput bucket for the client.
715+
:returns: An Iterable of items (dicts).
716+
:rtype: AsyncItemPaged[Dict[str, Any]]
639717
640718
.. admonition:: Example:
641719
@@ -708,7 +786,7 @@ def query_items(
708786
:keyword str session_token: Token for use with Session consistency.
709787
:keyword int throughput_bucket: The desired throughput bucket for the client.
710788
:returns: An Iterable of items (dicts).
711-
:rtype: ItemPaged[Dict[str, Any]]
789+
:rtype: AsyncItemPaged[Dict[str, Any]]
712790
713791
.. admonition:: Example:
714792

sdk/cosmos/azure-cosmos/azure/cosmos/container.py

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,15 @@ def query_items(
677677
max_integrated_cache_staleness_in_ms: Optional[int] = None,
678678
max_item_count: Optional[int] = None,
679679
parameters: Optional[List[Dict[str, object]]] = None,
680-
partition_key: Optional[_PartitionKeyType] = None,
680+
partition_key: _PartitionKeyType,
681681
populate_index_metrics: Optional[bool] = None,
682682
populate_query_metrics: Optional[bool] = None,
683683
priority: Optional[Literal["High", "Low"]] = None,
684684
response_hook: Optional[Callable[[Mapping[str, str], Dict[str, Any]], None]] = None,
685685
session_token: Optional[str] = None,
686686
throughput_bucket: Optional[int] = None,
687687
**kwargs: Any
688-
):
688+
) -> ItemPaged[Dict[str, Any]]:
689689
"""Return all results matching the given `query`.
690690
691691
You can use any value for the container name in the FROM clause, but
@@ -760,7 +760,7 @@ def query_items(
760760
continuation_token_limit: Optional[int] = None,
761761
enable_cross_partition_query: Optional[bool] = None,
762762
enable_scan_in_query: Optional[bool] = None,
763-
feed_range: Optional[Dict[str, Any]] = None,
763+
feed_range: Dict[str, Any],
764764
initial_headers: Optional[Dict[str, str]] = None,
765765
max_integrated_cache_staleness_in_ms: Optional[int] = None,
766766
max_item_count: Optional[int] = None,
@@ -772,7 +772,7 @@ def query_items(
772772
session_token: Optional[str] = None,
773773
throughput_bucket: Optional[int] = None,
774774
**kwargs: Any
775-
):
775+
) -> ItemPaged[Dict[str, Any]]:
776776
"""Return all results matching the given `query`.
777777
778778
You can use any value for the container name in the FROM clause, but
@@ -835,6 +835,87 @@ def query_items(
835835
"""
836836
...
837837

838+
@overload
839+
def query_items(
840+
self,
841+
query: str,
842+
*,
843+
continuation_token_limit: Optional[int] = None,
844+
enable_cross_partition_query: Optional[bool] = None,
845+
enable_scan_in_query: Optional[bool] = None,
846+
initial_headers: Optional[Dict[str, str]] = None,
847+
max_integrated_cache_staleness_in_ms: Optional[int] = None,
848+
max_item_count: Optional[int] = None,
849+
parameters: Optional[List[Dict[str, object]]] = None,
850+
populate_index_metrics: Optional[bool] = None,
851+
populate_query_metrics: Optional[bool] = None,
852+
priority: Optional[Literal["High", "Low"]] = None,
853+
response_hook: Optional[Callable[[Mapping[str, str], Dict[str, Any]], None]] = None,
854+
session_token: Optional[str] = None,
855+
throughput_bucket: Optional[int] = None,
856+
**kwargs: Any
857+
) -> ItemPaged[Dict[str, Any]]:
858+
"""Return all results matching the given `query`.
859+
860+
You can use any value for the container name in the FROM clause, but
861+
often the container name is used. In the examples below, the container
862+
name is "products," and is aliased as "p" for easier referencing in
863+
the WHERE clause.
864+
865+
:param str query: The Azure Cosmos DB SQL query to execute.
866+
:keyword int continuation_token_limit: The size limit in kb of the response continuation token in the query
867+
response. Valid values are positive integers.
868+
A value of 0 is the same as not passing a value (default no limit).
869+
:keyword bool enable_cross_partition_query: Allows sending of more than one request to
870+
execute the query in the Azure Cosmos DB service.
871+
More than one request is necessary if the query is not scoped to single partition key value.
872+
:keyword bool enable_scan_in_query: Allow scan on the queries which couldn't be served as
873+
indexing was opted out on the requested paths.
874+
:keyword list[str] excluded_locations: Excluded locations to be skipped from preferred locations. The locations
875+
in this list are specified as the names of the Azure Cosmos locations like, 'West US', 'East US' and so on.
876+
If all preferred locations were excluded, primary/hub location will be used.
877+
This excluded_location will override existing excluded_locations in client level.
878+
:keyword Dict[str, str] initial_headers: Initial headers to be sent as part of the request.
879+
:keyword int max_integrated_cache_staleness_in_ms: The max cache staleness for the integrated cache in
880+
milliseconds. For accounts configured to use the integrated cache, using Session or Eventual consistency,
881+
responses are guaranteed to be no staler than this value.
882+
:keyword int max_item_count: Max number of items to be returned in the enumeration operation.
883+
:keyword parameters: Optional array of parameters to the query.
884+
Each parameter is a dict() with 'name' and 'value' keys.
885+
Ignored if no query is provided.
886+
:paramtype parameters: [List[Dict[str, object]]]
887+
:keyword bool populate_index_metrics: Used to obtain the index metrics to understand how the query engine used
888+
existing indexes and how it could use potential new indexes. Please note that this option will incur
889+
overhead, so it should be enabled only when debugging slow queries.
890+
:keyword bool populate_query_metrics: Enable returning query metrics in response headers.
891+
:keyword Literal["High", "Low"] priority: Priority based execution allows users to set a priority for each
892+
request. Once the user has reached their provisioned throughput, low priority requests are throttled
893+
before high priority requests start getting throttled. Feature must first be enabled at the account level.
894+
:keyword response_hook: A callable invoked with the response metadata.
895+
:paramtype response_hook: Callable[[Mapping[str, str], Dict[str, Any]], None]
896+
:keyword str session_token: Token for use with Session consistency.
897+
:keyword int throughput_bucket: The desired throughput bucket for the client.
898+
:returns: An Iterable of items (dicts).
899+
:rtype: ItemPaged[Dict[str, Any]]
900+
901+
.. admonition:: Example:
902+
903+
.. literalinclude:: ../samples/examples.py
904+
:start-after: [START query_items]
905+
:end-before: [END query_items]
906+
:language: python
907+
:dedent: 0
908+
:caption: Get all products that have not been discontinued:
909+
910+
.. literalinclude:: ../samples/examples.py
911+
:start-after: [START query_items_param]
912+
:end-before: [END query_items_param]
913+
:language: python
914+
:dedent: 0
915+
:caption: Parameterized query to get all products that have been discontinued:
916+
"""
917+
...
918+
838919
@distributed_trace
839920
def query_items( # pylint:disable=docstring-missing-param
840921
self,

0 commit comments

Comments
 (0)