From 22669389e97650701d33ec18fdcde943ef092925 Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Mon, 28 Aug 2023 13:40:51 +0100 Subject: [PATCH 1/2] ci(typecheck): Install and run mypy during pre-merge checks --- .github/workflows/pre-merge-checks.yaml | 7 +++++++ requirements.txt | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pre-merge-checks.yaml b/.github/workflows/pre-merge-checks.yaml index d097a36..ec97d92 100644 --- a/.github/workflows/pre-merge-checks.yaml +++ b/.github/workflows/pre-merge-checks.yaml @@ -32,3 +32,10 @@ jobs: pip install -r requirements.txt - name: Run tests run: pytest + - name: Run typecheck + run: | + mypy adr_viewer \ + --install-types \ + --non-interactive \ + --ignore-missing-imports \ + --pretty diff --git a/requirements.txt b/requirements.txt index b8e6946..9a2efec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ mistune bs4 jinja2 pytest -bottle \ No newline at end of file +bottle +mypy \ No newline at end of file From bbc7d523a1ccfb9664190fd9549326d22b4ce691 Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Mon, 28 Aug 2023 13:57:51 +0100 Subject: [PATCH 2/2] ci(typecheck): Add return types for existing methods --- adr_viewer/__init__.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/adr_viewer/__init__.py b/adr_viewer/__init__.py index ec3cdac..7a547cb 100644 --- a/adr_viewer/__init__.py +++ b/adr_viewer/__init__.py @@ -1,3 +1,5 @@ +from typing import List, Iterator, Optional, Dict + import glob from jinja2.loaders import FileSystemLoader import mistune @@ -8,7 +10,7 @@ from bottle import Bottle, run -def extract_statuses_from_adr(page_object): +def extract_statuses_from_adr(page_object) -> Iterator[str]: status_section = page_object.find('h2', text='Status') if status_section and status_section.nextSibling: @@ -25,20 +27,20 @@ def extract_statuses_from_adr(page_object): continue -def parse_adr_to_config(path): +def parse_adr_to_config(path) -> Optional[Dict]: adr_as_html = mistune.markdown(open(path).read()) soup = BeautifulSoup(adr_as_html, features='html.parser') - status = list(extract_statuses_from_adr(soup)) + statuses = list(extract_statuses_from_adr(soup)) - if any([line.startswith("Amended by") for line in status]): + if any([line.startswith("Amended by") for line in statuses]): status = 'amended' - elif any([line.startswith("Accepted") for line in status]): + elif any([line.startswith("Accepted") for line in statuses]): status = 'accepted' - elif any([line.startswith("Superseded by") for line in status]): + elif any([line.startswith("Superseded by") for line in statuses]): status = 'superseded' - elif any([line.startswith("Proposed") or line.startswith("Pending") for line in status]): + elif any([line.startswith("Proposed") or line.startswith("Pending") for line in statuses]): status = 'pending' else: status = 'unknown' @@ -55,7 +57,7 @@ def parse_adr_to_config(path): return None -def render_html(config, template_dir_override=None): +def render_html(config, template_dir_override=None) -> str: env = Environment( loader=PackageLoader('adr_viewer', 'templates') if template_dir_override is None else FileSystemLoader(template_dir_override), @@ -67,20 +69,20 @@ def render_html(config, template_dir_override=None): return template.render(config=config) -def get_adr_files(path): +def get_adr_files(path) -> List[str]: files = glob.glob(path) files.sort() return files -def run_server(content, port): +def run_server(content, port) -> None: print(f'Starting server at http://localhost:{port}/') app = Bottle() app.route('/', 'GET', lambda: content) run(app, host='localhost', port=port, quiet=True) -def generate_content(path, template_dir_override=None, title=None): +def generate_content(path, template_dir_override=None, title=None) -> str: files = get_adr_files("%s/*.md" % path) @@ -110,7 +112,7 @@ def generate_content(path, template_dir_override=None, title=None): @click.option('--serve', default=False, help='Serve content at http://localhost:8000/', is_flag=True) @click.option('--port', default=8000, help='Change port for the server', show_default=True) @click.option('--template-dir', default=None, help='Template directory.', show_default=True) -def main(adr_path, output, title, serve, port, template_dir): +def main(adr_path, output, title, serve, port, template_dir) -> None: content = generate_content(adr_path, template_dir, title) if serve: