Skip to content

Commit 9e1fd5b

Browse files
authored
[EventHubs] fix credential mypy (Azure#23548)
* fix mypy * fix pylint * fix sync * Apply suggestions from code review * apply review feedback * move type checking to the front
1 parent 5466d4a commit 9e1fd5b

File tree

6 files changed

+78
-82
lines changed

6 files changed

+78
-82
lines changed

sdk/eventhub/azure-eventhub/azure/eventhub/_client_base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
MGMT_STATUS_DESC,
4343
)
4444

45-
if TYPE_CHECKING:
46-
from azure.core.credentials import TokenCredential
4745

4846
_LOGGER = logging.getLogger(__name__)
4947
_Address = collections.namedtuple("_Address", "hostname path")
@@ -264,9 +262,19 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
264262
return AccessToken(signature, expiry)
265263

266264

265+
if TYPE_CHECKING:
266+
from azure.core.credentials import TokenCredential
267+
CredentialTypes = Union[
268+
AzureSasCredential,
269+
AzureNamedKeyCredential,
270+
EventHubSharedKeyCredential,
271+
TokenCredential
272+
]
273+
274+
267275
class ClientBase(object): # pylint:disable=too-many-instance-attributes
268276
def __init__(self, fully_qualified_namespace, eventhub_name, credential, **kwargs):
269-
# type: (str, str, Union[AzureSasCredential, TokenCredential, AzureNamedKeyCredential], Any) -> None
277+
# type: (str, str, CredentialTypes, Any) -> None
270278
self.eventhub_name = eventhub_name
271279
if not eventhub_name:
272280
raise ValueError("The eventhub name can not be None or empty.")

sdk/eventhub/azure-eventhub/azure/eventhub/_consumer_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
if TYPE_CHECKING:
1717
import datetime
18-
from azure.core.credentials import (
19-
TokenCredential,
20-
AzureSasCredential,
21-
AzureNamedKeyCredential,
22-
)
2318
from typing import ( # pylint: disable=ungrouped-imports
2419
Any,
2520
Union,
@@ -31,6 +26,7 @@
3126
)
3227
from ._eventprocessor.partition_context import PartitionContext
3328
from ._common import EventData
29+
from ._client_base import CredentialTypes
3430

3531
_LOGGER = logging.getLogger(__name__)
3632

@@ -144,7 +140,7 @@ def __init__(
144140
fully_qualified_namespace, # type: str
145141
eventhub_name, # type: str
146142
consumer_group, # type: str
147-
credential, # type: Union[AzureSasCredential, TokenCredential, AzureNamedKeyCredential]
143+
credential, # type: CredentialTypes
148144
**kwargs # type: Any
149145
):
150146
# type: (...) -> None

sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
from ._common import EventDataBatch, EventData
1818

1919
if TYPE_CHECKING:
20-
from azure.core.credentials import (
21-
TokenCredential,
22-
AzureSasCredential,
23-
AzureNamedKeyCredential,
24-
)
20+
from ._client_base import CredentialTypes
2521

2622
SendEventTypes = List[Union[EventData, AmqpAnnotatedMessage]]
2723

@@ -92,7 +88,7 @@ def __init__(
9288
self,
9389
fully_qualified_namespace, # type: str
9490
eventhub_name, # type: str
95-
credential, # type: Union[AzureSasCredential, TokenCredential, AzureNamedKeyCredential]
91+
credential, # type: CredentialTypes
9692
**kwargs # type: Any
9793
):
9894
# type:(...) -> None

sdk/eventhub/azure-eventhub/azure/eventhub/aio/_client_base_async.py

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,70 @@
4646

4747
if TYPE_CHECKING:
4848
from azure.core.credentials_async import AsyncTokenCredential
49+
CredentialTypes = Union[
50+
"EventHubSharedKeyCredential",
51+
AsyncTokenCredential,
52+
AzureSasCredential,
53+
AzureNamedKeyCredential,
54+
]
4955

5056
try:
5157
from typing_extensions import Protocol
5258
except ImportError:
5359
Protocol = object # type: ignore
5460

61+
class AbstractConsumerProducer(Protocol):
62+
@property
63+
def _name(self):
64+
# type: () -> str
65+
"""Name of the consumer or producer"""
66+
67+
@_name.setter
68+
def _name(self, value):
69+
pass
70+
71+
@property
72+
def _client(self):
73+
# type: () -> ClientBaseAsync
74+
"""The instance of EventHubComsumerClient or EventHubProducerClient"""
75+
76+
@_client.setter
77+
def _client(self, value):
78+
pass
79+
80+
@property
81+
def _handler(self):
82+
# type: () -> AMQPClientAsync
83+
"""The instance of SendClientAsync or ReceiveClientAsync"""
84+
85+
@property
86+
def _internal_kwargs(self):
87+
# type: () -> dict
88+
"""The dict with an event loop that users may pass in to wrap sync calls to async API.
89+
It's furthur passed to uamqp APIs
90+
"""
91+
92+
@_internal_kwargs.setter
93+
def _internal_kwargs(self, value):
94+
pass
95+
96+
@property
97+
def running(self):
98+
# type: () -> bool
99+
"""Whether the consumer or producer is running"""
100+
101+
@running.setter
102+
def running(self, value):
103+
pass
104+
105+
def _create_handler(self, auth: authentication.JWTTokenAsync) -> None:
106+
pass
107+
108+
_MIXIN_BASE = AbstractConsumerProducer
109+
else:
110+
_MIXIN_BASE = object
111+
112+
55113
_LOGGER = logging.getLogger(__name__)
56114

57115

@@ -144,9 +202,7 @@ def __init__(
144202
self,
145203
fully_qualified_namespace: str,
146204
eventhub_name: str,
147-
credential: Union[
148-
"AsyncTokenCredential", AzureSasCredential, AzureNamedKeyCredential
149-
],
205+
credential: "CredentialTypes",
150206
**kwargs: Any
151207
) -> None:
152208
self._internal_kwargs = get_dict_with_loop_if_needed(kwargs.get("loop", None))
@@ -376,60 +432,6 @@ async def _close_async(self) -> None:
376432
await self._conn_manager_async.close_connection()
377433

378434

379-
if TYPE_CHECKING:
380-
381-
class AbstractConsumerProducer(Protocol):
382-
@property
383-
def _name(self):
384-
# type: () -> str
385-
"""Name of the consumer or producer"""
386-
387-
@_name.setter
388-
def _name(self, value):
389-
pass
390-
391-
@property
392-
def _client(self):
393-
# type: () -> ClientBaseAsync
394-
"""The instance of EventHubComsumerClient or EventHubProducerClient"""
395-
396-
@_client.setter
397-
def _client(self, value):
398-
pass
399-
400-
@property
401-
def _handler(self):
402-
# type: () -> AMQPClientAsync
403-
"""The instance of SendClientAsync or ReceiveClientAsync"""
404-
405-
@property
406-
def _internal_kwargs(self):
407-
# type: () -> dict
408-
"""The dict with an event loop that users may pass in to wrap sync calls to async API.
409-
It's furthur passed to uamqp APIs
410-
"""
411-
412-
@_internal_kwargs.setter
413-
def _internal_kwargs(self, value):
414-
pass
415-
416-
@property
417-
def running(self):
418-
# type: () -> bool
419-
"""Whether the consumer or producer is running"""
420-
421-
@running.setter
422-
def running(self, value):
423-
pass
424-
425-
def _create_handler(self, auth: authentication.JWTTokenAsync) -> None:
426-
pass
427-
428-
_MIXIN_BASE = AbstractConsumerProducer
429-
else:
430-
_MIXIN_BASE = object
431-
432-
433435
class ConsumerProducerMixin(_MIXIN_BASE):
434436
async def __aenter__(self):
435437
return self

sdk/eventhub/azure-eventhub/azure/eventhub/aio/_consumer_client_async.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
Awaitable,
1919
)
2020

21-
from azure.core.credentials import AzureSasCredential, AzureNamedKeyCredential
22-
2321
from ._eventprocessor.event_processor import EventProcessor
2422
from ._consumer_async import EventHubConsumer
2523
from ._client_base_async import ClientBaseAsync
@@ -28,7 +26,7 @@
2826

2927

3028
if TYPE_CHECKING:
31-
from azure.core.credentials_async import AsyncTokenCredential
29+
from ._client_base_async import CredentialTypes
3230
from uamqp.constants import TransportType
3331
from ._eventprocessor.partition_context import PartitionContext
3432
from ._eventprocessor.checkpoint_store import CheckpointStore
@@ -147,7 +145,7 @@ def __init__(
147145
fully_qualified_namespace: str,
148146
eventhub_name: str,
149147
consumer_group: str,
150-
credential: Union["AsyncTokenCredential", AzureSasCredential, AzureNamedKeyCredential],
148+
credential: "CredentialTypes",
151149
**kwargs
152150
) -> None:
153151
self._checkpoint_store = kwargs.pop("checkpoint_store", None)

sdk/eventhub/azure-eventhub/azure/eventhub/aio/_producer_client_async.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from typing import Any, Union, TYPE_CHECKING, List, Optional, Dict, cast
99
from uamqp import constants
1010

11-
from azure.core.credentials import AzureSasCredential, AzureNamedKeyCredential
12-
1311
from ..exceptions import ConnectError, EventHubError
1412
from ..amqp import AmqpAnnotatedMessage
1513
from ._client_base_async import ClientBaseAsync
@@ -18,7 +16,7 @@
1816
from .._common import EventDataBatch, EventData
1917

2018
if TYPE_CHECKING:
21-
from azure.core.credentials_async import AsyncTokenCredential
19+
from ._client_base_async import CredentialTypes
2220
from uamqp.constants import TransportType # pylint: disable=ungrouped-imports
2321

2422
SendEventTypes = List[Union[EventData, AmqpAnnotatedMessage]]
@@ -90,9 +88,7 @@ def __init__(
9088
self,
9189
fully_qualified_namespace: str,
9290
eventhub_name: str,
93-
credential: Union[
94-
"AsyncTokenCredential", AzureSasCredential, AzureNamedKeyCredential
95-
],
91+
credential: "CredentialTypes",
9692
**kwargs
9793
) -> None:
9894
super(EventHubProducerClient, self).__init__(

0 commit comments

Comments
 (0)