-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mypy Compatibilty for EventGrid #14344
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__path__ = __import__('pkgutil').extend_path(__path__, __name__) | ||
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
# license information. | ||
# -------------------------------------------------------------------------- | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import TYPE_CHECKING, cast, Dict, List, Any, Union | ||
|
||
from azure.core.tracing.decorator import distributed_trace | ||
from azure.core.pipeline.policies import ( | ||
|
@@ -27,10 +27,12 @@ | |
from ._generated._event_grid_publisher_client import EventGridPublisherClient as EventGridPublisherClientImpl | ||
from ._policies import CloudEventDistributedTracingPolicy | ||
from ._version import VERSION | ||
from ._generated.models import CloudEvent as InternalCloudEvent, EventGridEvent as InternalEventGridEvent | ||
|
||
if TYPE_CHECKING: | ||
# pylint: disable=unused-import,ungrouped-imports | ||
from typing import Any, Union, Dict, List | ||
from azure.core.credentials import AzureKeyCredential | ||
from ._shared_access_signature_credential import EventGridSharedAccessSignatureCredential | ||
SendType = Union[ | ||
CloudEvent, | ||
EventGridEvent, | ||
|
@@ -42,6 +44,13 @@ | |
List[Dict] | ||
] | ||
|
||
ListEventType = Union[ | ||
List[CloudEvent], | ||
List[EventGridEvent], | ||
List[CustomEvent], | ||
List[Dict] | ||
] | ||
|
||
|
||
class EventGridPublisherClient(object): | ||
"""EventGrid Python Publisher Client. | ||
|
@@ -79,7 +88,7 @@ def _policies(credential, **kwargs): | |
CustomHookPolicy(**kwargs), | ||
NetworkTraceLoggingPolicy(**kwargs), | ||
DistributedTracingPolicy(**kwargs), | ||
CloudEventDistributedTracingPolicy(**kwargs), | ||
CloudEventDistributedTracingPolicy(), | ||
HttpLoggingPolicy(**kwargs) | ||
] | ||
return policies | ||
|
@@ -98,20 +107,24 @@ def send(self, events, **kwargs): | |
:raises: :class:`ValueError`, when events do not follow specified SendType. | ||
""" | ||
if not isinstance(events, list): | ||
events = [events] | ||
events = cast(ListEventType, [events]) | ||
|
||
if all(isinstance(e, CloudEvent) for e in events) or all(_is_cloud_event(e) for e in events): | ||
try: | ||
events = [e._to_generated(**kwargs) for e in events] # pylint: disable=protected-access | ||
events = [cast(CloudEvent, e)._to_generated(**kwargs) for e in events] # pylint: disable=protected-access | ||
except AttributeError: | ||
pass # means it's a dictionary | ||
kwargs.setdefault("content_type", "application/cloudevents-batch+json; charset=utf-8") | ||
self._client.publish_cloud_event_events(self._topic_hostname, events, **kwargs) | ||
self._client.publish_cloud_event_events( | ||
self._topic_hostname, | ||
cast(List[InternalCloudEvent], events), | ||
**kwargs | ||
) | ||
elif all(isinstance(e, EventGridEvent) for e in events) or all(isinstance(e, dict) for e in events): | ||
kwargs.setdefault("content_type", "application/json; charset=utf-8") | ||
self._client.publish_events(self._topic_hostname, events, **kwargs) | ||
self._client.publish_events(self._topic_hostname, cast(List[InternalEventGridEvent], events), **kwargs) | ||
elif all(isinstance(e, CustomEvent) for e in events): | ||
serialized_events = [dict(e) for e in events] | ||
self._client.publish_custom_event_events(self._topic_hostname, serialized_events, **kwargs) | ||
serialized_events = [dict(e) for e in events] # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? What's the error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self._client.publish_custom_event_events(self._topic_hostname, cast(List, serialized_events), **kwargs) | ||
else: | ||
raise ValueError("Event schema is not correct.") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[mypy] | ||
python_version = 3.7 | ||
warn_return_any = True | ||
warn_unused_configs = True | ||
ignore_missing_imports = True | ||
|
||
# Per-module options: | ||
|
||
[mypy-azure.eventgrid._generated.*] | ||
ignore_errors = True | ||
|
||
[mypy-azure.core.*] | ||
ignore_errors = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is necessary unlike above - we get
because the msrest's deserialize method on line 59 isn't typed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then let's fix msrest too :)
Azure/msrest-for-python#226
But ok here, since we should not wait for msrest fix