From 1a7cc17bb3a86c2fa27ca8d5a0d5749f60e281f7 Mon Sep 17 00:00:00 2001 From: Miguel BarĂ£o Date: Wed, 17 Jul 2019 17:51:10 +0100 Subject: [PATCH] more type annotations. adds [mypy] section in mypy.ini --- aprendizations/learnapp.py | 6 ++++-- aprendizations/main.py | 2 +- aprendizations/models.py | 6 +++++- aprendizations/serve.py | 2 +- aprendizations/student.py | 2 +- aprendizations/tools.py | 2 +- mypy.ini | 9 +++++---- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/aprendizations/learnapp.py b/aprendizations/learnapp.py index cc444ee..6e55f4c 100644 --- a/aprendizations/learnapp.py +++ b/aprendizations/learnapp.py @@ -138,8 +138,10 @@ class LearnApp(object): else: name, hashed_pw = found - pw_ok = await loop.run_in_executor(None, bcrypt.checkpw, - pw.encode('utf-8'), hashed_pw) + pw_ok: bool = await loop.run_in_executor(None, + bcrypt.checkpw, + pw.encode('utf-8'), + hashed_pw) if pw_ok: if uid in self.online: diff --git a/aprendizations/main.py b/aprendizations/main.py index a51d062..d622a0f 100644 --- a/aprendizations/main.py +++ b/aprendizations/main.py @@ -62,7 +62,7 @@ def parse_cmdline_arguments(): # ---------------------------------------------------------------------------- -def get_logger_config(debug: bool = False) -> Dict[str, Any]: +def get_logger_config(debug: bool = False) -> Any: if debug: filename, level = 'logger-debug.yaml', 'DEBUG' else: diff --git a/aprendizations/models.py b/aprendizations/models.py index df59b3c..74069b5 100644 --- a/aprendizations/models.py +++ b/aprendizations/models.py @@ -1,4 +1,7 @@ +# python standard library +from typing import Any + # third party libraries from sqlalchemy import Column, ForeignKey, Integer, Float, String from sqlalchemy.ext.declarative import declarative_base @@ -7,7 +10,8 @@ from sqlalchemy.orm import relationship # =========================================================================== # Declare ORM -Base = declarative_base() +# FIXME Any is a workaround for mypy static type checking (see https://github.com/python/mypy/issues/6372) +Base: Any = declarative_base() # --------------------------------------------------------------------------- diff --git a/aprendizations/serve.py b/aprendizations/serve.py index 839e334..a16813a 100644 --- a/aprendizations/serve.py +++ b/aprendizations/serve.py @@ -8,7 +8,7 @@ import mimetypes from os import path import signal import sys -from typing import List, Optional, Tuple, Union +from typing import List, Optional, Union import uuid # third party libraries diff --git a/aprendizations/student.py b/aprendizations/student.py index 7be78d5..a815700 100644 --- a/aprendizations/student.py +++ b/aprendizations/student.py @@ -99,7 +99,7 @@ class StudentState(object): questions = t['questions'][:k] logger.debug(f'selected questions: {", ".join(questions)}') - self.questions = [await self.factory[ref].generate_async() + self.questions: List[Question] = [await self.factory[ref].generate_async() for ref in questions] n = len(self.questions) diff --git a/aprendizations/tools.py b/aprendizations/tools.py index 7f97156..960e49f 100644 --- a/aprendizations/tools.py +++ b/aprendizations/tools.py @@ -127,7 +127,7 @@ markdown = MarkdownWithMath(HighlightRenderer(escape=True)) def md_to_html(text: str, strip_p_tag: bool = False) -> str: - md = markdown(text) + md: str = markdown(text) if strip_p_tag and md.startswith('

') and md.endswith('

'): return md[3:-5] else: diff --git a/mypy.ini b/mypy.ini index 4a0325d..2f66baf 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,3 +1,8 @@ +[mypy] +python_version = 3.7 +warn_return_any = True +warn_unused_configs = True + [mypy-sqlalchemy.*] ignore_missing_imports = True @@ -12,7 +17,3 @@ ignore_missing_imports = True [mypy-mistune.*] ignore_missing_imports = True - -[mypy-setuptools.*] -ignore_missing_imports = True - -- libgit2 0.21.2