diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b5757483..2300f57f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,6 +44,11 @@ repos: additional_dependencies: - eslint@7.13.0 args: [src] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.790 + hooks: + - id: mypy + files: ^(src/pytest_html|testing) - repo: local hooks: - id: rst diff --git a/setup.cfg b/setup.cfg index 526aeb28..9629125b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,19 @@ [bdist_wheel] universal = 0 + +[mypy] +check_untyped_defs = True +disallow_any_generics = True +disallow_incomplete_defs = True +disallow_untyped_calls = True +disallow_untyped_decorators = True +disallow_untyped_defs = True +ignore_missing_imports = True +no_implicit_optional = True +no_implicit_reexport = True +show_error_codes = True +strict_equality = True +warn_redundant_casts = True +warn_return_any = True +warn_unreachable = True +warn_unused_configs = True diff --git a/src/pytest_html/__init__.py b/src/pytest_html/__init__.py index 4dcb81e2..e6f80ef5 100644 --- a/src/pytest_html/__init__.py +++ b/src/pytest_html/__init__.py @@ -1,5 +1,5 @@ try: - from . import __version + from . import __version # type: ignore __version__ = __version.version except ImportError: diff --git a/src/pytest_html/extras.py b/src/pytest_html/extras.py index f64a0eb5..abf7e4c8 100644 --- a/src/pytest_html/extras.py +++ b/src/pytest_html/extras.py @@ -1,6 +1,8 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +from typing import Dict +from typing import Optional FORMAT_HTML = "html" FORMAT_IMAGE = "image" @@ -10,7 +12,13 @@ FORMAT_VIDEO = "video" -def extra(content, format_type, name=None, mime_type=None, extension=None): +def extra( + content: str, + format_type: str, + name: Optional[str] = None, + mime_type: Optional[str] = None, + extension: Optional[str] = None, +) -> Dict[str, Optional[str]]: return { "name": name, "format_type": format_type, @@ -20,41 +28,51 @@ def extra(content, format_type, name=None, mime_type=None, extension=None): } -def html(content): +def html(content: str) -> Dict[str, Optional[str]]: return extra(content, FORMAT_HTML) -def image(content, name="Image", mime_type="image/png", extension="png"): +def image( + content: str, + name: str = "Image", + mime_type: str = "image/png", + extension: str = "png", +) -> Dict[str, Optional[str]]: return extra(content, FORMAT_IMAGE, name, mime_type, extension) -def png(content, name="Image"): +def png(content: str, name: str = "Image") -> Dict[str, Optional[str]]: return image(content, name, mime_type="image/png", extension="png") -def jpg(content, name="Image"): +def jpg(content: str, name: str = "Image") -> Dict[str, Optional[str]]: return image(content, name, mime_type="image/jpeg", extension="jpg") -def svg(content, name="Image"): +def svg(content: str, name: str = "Image") -> Dict[str, Optional[str]]: return image(content, name, mime_type="image/svg+xml", extension="svg") -def json(content, name="JSON"): +def json(content: str, name: str = "JSON") -> Dict[str, Optional[str]]: return extra(content, FORMAT_JSON, name, "application/json", "json") -def text(content, name="Text"): +def text(content: str, name: str = "Text") -> Dict[str, Optional[str]]: return extra(content, FORMAT_TEXT, name, "text/plain", "txt") -def url(content, name="URL"): +def url(content: str, name: str = "URL") -> Dict[str, Optional[str]]: return extra(content, FORMAT_URL, name) -def video(content, name="Video", mime_type="video/mp4", extension="mp4"): +def video( + content: str, + name: str = "Video", + mime_type: str = "video/mp4", + extension: str = "mp4", +) -> Dict[str, Optional[str]]: return extra(content, FORMAT_VIDEO, name, mime_type, extension) -def mp4(content, name="Video"): +def mp4(content: str, name: str = "Video") -> Dict[str, Optional[str]]: return video(content, name) diff --git a/src/pytest_html/hooks.py b/src/pytest_html/hooks.py index 7b75120b..da1daf4f 100644 --- a/src/pytest_html/hooks.py +++ b/src/pytest_html/hooks.py @@ -1,6 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +# type: ignore def pytest_html_report_title(report): diff --git a/src/pytest_html/html_report.py b/src/pytest_html/html_report.py index e1aa3973..ee5106ca 100644 --- a/src/pytest_html/html_report.py +++ b/src/pytest_html/html_report.py @@ -1,3 +1,4 @@ +# type: ignore import bisect import datetime import json diff --git a/src/pytest_html/outcome.py b/src/pytest_html/outcome.py index 1bb71acd..8056bb72 100644 --- a/src/pytest_html/outcome.py +++ b/src/pytest_html/outcome.py @@ -1,8 +1,17 @@ +from typing import Optional + from py.xml import html class Outcome: - def __init__(self, outcome, total=0, label=None, test_result=None, class_html=None): + def __init__( + self, + outcome: str, + total: int = 0, + label: Optional[str] = None, + test_result: Optional[str] = None, + class_html: Optional[str] = None, + ) -> None: self.outcome = outcome self.label = label or outcome self.class_html = class_html or outcome @@ -12,7 +21,7 @@ def __init__(self, outcome, total=0, label=None, test_result=None, class_html=No self.generate_checkbox() self.generate_summary_item() - def generate_checkbox(self): + def generate_checkbox(self) -> None: checkbox_kwargs = {"data-test-result": self.test_result.lower()} if self.total == 0: checkbox_kwargs["disabled"] = "true" @@ -27,7 +36,7 @@ def generate_checkbox(self): **checkbox_kwargs, ) - def generate_summary_item(self): + def generate_summary_item(self) -> None: self.summary_item = html.span( f"{self.total} {self.label}", class_=self.class_html ) diff --git a/src/pytest_html/plugin.py b/src/pytest_html/plugin.py index 6a0ef7a4..4d68bb14 100644 --- a/src/pytest_html/plugin.py +++ b/src/pytest_html/plugin.py @@ -1,6 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +# type: ignore import os import pytest diff --git a/src/pytest_html/result.py b/src/pytest_html/result.py index f791e6d7..9748ac61 100644 --- a/src/pytest_html/result.py +++ b/src/pytest_html/result.py @@ -1,3 +1,4 @@ +# type: ignore import json import os import re diff --git a/src/pytest_html/util.py b/src/pytest_html/util.py index 37259ec7..ae65c4a6 100644 --- a/src/pytest_html/util.py +++ b/src/pytest_html/util.py @@ -1,12 +1,14 @@ import importlib from functools import lru_cache +from types import ModuleType +from typing import Optional @lru_cache() -def ansi_support(): +def ansi_support() -> Optional[ModuleType]: try: # from ansi2html import Ansi2HTMLConverter, style # NOQA return importlib.import_module("ansi2html") except ImportError: # ansi2html is not installed - pass + return None diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index b2d23af9..b163891a 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -1,6 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +# type: ignore import builtins import json import os