mypy: Improve typing of bots & API; enforce no_implicit_optional.
This commit is contained in:
		
							parent
							
								
									7dc0703397
								
							
						
					
					
						commit
						55aff6f64b
					
				
					 5 changed files with 10 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -3,7 +3,7 @@ import requests
 | 
			
		|||
import logging
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
from typing import Any, Dict
 | 
			
		||||
from typing import Any, Dict, Optional
 | 
			
		||||
 | 
			
		||||
API_BASE_URL = "https://beta.idonethis.com/api/v2"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +25,7 @@ class UnknownCommandSyntax(Exception):
 | 
			
		|||
class UnspecifiedProblemException(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
def make_API_request(endpoint: str, method: str="GET", body: Dict[str, str]=None) -> Any:
 | 
			
		||||
def make_API_request(endpoint: str, method: str="GET", body: Optional[Dict[str, str]]=None) -> Any:
 | 
			
		||||
    headers = {'Authorization': 'Token ' + api_key}
 | 
			
		||||
    if method == "GET":
 | 
			
		||||
        r = requests.get(API_BASE_URL + endpoint, headers=headers)
 | 
			
		||||
| 
						 | 
				
			
			@ -52,7 +52,7 @@ def api_show_team(hash_id: str) -> Any:
 | 
			
		|||
def api_show_users(hash_id: str) -> Any:
 | 
			
		||||
    return make_API_request("/teams/{}/members".format(hash_id))
 | 
			
		||||
 | 
			
		||||
def api_list_entries(team_id: str=None) -> Any:
 | 
			
		||||
def api_list_entries(team_id: Optional[str]=None) -> Any:
 | 
			
		||||
    if team_id:
 | 
			
		||||
        return make_API_request("/entries?team_id={}".format(team_id))
 | 
			
		||||
    else:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@ import random
 | 
			
		|||
import logging
 | 
			
		||||
import requests
 | 
			
		||||
 | 
			
		||||
from typing import Any, Dict
 | 
			
		||||
from typing import Any, Dict, Optional
 | 
			
		||||
 | 
			
		||||
XKCD_TEMPLATE_URL = 'https://xkcd.com/%s/info.0.json'
 | 
			
		||||
LATEST_XKCD_URL = 'https://xkcd.com/info.0.json'
 | 
			
		||||
| 
						 | 
				
			
			@ -84,7 +84,7 @@ def get_xkcd_bot_response(message: Dict[str, str]) -> str:
 | 
			
		|||
                                           fetched['alt'],
 | 
			
		||||
                                           fetched['img']))
 | 
			
		||||
 | 
			
		||||
def fetch_xkcd_query(mode: int, comic_id: str = None) -> Dict[str, str]:
 | 
			
		||||
def fetch_xkcd_query(mode: int, comic_id: Optional[str]=None) -> Dict[str, str]:
 | 
			
		||||
    try:
 | 
			
		||||
        if mode == XkcdBotCommand.LATEST:  # Fetch the latest comic strip.
 | 
			
		||||
            url = LATEST_XKCD_URL
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
from zulip_bots.bots.yoda.yoda import ServiceUnavailableError
 | 
			
		||||
from zulip_bots.test_lib import BotTestCase
 | 
			
		||||
 | 
			
		||||
from typing import Optional
 | 
			
		||||
 | 
			
		||||
class TestYodaBot(BotTestCase):
 | 
			
		||||
    bot_name = "yoda"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -18,7 +20,7 @@ class TestYodaBot(BotTestCase):
 | 
			
		|||
            @mention-bot You will learn how to speak like me someday.
 | 
			
		||||
            '''
 | 
			
		||||
 | 
			
		||||
    def _test(self, message: str, response: str, fixture: str=None) -> None:
 | 
			
		||||
    def _test(self, message: str, response: str, fixture: Optional[str]=None) -> None:
 | 
			
		||||
        with self.mock_config_info({'api_key': '12345678'}):
 | 
			
		||||
            if fixture is not None:
 | 
			
		||||
                with self.mock_http_conversation(fixture):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue