chore: Enforce Mypy for non-tests (#15757)

Co-authored-by: John Bodley <john.bodley@airbnb.com>
This commit is contained in:
John Bodley
2021-07-21 11:46:43 -07:00
committed by GitHub
parent d26254099e
commit ab4e3b9bf9
20 changed files with 134 additions and 74 deletions

View File

@@ -33,7 +33,7 @@ Example:
./cancel_github_workflows.py 1024 --include-last
"""
import os
from typing import Iterable, List, Optional, Union
from typing import Any, Dict, Iterable, Iterator, List, Optional, Union
import click
import requests
@@ -45,7 +45,9 @@ github_token = os.environ.get("GITHUB_TOKEN")
github_repo = os.environ.get("GITHUB_REPOSITORY", "apache/superset")
def request(method: Literal["GET", "POST", "DELETE", "PUT"], endpoint: str, **kwargs):
def request(
method: Literal["GET", "POST", "DELETE", "PUT"], endpoint: str, **kwargs: Any
) -> Dict[str, Any]:
resp = requests.request(
method,
f"https://api.github.com/{endpoint.lstrip('/')}",
@@ -57,10 +59,14 @@ def request(method: Literal["GET", "POST", "DELETE", "PUT"], endpoint: str, **kw
return resp
def list_runs(repo: str, params=None):
def list_runs(
repo: str, params: Optional[Dict[str, str]] = None,
) -> Iterator[Dict[str, Any]]:
"""List all github workflow runs.
Returns:
An iterator that will iterate through all pages of matching runs."""
if params is None:
params = {}
page = 1
total_count = 10000
while page * 100 < total_count:
@@ -75,11 +81,11 @@ def list_runs(repo: str, params=None):
page += 1
def cancel_run(repo: str, run_id: Union[str, int]):
def cancel_run(repo: str, run_id: Union[str, int]) -> Dict[str, Any]:
return request("POST", f"/repos/{repo}/actions/runs/{run_id}/cancel")
def get_pull_request(repo: str, pull_number: Union[str, int]):
def get_pull_request(repo: str, pull_number: Union[str, int]) -> Dict[str, Any]:
return request("GET", f"/repos/{repo}/pulls/{pull_number}")
@@ -89,7 +95,7 @@ def get_runs(
user: Optional[str] = None,
statuses: Iterable[str] = ("queued", "in_progress"),
events: Iterable[str] = ("pull_request", "push"),
):
) -> List[Dict[str, Any]]:
"""Get workflow runs associated with the given branch"""
return [
item
@@ -101,7 +107,7 @@ def get_runs(
]
def print_commit(commit, branch):
def print_commit(commit: Dict[str, Any], branch: str) -> None:
"""Print out commit message for verification"""
indented_message = " \n".join(commit["message"].split("\n"))
date_str = (
@@ -151,7 +157,7 @@ def cancel_github_workflows(
event: List[str],
include_last: bool,
include_running: bool,
):
) -> None:
"""Cancel running or queued GitHub workflows by branch or pull request ID"""
if not github_token:
raise ClickException("Please provide GITHUB_TOKEN as an env variable")
@@ -231,7 +237,7 @@ def cancel_github_workflows(
try:
print(f"[{entry['status']}] {entry['name']}", end="\r")
cancel_run(repo, entry["id"])
print(f"[Cancled] {entry['name']} ")
print(f"[Canceled] {entry['name']} ")
except ClickException as error:
print(f"[Error: {error.message}] {entry['name']} ")
print("")