Skip to content
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

Merged
merged 3 commits into from
Oct 16, 2020
Merged

Mypy Compatibilty for EventGrid #14344

merged 3 commits into from
Oct 16, 2020

Conversation

rakshith91
Copy link
Contributor

@rakshith91 rakshith91 commented Oct 7, 2020

Fixes #13883

@ghost ghost added the Event Grid label Oct 7, 2020
@rakshith91
Copy link
Contributor Author

/azp run python - eventgrid - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rakshith91
Copy link
Contributor Author

/azp run python - eventgrid - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rakshith91 rakshith91 marked this pull request as ready for review October 7, 2020 21:09
@rakshith91 rakshith91 changed the title Mypy Compatibilyt for EventGrid Mypy Compatibilty for EventGrid Oct 7, 2020
@@ -35,7 +35,7 @@ def decode_cloud_event(self, cloud_event, **kwargs): # pylint: disable=no-self-u
cloud_event = CloudEvent._from_json(cloud_event, encode) # pylint: disable=protected-access
deserialized_event = CloudEvent._from_generated(cloud_event) # pylint: disable=protected-access
CloudEvent._deserialize_data(deserialized_event, deserialized_event.type) # pylint: disable=protected-access
return deserialized_event
return cast(CloudEvent, deserialized_event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the error that triggered the need for this cast? I would expect the return type of CloudEvent._from_generated to be CloudEvent , and then this cast unecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unnecessary - removed it

@@ -58,7 +58,7 @@ def decode_eventgrid_event(self, eventgrid_event, **kwargs): # pylint: disable=n
eventgrid_event = EventGridEvent._from_json(eventgrid_event, encode) # pylint: disable=protected-access
deserialized_event = EventGridEvent.deserialize(eventgrid_event)
EventGridEvent._deserialize_data(deserialized_event, deserialized_event.event_type) # pylint: disable=protected-access
return deserialized_event
return cast(EventGridEvent, deserialized_event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question

Copy link
Contributor Author

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

Returning Any from function declared to return "EventGridEvent"

because the msrest's deserialize method on line 59 isn't typed

Copy link
Member

@lmazuel lmazuel Oct 16, 2020

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

@KieranBrantnerMagee
Copy link
Member

(to be clear, obviously pending laurent's changes, but if you had an answer to those I didn't see anything else glaring)

@@ -82,7 +86,7 @@ def _get_authentication_policy(credential):
return authentication_policy

def _is_cloud_event(event):
# type: dict -> bool
# type: (dict) -> bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input is actually Any, since since if the input is not a dict, you don't raise an exception, you return False.

This will remove the cast later down the road

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MY rationale was that we expect only a dictionary here - but yes, you are right - changing it


if all(isinstance(e, CloudEvent) for e in events) or all(_is_cloud_event(e) for e in events):
if all(isinstance(e, CloudEvent) for e in events) or all(_is_cloud_event(cast(Dict, e)) for e in events):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast(Dict, e) not necessary once you change _is_cloud_event input to Any

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? What's the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument 1 to "dict" has incompatible type "Union[CloudEvent, EventGridEvent, CustomEvent, Dict[Any, Any]]"; expected "Mapping[Any, Any]"
I can use a cast, but it won't entirely be true - ideally we validate that it's not a cloudevent, eventgrid event by the time we hit this line and they should not be included in the union - afaik, it's a problem with mypy.

@scbedd
Copy link
Member

scbedd commented Oct 8, 2020

/azp run python - eventgrid - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rakshith91 rakshith91 merged commit d2441fc into Azure:master Oct 16, 2020
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Oct 20, 2020
…into add_business_multipage_tests

* 'master' of https://github.com/Azure/azure-sdk-for-python: (24 commits)
  samples updates from other branch (Azure#14598)
  [form recognizer] add multipage business card form (Azure#14613)
  Remove extra newline at the end of the file (Azure#14608)
  Update .gitignore (Azure#14609)
  Enable the link check for link verification step. (Azure#14604)
  Switch the content from array to string. (Azure#14576)
  [Fileshare] Added support for set share properties including access tier (Azure#14355)
  [EventHubs & ServiceBus] add python3.9 support (Azure#14301)
  Add parse_key_vault_certificate_id method and tests (Azure#14518)
  Enable Codespaces. (Azure#14564)
  Failed the anchor links with Uppercase. (Azure#14535)
  Sync eng/common directory with azure-sdk-tools for PR 1091 (Azure#14550)
  Only check the touched markdown files in PR for the Verify link step (Azure#14466)
  [formrecognizer] add logic to set page_number on `ContactNames` field (Azure#14552)
  update deps for multiapi (Azure#14534)
  add sample tests for business cards and model compose (Azure#14515)
  Ma accept str for datetime (Azure#14517)
  Fix anchor links so they work when converting to html
  [formrecognizer] initial selection marks (Azure#14024)
  Mypy Compatibilty for EventGrid (Azure#14344)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Eventgrid Mypy Comaptibility
4 participants