From 9bfc2f49eafb843aafd6ad7af268e81bf77eb885 Mon Sep 17 00:00:00 2001 From: rmorshea Date: Mon, 12 Sep 2022 23:37:02 -0700 Subject: [PATCH] check mypy --- MANIFEST.in | 1 + noxfile.py | 8 ++++++++ pyproject.toml | 6 ++++++ requirements/check-types.txt | 2 ++ src/django_idom/http/urls.py | 2 +- src/django_idom/py.typed | 1 + src/django_idom/utils.py | 19 ++++++++++--------- 7 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 requirements/check-types.txt create mode 100644 src/django_idom/py.typed diff --git a/MANIFEST.in b/MANIFEST.in index 5b9e5fe2..88d8e43b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ +include src/django_idom/py.typed recursive-include src/django_idom/static * recursive-include src/django_idom/templates *.html diff --git a/noxfile.py b/noxfile.py index b28665df..e6426d50 100644 --- a/noxfile.py +++ b/noxfile.py @@ -37,6 +37,7 @@ def test(session: Session) -> None: """Run the complete test suite""" session.install("--upgrade", "pip", "setuptools", "wheel") session.notify("test_suite", posargs=session.posargs) + session.notify("test_types") session.notify("test_style") @@ -60,6 +61,13 @@ def test_suite(session: Session) -> None: session.run("python", "manage.py", "test", *posargs) +@nox.session +def test_types(session: Session) -> None: + install_requirements_file(session, "check-types") + install_requirements_file(session, "pkg-deps") + session.run("mypy", "--show-error-codes", "src/django_idom", "tests/test_app") + + @nox.session def test_style(session: Session) -> None: """Check that style guidelines are being followed""" diff --git a/pyproject.toml b/pyproject.toml index 18a77f5a..74157e0d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,3 +10,9 @@ ensure_newline_before_comments = "True" include_trailing_comma = "True" line_length = 88 lines_after_imports = 2 + +[tool.mypy] +ignore_missing_imports = "True" +warn_unused_configs = "True" +warn_redundant_casts = "True" +warn_unused_ignores = "True" diff --git a/requirements/check-types.txt b/requirements/check-types.txt new file mode 100644 index 00000000..c176075a --- /dev/null +++ b/requirements/check-types.txt @@ -0,0 +1,2 @@ +mypy +django-stubs[compatible-mypy] diff --git a/src/django_idom/http/urls.py b/src/django_idom/http/urls.py index 019a603e..6f7021b7 100644 --- a/src/django_idom/http/urls.py +++ b/src/django_idom/http/urls.py @@ -8,7 +8,7 @@ urlpatterns = [ path( "web_module/", - views.web_modules_file, + views.web_modules_file, # type: ignore[arg-type] name="web_modules", ) ] diff --git a/src/django_idom/py.typed b/src/django_idom/py.typed new file mode 100644 index 00000000..7632ecf7 --- /dev/null +++ b/src/django_idom/py.typed @@ -0,0 +1 @@ +# Marker file for PEP 561 diff --git a/src/django_idom/utils.py b/src/django_idom/utils.py index 81783013..9dd635f8 100644 --- a/src/django_idom/utils.py +++ b/src/django_idom/utils.py @@ -1,10 +1,11 @@ +from __future__ import annotations + import contextlib import logging import os import re from fnmatch import fnmatch from importlib import import_module -from typing import Set from django.template import engines from django.utils.encoding import smart_str @@ -68,9 +69,9 @@ def _get_loaders(self): loaders.append(loader) return loaders - def _get_paths(self) -> Set: + def _get_paths(self) -> set[str]: """Obtains a set of all template directories.""" - paths = set() + paths: set[str] = set() for loader in self._get_loaders(): with contextlib.suppress(ImportError, AttributeError, TypeError): module = import_module(loader.__module__) @@ -80,12 +81,12 @@ def _get_paths(self) -> Set: paths.update(smart_str(origin) for origin in get_template_sources("")) return paths - def _get_templates(self, paths: Set) -> Set: + def _get_templates(self, paths: set[str]) -> set[str]: """Obtains a set of all HTML template paths.""" extensions = [".html"] - templates = set() + templates: set[str] = set() for path in paths: - for root, dirs, files in os.walk(path, followlinks=False): + for root, _, files in os.walk(path, followlinks=False): templates.update( os.path.join(root, name) for name in files @@ -95,9 +96,9 @@ def _get_templates(self, paths: Set) -> Set: return templates - def _get_components(self, templates: Set) -> Set: + def _get_components(self, templates: set[str]) -> set[str]: """Obtains a set of all IDOM components by parsing HTML templates.""" - components = set() + components: set[str] = set() for template in templates: with contextlib.suppress(Exception): with open(template, "r", encoding="utf-8") as template_file: @@ -118,7 +119,7 @@ def _get_components(self, templates: Set) -> Set: ) return components - def _register_components(self, components: Set) -> None: + def _register_components(self, components: set[str]) -> None: """Registers all IDOM components in an iterable.""" for component in components: try: