Skip to content

Commit

Permalink
mypy annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmcgruer committed Aug 5, 2020
1 parent 1bbee29 commit b01accf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 11 additions & 0 deletions tools/ci/tc/github_checks_output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
MYPY = False
if MYPY:
# MYPY is set to True when run under Mypy.
from typing import Any
from typing import Dict
from typing import Optional
from typing import Text

class GitHubChecksOutputter(object):
"""Provides a method to output data to be shown in the GitHub Checks UI.
Expand All @@ -8,16 +16,19 @@ class GitHubChecksOutputter(object):
See https://docs.taskcluster.net/docs/reference/integrations/github/checks#custom-text-output-in-checks
"""
def __init__(self, path):
# type: (Text) -> None
self.path = path

def output(self, line):
# type: (Any) -> None
with open(self.path, 'a') as f:
f.write(line)
f.write('\n')


__outputter = None
def get_gh_checks_outputter(kwargs):
# type: (Dict[Text, Text]) -> Optional[GitHubChecksOutputter]
"""Return the outputter for GitHub Checks output, if enabled.
:param kwargs: The arguments passed to the program (to look for the
Expand Down
14 changes: 9 additions & 5 deletions tools/lint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from . import fnmatch
from . import rules
from .. import localpaths
from ..ci.tc.github_checks_output import get_gh_checks_outputter
from ..ci.tc.github_checks_output import get_gh_checks_outputter, GitHubChecksOutputter
from ..gitignore.gitignore import PathFilter
from ..wpt import testfiles
from ..manifest.vcs import walk
Expand All @@ -31,6 +31,7 @@
if MYPY:
# MYPY is set to True when run under Mypy.
from typing import Any
from typing import Callable
from typing import Dict
from typing import IO
from typing import Iterable
Expand Down Expand Up @@ -804,7 +805,7 @@ def check_file_contents(repo_root, path, f):


def output_errors_text(log, errors):
# type: (List[rules.Error]) -> None
# type: (Callable[[Any], None], List[rules.Error]) -> None
for error_type, description, path, line_number in errors:
pos_string = path
if line_number:
Expand All @@ -813,7 +814,7 @@ def output_errors_text(log, errors):


def output_errors_markdown(log, errors):
# type: (List[rules.Error]) -> None
# type: (Callable[[Any], None], List[rules.Error]) -> None
if not errors:
return
heading = """Got lint errors:
Expand All @@ -830,7 +831,7 @@ def output_errors_markdown(log, errors):


def output_errors_json(log, errors):
# type: (List[rules.Error]) -> None
# type: (Callable[[Any], None], List[rules.Error]) -> None
for error_type, error, path, line_number in errors:
# We use 'print' rather than the log function to ensure that the output
# is valid JSON (e.g. with no logger preamble).
Expand All @@ -839,6 +840,7 @@ def output_errors_json(log, errors):


def output_errors_github_checks(outputter, errors, first_reported):
# type: (GitHubChecksOutputter, List[rules.Error], bool) -> None
"""Output errors to the GitHub Checks output markdown format.
:param outputter: the GitHub Checks outputter
Expand Down Expand Up @@ -956,7 +958,7 @@ def main(**kwargs_str):


def lint(repo_root, paths, output_format, ignore_glob=None, github_checks_outputter=None):
# type: (Text, List[Text], Text, Optional[List[Text]]) -> int
# type: (Text, List[Text], Text, Optional[List[Text]], Optional[GitHubChecksOutputter]) -> int
error_count = defaultdict(int) # type: Dict[Text, int]
last = None

Expand Down Expand Up @@ -984,7 +986,9 @@ def process_errors(errors):
if not errors:
return None

assert logger is not None
output_errors(logger.error, errors)

if github_checks_outputter:
first_output = len(error_count) == 0
output_errors_github_checks(github_checks_outputter, errors, first_output)
Expand Down

0 comments on commit b01accf

Please sign in to comment.