mypy: Fix type annotation style in webhook walkthrough.

This commit is contained in:
Tim Abbott 2017-11-04 19:44:12 -07:00
parent 969cc506d2
commit 671c3dea66
1 changed files with 14 additions and 22 deletions

View File

@ -71,12 +71,10 @@ from typing import Dict, Any, Iterable, Optional, Text
@api_key_only_webhook_view('HelloWorld') @api_key_only_webhook_view('HelloWorld')
@has_request_variables @has_request_variables
def api_helloworld_webhook(request, user_profile, def api_helloworld_webhook(request: HttpRequest, user_profile: UserProfile,
payload=REQ(argument_type='body'), payload: Dict[str, Iterable[Dict[str, Any]]]=REQ(argument_type='body'),
stream=REQ(default='test'), stream: Text=REQ(default='test'),
topic=REQ(default='Hello World')): topic: Text=REQ(default='Hello World')) -> HttpResponse:
# type: (HttpRequest, UserProfile, Dict[str, Iterable[Dict[str, Any]]], Text, Optional[Text]) -> HttpResponse
# construct the body of the message # construct the body of the message
body = 'Hello! I am happy to be here! :smile:' body = 'Hello! I am happy to be here! :smile:'
@ -226,8 +224,7 @@ class HelloWorldHookTests(WebhookTestCase):
FIXTURE_DIR_NAME = 'helloworld' FIXTURE_DIR_NAME = 'helloworld'
# Note: Include a test function per each distinct message condition your integration supports # Note: Include a test function per each distinct message condition your integration supports
def test_hello_message(self): def test_hello_message(self) -> None:
# type: () -> None
expected_subject = u"Hello World"; expected_subject = u"Hello World";
expected_message = u"Hello! I am happy to be here! :smile: \nThe Wikipedia featured article for today is **[Marilyn Monroe](https://en.wikipedia.org/wiki/Marilyn_Monroe)**"; expected_message = u"Hello! I am happy to be here! :smile: \nThe Wikipedia featured article for today is **[Marilyn Monroe](https://en.wikipedia.org/wiki/Marilyn_Monroe)**";
@ -235,7 +232,7 @@ class HelloWorldHookTests(WebhookTestCase):
self.send_and_test_stream_message('hello', expected_subject, expected_message, self.send_and_test_stream_message('hello', expected_subject, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name): def get_body(self, fixture_name: Text) -> Text:
# type: (Text) -> Text # type: (Text) -> Text
return self.fixture_data("helloworld", fixture_name, file_type="json") return self.fixture_data("helloworld", fixture_name, file_type="json")
@ -269,8 +266,7 @@ World` webhook, we would add another test function to `HelloWorldHookTests`
class called something like `test_goodbye_message`: class called something like `test_goodbye_message`:
``` ```
def test_goodbye_message(self): def test_goodbye_message(self) -> None:
# type: () -> None
expected_subject = u"Hello World"; expected_subject = u"Hello World";
expected_message = u"Hello! I am happy to be here! :smile:\nThe Wikipedia featured article for today is **[Goodbye](https://en.wikipedia.org/wiki/Goodbye)**"; expected_message = u"Hello! I am happy to be here! :smile:\nThe Wikipedia featured article for today is **[Goodbye](https://en.wikipedia.org/wiki/Goodbye)**";
@ -413,9 +409,7 @@ rather than call the usual helper function.
Here is an example from the WordPress webhook: Here is an example from the WordPress webhook:
``` ```
def test_unknown_action_no_data(self): def test_unknown_action_no_data(self) -> None:
# type: () -> None
# Mimic send_and_test_stream_message() to manually execute a negative test. # Mimic send_and_test_stream_message() to manually execute a negative test.
# Otherwise its call to send_json_payload() would assert on the non-success # Otherwise its call to send_json_payload() would assert on the non-success
# we are testing. The value of result is the error message the webhook should # we are testing. The value of result is the error message the webhook should
@ -459,9 +453,10 @@ For example, here is the definition of a webhook function that gets both `stream
and `topic` from the query parameters: and `topic` from the query parameters:
``` ```
def api_querytest_webhook(request, user_profile, client, def api_querytest_webhook(request: HttpRequest, user_profile: UserProfile,
payload=REQ(argument_type='body'), stream=REQ(default='test'), payload: str=REQ(argument_type='body'),
topic=REQ(default='Default Alert')): stream: str=REQ(default='test'),
topic: str=REQ(default='Default Alert')):
``` ```
In actual use, you might configure the 3rd party service to call your Zulip In actual use, you might configure the 3rd party service to call your Zulip
@ -486,9 +481,7 @@ class QuerytestHookTests(WebhookTestCase):
URL_TEMPLATE = "/api/v1/external/querytest?api_key={api_key}&stream={stream}" URL_TEMPLATE = "/api/v1/external/querytest?api_key={api_key}&stream={stream}"
FIXTURE_DIR_NAME = 'querytest' FIXTURE_DIR_NAME = 'querytest'
def test_querytest_test_one(self): def test_querytest_test_one(self) -> None:
# type: () -> None
# construct the URL used for this test # construct the URL used for this test
self.TOPIC = u"Query Test" self.TOPIC = u"Query Test"
self.url = self.build_webhook_url(topic=self.TOPIC) self.url = self.build_webhook_url(topic=self.TOPIC)
@ -500,8 +493,7 @@ class QuerytestHookTests(WebhookTestCase):
self.send_and_test_stream_message('test_one', expected_subject, expected_message, self.send_and_test_stream_message('test_one', expected_subject, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name): def get_body(self, fixture_name: Text) -> Text:
# type: (Text) -> Text
return self.fixture_data("querytest", fixture_name, file_type="json") return self.fixture_data("querytest", fixture_name, file_type="json")
``` ```