Skip to content

Commit f26024c

Browse files
author
Yalin Li
authored
[ACR] Update method type hints to Python3 syntax (Azure#28117)
1 parent b8bada6 commit f26024c

15 files changed

+471
-371
lines changed

sdk/containerregistry/azure-containerregistry/azure/containerregistry/_anonymous_exchange_client.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) Microsoft Corporation.
44
# Licensed under the MIT License.
55
# ------------------------------------
6-
from typing import Any, Union, Optional
6+
from typing import Optional
77

88
from ._exchange_client import ExchangeClientAuthenticationPolicy
99
from ._generated import ContainerRegistry
@@ -18,12 +18,11 @@ class AnonymousACRExchangeClient(object):
1818
:param endpoint: Azure Container Registry endpoint
1919
:type endpoint: str
2020
:keyword api_version: API Version. The default value is "2021-07-01". Note that overriding this default value
21-
may result in unsupported behavior.
21+
may result in unsupported behavior.
2222
:paramtype api_version: str
2323
"""
2424

25-
def __init__(self, endpoint, **kwargs): # pylint: disable=missing-client-constructor-parameter-credential
26-
# type: (str, Any) -> None
25+
def __init__(self, endpoint: str, **kwargs) -> None: # pylint: disable=missing-client-constructor-parameter-credential
2726
if not endpoint.startswith("https://") and not endpoint.startswith("http://"):
2827
endpoint = "https://" + endpoint
2928
self._endpoint = endpoint
@@ -35,8 +34,7 @@ def __init__(self, endpoint, **kwargs): # pylint: disable=missing-client-constr
3534
**kwargs
3635
)
3736

38-
def get_acr_access_token(self, challenge, **kwargs):
39-
# type: (str, Any) -> Optional[str]
37+
def get_acr_access_token(self, challenge: str, **kwargs) -> Optional[str]:
4038
parsed_challenge = _parse_challenge(challenge)
4139
return self.exchange_refresh_token_for_access_token(
4240
"",
@@ -46,8 +44,9 @@ def get_acr_access_token(self, challenge, **kwargs):
4644
**kwargs
4745
)
4846

49-
def exchange_refresh_token_for_access_token(self, refresh_token, service, scope, grant_type, **kwargs):
50-
# type: (str, str, str, Union[str, TokenGrantType], Any) -> Optional[str]
47+
def exchange_refresh_token_for_access_token(
48+
self, refresh_token: str, service: str, scope: str, grant_type: str, **kwargs
49+
) -> Optional[str]:
5150
access_token = self._client.authentication.exchange_acr_refresh_token_for_acr_access_token(
5251
service=service, scope=scope, refresh_token=refresh_token, grant_type=grant_type, **kwargs
5352
)
@@ -60,8 +59,7 @@ def __enter__(self):
6059
def __exit__(self, *args):
6160
self._client.__exit__(*args)
6261

63-
def close(self):
64-
# type: () -> None
62+
def close(self) -> None:
6563
"""Close sockets opened by the client.
6664
Calling this method is unnecessary when using the client as a context manager.
6765
"""

sdk/containerregistry/azure-containerregistry/azure/containerregistry/_authentication_policy.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,41 @@
44
# Licensed under the MIT License.
55
# ------------------------------------
66

7-
from typing import TYPE_CHECKING, Any, Union, Optional
7+
from typing import Union, Optional
88
from io import SEEK_SET, UnsupportedOperation
99

10+
from azure.core.credentials import TokenCredential
11+
from azure.core.pipeline import PipelineRequest, PipelineResponse
1012
from azure.core.pipeline.policies import HTTPPolicy
1113

1214
from ._anonymous_exchange_client import AnonymousACRExchangeClient
1315
from ._exchange_client import ACRExchangeClient
1416
from ._helpers import _enforce_https
1517

16-
if TYPE_CHECKING:
17-
from azure.core.credentials import TokenCredential
18-
from azure.core.pipeline import PipelineRequest, PipelineResponse
19-
2018

2119
class ContainerRegistryChallengePolicy(HTTPPolicy):
2220
"""Authentication policy for ACR which accepts a challenge"""
2321

24-
def __init__(self, credential, endpoint, **kwargs):
25-
# type: (Optional[TokenCredential], str, **Any) -> None
22+
def __init__(self, credential: Optional[TokenCredential], endpoint: str, **kwargs) -> None:
2623
super(ContainerRegistryChallengePolicy, self).__init__()
2724
self._credential = credential
2825
if self._credential is None:
2926
self._exchange_client = AnonymousACRExchangeClient(endpoint, **kwargs) # type: Union[AnonymousACRExchangeClient, ACRExchangeClient] # pylint: disable=line-too-long
3027
else:
3128
self._exchange_client = ACRExchangeClient(endpoint, self._credential, **kwargs)
3229

33-
def on_request(self, request):
34-
# type: (PipelineRequest) -> None
30+
def on_request(self, request: PipelineRequest) -> None:
3531
"""Called before the policy sends a request.
3632
The base implementation authorizes the request with a bearer token.
33+
3734
:param ~azure.core.pipeline.PipelineRequest request: the request
3835
"""
3936
# Future caching implementation will be included here
4037
pass # pylint: disable=unnecessary-pass
4138

42-
def send(self, request):
43-
# type: (PipelineRequest) -> PipelineResponse
39+
def send(self, request: PipelineRequest) -> PipelineResponse:
4440
"""Authorizes a request with a bearer token, possibly handling an authentication challenge
41+
4542
:param ~azure.core.pipeline.PipelineRequest request: the request
4643
"""
4744
_enforce_https(request)
@@ -65,16 +62,16 @@ def send(self, request):
6562

6663
return response
6764

68-
def on_challenge(self, request, response, challenge):
69-
# type: (PipelineRequest, PipelineResponse, str) -> bool
65+
def on_challenge(self, request: PipelineRequest, response: PipelineResponse, challenge: str) -> bool:
7066
"""Authorize request according to an authentication challenge
7167
This method is called when the resource provider responds 401 with a WWW-Authenticate header.
68+
7269
:param ~azure.core.pipeline.PipelineRequest request: the request which elicited an authentication challenge
7370
:param ~azure.core.pipeline.PipelineResponse response: the resource provider's response
7471
:param str challenge: response's WWW-Authenticate header, unparsed. It may contain multiple challenges.
7572
:returns: a bool indicating whether the policy should send the request
7673
"""
77-
# pylint:disable=unused-argument,no-self-use
74+
# pylint:disable=unused-argument, no-self-use
7875

7976
access_token = self._exchange_client.get_acr_access_token(challenge)
8077
if access_token is not None:

sdk/containerregistry/azure-containerregistry/azure/containerregistry/_base_client.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
# Licensed under the MIT License.
55
# ------------------------------------
66
from enum import Enum
7-
from typing import TYPE_CHECKING, Any, Optional
7+
from typing import Optional
88

99
from azure.core import CaseInsensitiveEnumMeta
10+
from azure.core.credentials import TokenCredential
1011
from azure.core.pipeline.transport import HttpTransport
1112

1213
from ._authentication_policy import ContainerRegistryChallengePolicy
1314
from ._generated import ContainerRegistry
1415
from ._user_agent import USER_AGENT
1516

16-
if TYPE_CHECKING:
17-
from azure.core.credentials import TokenCredential
18-
19-
2017

2118
class ContainerRegistryApiVersion(str, Enum, metaclass=CaseInsensitiveEnumMeta):
2219
"""Container Registry API version supported by this package"""
@@ -33,12 +30,11 @@ class ContainerRegistryBaseClient(object):
3330
:keyword credential_scopes: URL for credential authentication if different from the default
3431
:paramtype credential_scopes: List[str]
3532
:keyword api_version: API Version. The default value is "2021-07-01". Note that overriding this default value
36-
may result in unsupported behavior.
33+
may result in unsupported behavior.
3734
:paramtype api_version: str
3835
"""
3936

40-
def __init__(self, endpoint, credential, **kwargs):
41-
# type: (str, Optional[TokenCredential], Any) -> None
37+
def __init__(self, endpoint: str, credential: Optional[TokenCredential], **kwargs) -> None:
4238
self._auth_policy = ContainerRegistryChallengePolicy(credential, endpoint, **kwargs)
4339
self._client = ContainerRegistry(
4440
credential=credential,
@@ -57,8 +53,7 @@ def __exit__(self, *args):
5753
self._auth_policy.__exit__(*args)
5854
self._client.__exit__(*args)
5955

60-
def close(self):
61-
# type: () -> None
56+
def close(self) -> None:
6257
"""Close sockets opened by the client.
6358
Calling this method is unnecessary when using the client as a context manager.
6459
"""

0 commit comments

Comments
 (0)