From bd40e14bfe132a744ba1941fe5884bf4c161e20b Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Mon, 28 Aug 2023 13:57:51 +0100 Subject: [PATCH] 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: