Skip to content

Commit c41c06e

Browse files
authored
[Key Vault] Resolve next-pylint and next-mypy errors (#33448)
1 parent 0ad6362 commit c41c06e

38 files changed

+894
-995
lines changed

sdk/keyvault/azure-keyvault-administration/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ to the library's [credential documentation][sas_docs]. Alternatively, it is poss
278278
CONTAINER_URL = os.environ["CONTAINER_URL"]
279279
SAS_TOKEN = os.environ["SAS_TOKEN"]
280280

281-
backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL, SAS_TOKEN).result()
281+
backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL, sas_token=SAS_TOKEN).result()
282282
print(f"Azure Storage Blob URL of the backup: {backup_result.folder_url}")
283283
```
284284

@@ -297,14 +297,14 @@ For more details on creating a SAS token using a `BlobServiceClient` from [`azur
297297
to the library's [credential documentation][sas_docs]. Alternatively, it is possible to
298298
[generate a SAS token in Storage Explorer][storage_explorer].
299299

300-
<!-- SNIPPET:backup_restore_operations.begin_backup -->
300+
<!-- SNIPPET:backup_restore_operations.begin_restore -->
301301

302302
```python
303-
CONTAINER_URL = os.environ["CONTAINER_URL"]
304303
SAS_TOKEN = os.environ["SAS_TOKEN"]
305304

306-
backup_result: KeyVaultBackupResult = client.begin_backup(CONTAINER_URL, SAS_TOKEN).result()
307-
print(f"Azure Storage Blob URL of the backup: {backup_result.folder_url}")
305+
# `backup_result` is the KeyVaultBackupResult returned by `begin_backup`
306+
client.begin_restore(backup_result.folder_url, sas_token=SAS_TOKEN).wait()
307+
print("Vault restored successfully.")
308308
```
309309

310310
<!-- END SNIPPET -->

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_access_control_client.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
from typing import TYPE_CHECKING
6-
from uuid import uuid4
5+
from typing import Any, Union
6+
from uuid import UUID, uuid4
77

88
from azure.core.exceptions import ResourceNotFoundError
9+
from azure.core.paging import ItemPaged
910
from azure.core.tracing.decorator import distributed_trace
1011

12+
from ._enums import KeyVaultRoleScope
1113
from ._models import KeyVaultRoleAssignment, KeyVaultRoleDefinition
1214
from ._internal import KeyVaultClientBase
1315

14-
if TYPE_CHECKING:
15-
# pylint:disable=ungrouped-imports
16-
from typing import Union
17-
from uuid import UUID
18-
from azure.core.paging import ItemPaged
19-
from ._enums import KeyVaultRoleScope
20-
2116

2217
class KeyVaultAccessControlClient(KeyVaultClientBase):
2318
"""Manages role-based access to Azure Key Vault.
@@ -27,7 +22,7 @@ class KeyVaultAccessControlClient(KeyVaultClientBase):
2722
See https://aka.ms/azsdk/blog/vault-uri for details.
2823
:param credential: An object which can provide an access token for the vault, such as a credential from
2924
:mod:`azure.identity`
30-
:type credential: :class:`~azure.core.credentials.TokenCredential`
25+
:type credential: ~azure.core.credentials.TokenCredential
3126
3227
:keyword api_version: Version of the service API to use. Defaults to the most recent.
3328
:paramtype api_version: ~azure.keyvault.administration.ApiVersion or str
@@ -39,7 +34,7 @@ class KeyVaultAccessControlClient(KeyVaultClientBase):
3934

4035
@distributed_trace
4136
def create_role_assignment(
42-
self, scope: "Union[str, KeyVaultRoleScope]", definition_id: str, principal_id: str, **kwargs
37+
self, scope: Union[str, KeyVaultRoleScope], definition_id: str, principal_id: str, **kwargs: Any
4338
) -> KeyVaultRoleAssignment:
4439
"""Create a role assignment.
4540
@@ -74,7 +69,7 @@ def create_role_assignment(
7469

7570
@distributed_trace
7671
def delete_role_assignment(
77-
self, scope: "Union[str, KeyVaultRoleScope]", name: "Union[str, UUID]", **kwargs
72+
self, scope: Union[str, KeyVaultRoleScope], name: Union[str, UUID], **kwargs: Any
7873
) -> None:
7974
"""Delete a role assignment.
8075
@@ -96,7 +91,7 @@ def delete_role_assignment(
9691

9792
@distributed_trace
9893
def get_role_assignment(
99-
self, scope: "Union[str, KeyVaultRoleScope]", name: "Union[str, UUID]", **kwargs
94+
self, scope: Union[str, KeyVaultRoleScope], name: Union[str, UUID], **kwargs: Any
10095
) -> KeyVaultRoleAssignment:
10196
"""Get a role assignment.
10297
@@ -116,8 +111,8 @@ def get_role_assignment(
116111

117112
@distributed_trace
118113
def list_role_assignments(
119-
self, scope: "Union[str, KeyVaultRoleScope]", **kwargs
120-
) -> "ItemPaged[KeyVaultRoleAssignment]":
114+
self, scope: Union[str, KeyVaultRoleScope], **kwargs: Any
115+
) -> ItemPaged[KeyVaultRoleAssignment]:
121116
"""List all role assignments for a scope.
122117
123118
:param scope: scope of the role assignments. :class:`KeyVaultRoleScope` defines common broad scopes.
@@ -136,8 +131,8 @@ def list_role_assignments(
136131

137132
@distributed_trace
138133
def set_role_definition(
139-
self, scope: "Union[str, KeyVaultRoleScope]", **kwargs
140-
) -> "KeyVaultRoleDefinition":
134+
self, scope: Union[str, KeyVaultRoleScope], **kwargs: Any
135+
) -> KeyVaultRoleDefinition:
141136
"""Creates or updates a custom role definition.
142137
143138
To update a role definition, specify the definition's ``name``.
@@ -194,8 +189,8 @@ def set_role_definition(
194189

195190
@distributed_trace
196191
def get_role_definition(
197-
self, scope: "Union[str, KeyVaultRoleScope]", name: "Union[str, UUID]", **kwargs
198-
) -> "KeyVaultRoleDefinition":
192+
self, scope: Union[str, KeyVaultRoleScope], name: Union[str, UUID], **kwargs: Any
193+
) -> KeyVaultRoleDefinition:
199194
"""Get the specified role definition.
200195
201196
:param scope: scope of the role definition. :class:`KeyVaultRoleScope` defines common broad scopes.
@@ -214,7 +209,7 @@ def get_role_definition(
214209

215210
@distributed_trace
216211
def delete_role_definition(
217-
self, scope: "Union[str, KeyVaultRoleScope]", name: "Union[str, UUID]", **kwargs
212+
self, scope: Union[str, KeyVaultRoleScope], name: Union[str, UUID], **kwargs: Any
218213
) -> None:
219214
"""Deletes a custom role definition.
220215
@@ -236,8 +231,8 @@ def delete_role_definition(
236231

237232
@distributed_trace
238233
def list_role_definitions(
239-
self, scope: "Union[str, KeyVaultRoleScope]", **kwargs
240-
) -> "ItemPaged[KeyVaultRoleDefinition]":
234+
self, scope: Union[str, KeyVaultRoleScope], **kwargs: Any
235+
) -> ItemPaged[KeyVaultRoleDefinition]:
241236
"""List all role definitions applicable at and above a scope.
242237
243238
:param scope: scope of the role definitions. :class:`KeyVaultRoleScope` defines common broad scopes.

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_backup_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55
import base64
66
import functools
77
import pickle
8-
from typing import Any, Optional, overload, TYPE_CHECKING
8+
from typing import Any, Optional, overload
99
from urllib.parse import urlparse
1010

1111
from typing_extensions import Literal
1212

13+
from azure.core.polling import LROPoller
1314
from azure.core.tracing.decorator import distributed_trace
1415

1516
from ._models import KeyVaultBackupResult
1617
from ._internal import KeyVaultClientBase, parse_folder_url
1718
from ._internal.polling import KeyVaultBackupClientPolling, KeyVaultBackupClientPollingMethod
1819

19-
if TYPE_CHECKING:
20-
# pylint:disable=unused-import
21-
from azure.core.polling import LROPoller
22-
2320

2421
def _parse_status_url(url):
2522
parsed = urlparse(url)
@@ -35,7 +32,7 @@ class KeyVaultBackupClient(KeyVaultClientBase):
3532
See https://aka.ms/azsdk/blog/vault-uri for details.
3633
:param credential: An object which can provide an access token for the vault, such as a credential from
3734
:mod:`azure.identity`
38-
:type credential: :class:`~azure.core.credentials.TokenCredential`
35+
:type credential: ~azure.core.credentials.TokenCredential
3936
4037
:keyword api_version: Version of the service API to use. Defaults to the most recent.
4138
:paramtype api_version: ~azure.keyvault.administration.ApiVersion or str
@@ -51,7 +48,7 @@ def begin_backup(
5148
use_managed_identity: Literal[True],
5249
continuation_token: Optional[str] = None,
5350
**kwargs: Any,
54-
) -> "LROPoller[KeyVaultBackupResult]":
51+
) -> LROPoller[KeyVaultBackupResult]:
5552
...
5653

5754
@overload
@@ -62,11 +59,11 @@ def begin_backup(
6259
sas_token: str,
6360
continuation_token: Optional[str] = None,
6461
**kwargs: Any,
65-
) -> "LROPoller[KeyVaultBackupResult]":
62+
) -> LROPoller[KeyVaultBackupResult]:
6663
...
6764

6865
@distributed_trace
69-
def begin_backup(self, blob_storage_url: str, *args: str, **kwargs: Any) -> "LROPoller[KeyVaultBackupResult]":
66+
def begin_backup(self, blob_storage_url: str, *args: str, **kwargs: Any) -> LROPoller[KeyVaultBackupResult]:
7067
"""Begin a full backup of the Key Vault.
7168
7269
:param str blob_storage_url: URL of the blob storage container in which the backup will be stored, for example
@@ -142,7 +139,7 @@ def begin_restore(
142139
key_name: Optional[str] = None,
143140
continuation_token: Optional[str] = None,
144141
**kwargs: Any,
145-
) -> "LROPoller":
142+
) -> LROPoller[None]:
146143
...
147144

148145
@overload
@@ -154,11 +151,11 @@ def begin_restore(
154151
key_name: Optional[str] = None,
155152
continuation_token: Optional[str] = None,
156153
**kwargs: Any,
157-
) -> "LROPoller":
154+
) -> LROPoller[None]:
158155
...
159156

160157
@distributed_trace
161-
def begin_restore(self, folder_url: str, *args: str, **kwargs: Any) -> "LROPoller":
158+
def begin_restore(self, folder_url: str, *args: str, **kwargs: Any) -> LROPoller[None]:
162159
"""Restore a Key Vault backup.
163160
164161
This method restores either a complete Key Vault backup or when ``key_name`` has a value, a single key.

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_internal/async_challenge_auth_policy.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,33 @@
1515
"""
1616

1717
import time
18-
from typing import TYPE_CHECKING
18+
from typing import Any, Optional
1919
from urllib.parse import urlparse
2020

21+
from azure.core.credentials import AccessToken
22+
from azure.core.credentials_async import AsyncTokenCredential
23+
from azure.core.pipeline import PipelineRequest, PipelineResponse
2124
from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy
2225

2326
from . import http_challenge_cache as ChallengeCache
2427
from .challenge_auth_policy import _enforce_tls, _update_challenge
2528

26-
if TYPE_CHECKING:
27-
from typing import Optional
28-
from azure.core.credentials import AccessToken
29-
from azure.core.credentials_async import AsyncTokenCredential
30-
from azure.core.pipeline import PipelineRequest, PipelineResponse
31-
3229

3330
class AsyncChallengeAuthPolicy(AsyncBearerTokenCredentialPolicy):
3431
"""Policy for handling HTTP authentication challenges.
3532
3633
:param credential: An object which can provide an access token for the vault, such as a credential from
3734
:mod:`azure.identity.aio`
38-
:type credential: :class:`~azure.core.credentials_async.AsyncTokenCredential`
35+
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
3936
"""
4037

41-
def __init__(self, credential: "AsyncTokenCredential", *scopes: str, **kwargs) -> None:
38+
def __init__(self, credential: AsyncTokenCredential, *scopes: str, **kwargs: Any) -> None:
4239
super().__init__(credential, *scopes, **kwargs)
4340
self._credential = credential
44-
self._token: "Optional[AccessToken]" = None
41+
self._token: Optional[AccessToken] = None
4542
self._verify_challenge_resource = kwargs.pop("verify_challenge_resource", True)
4643

47-
async def on_request(self, request: "PipelineRequest") -> None:
44+
async def on_request(self, request: PipelineRequest) -> None:
4845
_enforce_tls(request)
4946
challenge = ChallengeCache.get_challenge_for_url(request.http_request.url)
5047
if challenge:
@@ -68,7 +65,7 @@ async def on_request(self, request: "PipelineRequest") -> None:
6865
request.http_request.headers["Content-Length"] = "0"
6966

7067

71-
async def on_challenge(self, request: "PipelineRequest", response: "PipelineResponse") -> bool:
68+
async def on_challenge(self, request: PipelineRequest, response: PipelineResponse) -> bool:
7269
try:
7370
challenge = _update_challenge(request, response)
7471
# azure-identity credentials require an AADv2 scope but the challenge may specify an AADv1 resource

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_internal/async_client_base.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
from typing import TYPE_CHECKING
5+
from typing import Any, Awaitable
66

7+
from azure.core.credentials_async import AsyncTokenCredential
78
from azure.core.pipeline.policies import HttpLoggingPolicy
9+
from azure.core.rest import AsyncHttpResponse, HttpRequest
810
from azure.core.tracing.decorator_async import distributed_trace_async
911

1012
from . import AsyncChallengeAuthPolicy
@@ -13,16 +15,10 @@
1315
from .._generated.aio import KeyVaultClient as _KeyVaultClient
1416
from .._generated import models as _models
1517

16-
if TYPE_CHECKING:
17-
# pylint:disable=unused-import
18-
from typing import Any, Awaitable
19-
from azure.core.credentials_async import AsyncTokenCredential
20-
from azure.core.rest import AsyncHttpResponse, HttpRequest
21-
2218

2319
class AsyncKeyVaultClientBase(object):
2420
# pylint:disable=protected-access
25-
def __init__(self, vault_url: str, credential: "AsyncTokenCredential", **kwargs) -> None:
21+
def __init__(self, vault_url: str, credential: AsyncTokenCredential, **kwargs: Any) -> None:
2622
if not credential:
2723
raise ValueError(
2824
"credential should be an object supporting the AsyncTokenCredential protocol, "
@@ -88,7 +84,9 @@ async def close(self) -> None:
8884
await self._client.close()
8985

9086
@distributed_trace_async
91-
def send_request(self, request: "HttpRequest", *, stream: bool = False, **kwargs) -> "Awaitable[AsyncHttpResponse]":
87+
def send_request(
88+
self, request: HttpRequest, *, stream: bool = False, **kwargs: Any
89+
) -> Awaitable[AsyncHttpResponse]:
9290
"""Runs a network request using the client's existing pipeline.
9391
9492
The request URL can be relative to the vault URL. The service API version used for the request is the same as

sdk/keyvault/azure-keyvault-administration/azure/keyvault/administration/_internal/challenge_auth_policy.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,17 @@
1515
"""
1616

1717
import time
18-
from typing import TYPE_CHECKING
18+
from typing import Any, Optional
1919
from urllib.parse import urlparse
2020

21+
from azure.core.credentials import AccessToken, TokenCredential
2122
from azure.core.exceptions import ServiceRequestError
22-
from azure.core.pipeline import PipelineRequest
23+
from azure.core.pipeline import PipelineRequest, PipelineResponse
2324
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
2425

2526
from .http_challenge import HttpChallenge
2627
from . import http_challenge_cache as ChallengeCache
2728

28-
if TYPE_CHECKING:
29-
from typing import Optional
30-
from azure.core.credentials import AccessToken, TokenCredential
31-
from azure.core.pipeline import PipelineResponse
32-
3329

3430
def _enforce_tls(request: PipelineRequest) -> None:
3531
if not request.http_request.url.lower().startswith("https"):
@@ -38,13 +34,13 @@ def _enforce_tls(request: PipelineRequest) -> None:
3834
)
3935

4036

41-
def _update_challenge(request: PipelineRequest, challenger: "PipelineResponse") -> HttpChallenge:
37+
def _update_challenge(request: PipelineRequest, challenger: PipelineResponse) -> HttpChallenge:
4238
"""Parse challenge from a challenge response, cache it, and return it.
4339
4440
:param request: The pipeline request that prompted the challenge response.
45-
:type request: :class:`~azure.core.pipeline.PipelineRequest`
41+
:type request: ~azure.core.pipeline.PipelineRequest
4642
:param challenger: The pipeline response containing the authentication challenge.
47-
:type challenger: :class:`~azure.core.pipeline.PipelineResponse`
43+
:type challenger: ~azure.core.pipeline.PipelineResponse
4844
4945
:returns: An HttpChallenge object representing the authentication challenge.
5046
:rtype: HttpChallenge
@@ -64,10 +60,10 @@ class ChallengeAuthPolicy(BearerTokenCredentialPolicy):
6460
6561
:param credential: An object which can provide an access token for the vault, such as a credential from
6662
:mod:`azure.identity`
67-
:type credential: :class:`~azure.core.credentials.TokenCredential`
63+
:type credential: ~azure.core.credentials.TokenCredential
6864
"""
6965

70-
def __init__(self, credential: "TokenCredential", *scopes: str, **kwargs) -> None:
66+
def __init__(self, credential: TokenCredential, *scopes: str, **kwargs: Any) -> None:
7167
super(ChallengeAuthPolicy, self).__init__(credential, *scopes, **kwargs)
7268
self._credential = credential
7369
self._token: "Optional[AccessToken]" = None
@@ -96,7 +92,7 @@ def on_request(self, request: PipelineRequest) -> None:
9692
request.http_request.set_json_body(None)
9793
request.http_request.headers["Content-Length"] = "0"
9894

99-
def on_challenge(self, request: PipelineRequest, response: "PipelineResponse") -> bool:
95+
def on_challenge(self, request: PipelineRequest, response: PipelineResponse) -> bool:
10096
try:
10197
challenge = _update_challenge(request, response)
10298
# azure-identity credentials require an AADv2 scope but the challenge may specify an AADv1 resource

0 commit comments

Comments
 (0)