From 24241c05bd10a49cacae5744408a1361a311ca99 Mon Sep 17 00:00:00 2001 From: Yalin Li Date: Tue, 19 Sep 2023 11:19:05 -0700 Subject: [PATCH] [AppConfig] Run pyright (#32016) --- .../azure-appconfiguration/azure/__init__.py | 2 +- .../_azure_appconfiguration_client.py | 32 ++++++++----------- .../_generated/_serialization.py | 2 +- .../azure/appconfiguration/_models.py | 12 ++----- .../azure/appconfiguration/_sync_token.py | 10 +++--- .../_azure_appconfiguration_client_async.py | 27 +++++++--------- .../appconfiguration/aio/_sync_token_async.py | 12 ++++--- 7 files changed, 41 insertions(+), 56 deletions(-) diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/__init__.py b/sdk/appconfiguration/azure-appconfiguration/azure/__init__.py index d55ccad1f573..8db66d3d0f0f 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/__init__.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/__init__.py @@ -1 +1 @@ -__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore +__path__ = __import__("pkgutil").extend_path(__path__, __name__) diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_azure_appconfiguration_client.py b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_azure_appconfiguration_client.py index 88456811127e..da859c45f469 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_azure_appconfiguration_client.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_azure_appconfiguration_client.py @@ -4,8 +4,8 @@ # license information. # ------------------------------------------------------------------------- import binascii -import sys from typing import Any, Dict, List, Mapping, Optional, Union, cast +from typing_extensions import Literal from azure.core import MatchConditions from azure.core.paging import ItemPaged from azure.core.credentials import TokenCredential @@ -45,11 +45,6 @@ from ._sync_token import SyncTokenPolicy from ._user_agent import USER_AGENT -if sys.version_info >= (3, 8): - from typing import Literal # pylint: disable=no-name-in-module, ungrouped-imports -else: - from typing_extensions import Literal # type: ignore # pylint: disable=ungrouped-imports - class AzureAppConfigurationClient: """Represents a client that calls restful API of Azure App Configuration service. @@ -86,7 +81,7 @@ def __init__(self, base_url: str, credential: TokenCredential, **kwargs: Any) -> if pipeline is None: aad_mode = not isinstance(credential, AppConfigConnectionStringCredential) pipeline = self._create_appconfig_pipeline( - credential=credential, aad_mode=aad_mode, base_url=base_url, **kwargs + credential=credential, credential_scopes=self._credential_scopes, aad_mode=aad_mode, **kwargs ) self._impl = AzureAppConfiguration( @@ -118,15 +113,14 @@ def from_connection_string(cls, connection_string: str, **kwargs: Any) -> "Azure **kwargs ) - def _create_appconfig_pipeline(self, credential, base_url=None, aad_mode=False, **kwargs): + def _create_appconfig_pipeline(self, credential, credential_scopes, aad_mode=False, **kwargs): transport = kwargs.get("transport") policies = kwargs.get("policies") if policies is None: # [] is a valid policy list if aad_mode: - scope = base_url.strip("/") + "/.default" if hasattr(credential, "get_token"): - credential_policy = BearerTokenCredentialPolicy(credential, scope) + credential_policy = BearerTokenCredentialPolicy(credential, credential_scopes) else: raise TypeError( "Please provide an instance from azure-identity " @@ -241,7 +235,7 @@ def get_configuration_setting( key="MyKey", label="MyLabel" ) """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfPresent: @@ -290,7 +284,7 @@ def add_configuration_setting(self, configuration_setting: ConfigurationSetting, added_config_setting = client.add_configuration_setting(config_setting) """ key_value = configuration_setting._to_generated() - custom_headers = CaseInsensitiveDict(kwargs.get("headers")) # type: Mapping[str, Any] + custom_headers: Mapping[str, Any] = CaseInsensitiveDict(kwargs.get("headers")) error_map = {412: ResourceExistsError} try: key_value_added = self._impl.put_key_value( @@ -346,8 +340,8 @@ def set_configuration_setting( returned_config_setting = client.set_configuration_setting(config_setting) """ key_value = configuration_setting._to_generated() - custom_headers = CaseInsensitiveDict(kwargs.get("headers")) # type: Mapping[str, Any] - error_map = {409: ResourceReadOnlyError} # type: Dict[int, Any] + custom_headers: Mapping[str, Any] = CaseInsensitiveDict(kwargs.get("headers")) + error_map: Dict[int, Any] = {409: ResourceReadOnlyError} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: @@ -404,8 +398,8 @@ def delete_configuration_setting( # pylint:disable=delete-operation-wrong-retur """ etag = kwargs.pop("etag", None) match_condition = kwargs.pop("match_condition", MatchConditions.Unconditionally) - custom_headers = CaseInsensitiveDict(kwargs.get("headers")) # type: Mapping[str, Any] - error_map = {409: ResourceReadOnlyError} # type: Dict[int, Any] + custom_headers: Mapping[str, Any] = CaseInsensitiveDict(kwargs.get("headers")) + error_map: Dict[int, Any] = {409: ResourceReadOnlyError} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: @@ -510,7 +504,7 @@ def set_read_only( read_only_config_setting = client.set_read_only(config_setting) read_only_config_setting = client.set_read_only(config_setting, read_only=False) """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} match_condition = kwargs.pop("match_condition", MatchConditions.Unconditionally) if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) @@ -614,7 +608,7 @@ def archive_snapshot( :rtype: ~azure.appconfiguration.Snapshot :raises: :class:`~azure.core.exceptions.HttpResponseError` """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: @@ -657,7 +651,7 @@ def recover_snapshot( :rtype: ~azure.appconfiguration.Snapshot :raises: :class:`~azure.core.exceptions.HttpResponseError` """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_generated/_serialization.py b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_generated/_serialization.py index eab91b1ae78d..9f3e29b11388 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_generated/_serialization.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_generated/_serialization.py @@ -1912,7 +1912,7 @@ def deserialize_date(attr): if re.search(r"[^\W\d_]", attr, re.I + re.U): # type: ignore raise DeserializationError("Date must have only digits and -. Received: %s" % attr) # This must NOT use defaultmonth/defaultday. Using None ensure this raises an exception. - return isodate.parse_date(attr, defaultmonth=None, defaultday=None) + return isodate.parse_date(attr, defaultmonth=0, defaultday=0) @staticmethod def deserialize_time(attr): diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_models.py b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_models.py index c641f9509c19..c8e099369f20 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_models.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_models.py @@ -3,9 +3,9 @@ # Licensed under the MIT License. # ------------------------------------ import json -import sys from datetime import datetime from typing import Any, Dict, List, Optional, Union, cast +from typing_extensions import Literal from azure.core.rest import HttpResponse from ._generated._serialization import Model @@ -16,12 +16,6 @@ SnapshotStatus, ) -if sys.version_info >= (3, 8): - from typing import Literal # pylint: disable=no-name-in-module, ungrouped-imports -else: - from typing_extensions import Literal # type: ignore # pylint: disable=ungrouped-imports - - PolymorphicConfigurationSetting = Union[ "ConfigurationSetting", "SecretReferenceConfigurationSetting", "FeatureFlagConfigurationSetting" ] @@ -38,7 +32,7 @@ class ConfigurationSetting(Model): """The key of the configuration setting.""" label: str """The label of the configuration setting.""" - content_type: str + content_type: Optional[str] """The content_type of the configuration setting.""" last_modified: datetime """A date representing the last time the key-value was modified.""" @@ -67,7 +61,7 @@ def __init__(self, **kwargs: Any) -> None: self.label = kwargs.get("label", None) self.value = kwargs.get("value", None) self.etag = kwargs.get("etag", None) - self.content_type = kwargs.get("content_type", self.content_type) + self.content_type = kwargs.get("content_type", None) self.last_modified = kwargs.get("last_modified", None) self.read_only = kwargs.get("read_only", None) self.tags = kwargs.get("tags", {}) diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_sync_token.py b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_sync_token.py index 836320aa3305..c1ab0d8e8541 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_sync_token.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_sync_token.py @@ -23,7 +23,7 @@ # IN THE SOFTWARE. # # -------------------------------------------------------------------------- -from typing import Any, Dict +from typing import Any, Dict, Optional from threading import Lock from azure.core.pipeline import PipelineRequest, PipelineResponse from azure.core.pipeline.policies import SansIOHTTPPolicy @@ -67,10 +67,10 @@ class SyncTokenPolicy(SansIOHTTPPolicy): def __init__(self, **kwargs: Any) -> None: # pylint: disable=unused-argument self._sync_token_header = "Sync-Token" - self._sync_tokens = {} # type: Dict[str, Any] + self._sync_tokens: Dict[str, Any] = {} self._lock = Lock() - def on_request(self, request: PipelineRequest) -> None: # type: ignore # pylint: disable=arguments-differ + def on_request(self, request: PipelineRequest) -> None: """This is executed before sending the request to the next policy. :param request: The PipelineRequest object. :type request: ~azure.core.pipeline.PipelineRequest @@ -80,7 +80,7 @@ def on_request(self, request: PipelineRequest) -> None: # type: ignore # pylint if sync_token_header: request.http_request.headers.update({self._sync_token_header: sync_token_header}) - def on_response(self, request: PipelineRequest, response: PipelineResponse) -> None: # type: ignore # pylint: disable=arguments-differ + def on_response(self, request: PipelineRequest, response: PipelineResponse) -> None: """This is executed after the request comes back from the policy. :param request: The PipelineRequest object. @@ -104,7 +104,7 @@ def add_token(self, full_raw_tokens: str) -> None: sync_token = SyncToken.from_sync_token_string(raw_token) self._update_sync_token(sync_token) - def _update_sync_token(self, sync_token: SyncToken) -> None: + def _update_sync_token(self, sync_token: Optional[SyncToken]) -> None: if not sync_token: return with self._lock: diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_azure_appconfiguration_client_async.py b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_azure_appconfiguration_client_async.py index 41d69ce692e7..96fad2af9093 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_azure_appconfiguration_client_async.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_azure_appconfiguration_client_async.py @@ -4,8 +4,8 @@ # license information. # ------------------------------------------------------------------------- import binascii -import sys from typing import Any, Dict, List, Mapping, Optional, Union, cast +from typing_extensions import Literal from azure.core import MatchConditions from azure.core.async_paging import AsyncItemPaged from azure.core.credentials_async import AsyncTokenCredential @@ -37,11 +37,6 @@ prep_if_none_match, ) -if sys.version_info >= (3, 8): - from typing import Literal # pylint: disable=no-name-in-module, ungrouped-imports -else: - from typing_extensions import Literal # type: ignore # pylint: disable=ungrouped-imports - class AzureAppConfigurationClient: """Represents a client that calls restful API of Azure App Configuration service. @@ -80,7 +75,7 @@ def __init__(self, base_url: str, credential: AsyncTokenCredential, **kwargs: An if aad_mode: if hasattr(credential, "get_token"): credential_policy = AsyncBearerTokenCredentialPolicy( - credential, # type: ignore + credential, credential_scopes, ) else: @@ -217,7 +212,7 @@ async def get_configuration_setting( key="MyKey", label="MyLabel" ) """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfPresent: @@ -270,7 +265,7 @@ async def add_configuration_setting( added_config_setting = await async_client.add_configuration_setting(config_setting) """ key_value = configuration_setting._to_generated() - custom_headers = CaseInsensitiveDict(kwargs.get("headers")) # type: Mapping[str, Any] + custom_headers: Mapping[str, Any] = CaseInsensitiveDict(kwargs.get("headers")) error_map = {412: ResourceExistsError} try: @@ -331,8 +326,8 @@ async def set_configuration_setting( etag = kwargs.get("etag", configuration_setting.etag) key_value = configuration_setting._to_generated() - custom_headers = CaseInsensitiveDict(kwargs.get("headers")) # type: Mapping[str, Any] - error_map = {409: ResourceReadOnlyError} # type: Dict[int, Any] + custom_headers: Mapping[str, Any] = CaseInsensitiveDict(kwargs.get("headers")) + error_map: Dict[int, Any] = {409: ResourceReadOnlyError} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: @@ -390,8 +385,8 @@ async def delete_configuration_setting( """ etag = kwargs.pop("etag", None) match_condition = kwargs.pop("match_condition", MatchConditions.Unconditionally) - custom_headers = CaseInsensitiveDict(kwargs.get("headers")) # type: Mapping[str, Any] - error_map = {409: ResourceReadOnlyError} # type: Dict[int, Any] + custom_headers: Mapping[str, Any] = CaseInsensitiveDict(kwargs.get("headers")) + error_map: Dict[int, Any] = {409: ResourceReadOnlyError} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: @@ -499,7 +494,7 @@ async def set_read_only( read_only_config_setting = await async_client.set_read_only(config_setting) read_only_config_setting = await client.set_read_only(config_setting, read_only=False) """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} match_condition = kwargs.pop("match_condition", MatchConditions.Unconditionally) if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) @@ -603,7 +598,7 @@ async def archive_snapshot( :rtype: ~azure.appconfiguration.Snapshot :raises: :class:`~azure.core.exceptions.HttpResponseError` """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: @@ -646,7 +641,7 @@ async def recover_snapshot( :rtype: ~azure.appconfiguration.Snapshot :raises: :class:`~azure.core.exceptions.HttpResponseError` """ - error_map = {} # type: Dict[int, Any] + error_map: Dict[int, Any] = {} if match_condition == MatchConditions.IfNotModified: error_map.update({412: ResourceModifiedError}) if match_condition == MatchConditions.IfModified: diff --git a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_sync_token_async.py b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_sync_token_async.py index 9d2441dc438e..5bf387ac7598 100644 --- a/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_sync_token_async.py +++ b/sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_sync_token_async.py @@ -23,7 +23,7 @@ # IN THE SOFTWARE. # # -------------------------------------------------------------------------- -from typing import Any, Dict +from typing import Any, Dict, Optional from asyncio import Lock from azure.core.pipeline import PipelineRequest, PipelineResponse from azure.core.pipeline.policies import SansIOHTTPPolicy @@ -39,10 +39,10 @@ class AsyncSyncTokenPolicy(SansIOHTTPPolicy): def __init__(self, **kwargs: Any) -> None: # pylint: disable=unused-argument self._sync_token_header = "Sync-Token" - self._sync_tokens = {} # type: Dict[str, Any] + self._sync_tokens: Dict[str, Any] = {} self._lock = Lock() - async def on_request(self, request: PipelineRequest) -> None: # type: ignore # pylint: disable=arguments-differ, invalid-overridden-method + async def on_request(self, request: PipelineRequest) -> None: # pylint: disable=invalid-overridden-method """This is executed before sending the request to the next policy. :param request: The PipelineRequest object. @@ -53,7 +53,9 @@ async def on_request(self, request: PipelineRequest) -> None: # type: ignore # if sync_token_header: request.http_request.headers.update({self._sync_token_header: sync_token_header}) - async def on_response(self, request: PipelineRequest, response: PipelineResponse) -> None: # type: ignore # pylint: disable=arguments-differ, invalid-overridden-method + async def on_response( # pylint: disable=invalid-overridden-method + self, request: PipelineRequest, response: PipelineResponse + ) -> None: """This is executed after the request comes back from the policy. :param request: The PipelineRequest object. @@ -77,7 +79,7 @@ async def add_token(self, full_raw_tokens: str) -> None: sync_token = SyncToken.from_sync_token_string(raw_token) await self._update_sync_token(sync_token) - async def _update_sync_token(self, sync_token: SyncToken) -> None: + async def _update_sync_token(self, sync_token: Optional[SyncToken]) -> None: if not sync_token: return async with self._lock: