From 3639e92317d876eacb89afe3a4251003f211607c Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 17 Jan 2024 15:28:00 +0100 Subject: [PATCH] pytype flake8 mypy --- .gitignore | 1 + CONTRIBUTING.md | 2 +- dotdrop/action.py | 7 ++++--- dotdrop/cfg_yaml.py | 2 +- dotdrop/dictparser.py | 6 +++--- dotdrop/utils.py | 10 ++++++---- scripts/check-syntax.sh | 42 ++++++++++++++++++++++++++++++++++------- tests-requirements.txt | 7 +++++-- 8 files changed, 56 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index f7b2636..f018412 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ build/ tags env venv +.pytype # coverage stuff .coverage diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3822d93..39df4c1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -209,7 +209,7 @@ for a match with the ignore patterns. Dotdrop is tested with the use of the [tests.sh](/tests.sh) script. -* Test for PEP8 compliance with `pylint`, `pycodestyle` and `pyflakes` (see [check-syntax.sh](/scripts/test-syntax.sh)) +* Test for PEP8 compliance with linters (see [check-syntax.sh](/scripts/test-syntax.sh)) * Test the documentation and links (see [check-doc.sh](/scripts/check-doc.sh)) * Run the unittests in [tests directory](/tests) with pytest (see [check-unittest.sh](/scripts/check-unittests.sh)) * Run the blackbox bash script tests in [tests-ng directory](/tests-ng) (see [check-tests-ng.sh](/scripts/check-tests-ng.sh)) diff --git a/dotdrop/action.py b/dotdrop/action.py index 6d032aa..5f60791 100644 --- a/dotdrop/action.py +++ b/dotdrop/action.py @@ -8,6 +8,7 @@ in dotdrop import subprocess import os +from typing import List # local imports from dotdrop.dictparser import DictParser @@ -17,7 +18,7 @@ from dotdrop.exceptions import UndefinedException class Cmd(DictParser): """A command to execute""" - args = [] + args: List[str] = [] eq_ignore = ('log',) descr = 'command' @@ -32,13 +33,13 @@ class Cmd(DictParser): self.silent = key.startswith('_') def _get_action(self, templater, debug): - action = None + action = '' try: action = templater.generate_string(self.action) except UndefinedException as exc: err = f'undefined variable for {self.descr}: \"{exc}\"' self.log.warn(err) - return False + return action if debug: self.log.dbg(f'{self.descr}:') self.log.dbg(f' - raw \"{self.action}\"') diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 5d2c701..8521852 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -29,7 +29,7 @@ from ruamel.yaml import YAML as yaml try: import tomllib except ImportError: - import tomli as tomllib + import tomli as tomllib # type: ignore import tomli_w # local imports diff --git a/dotdrop/dictparser.py b/dotdrop/dictparser.py index 49017b8..9444b32 100644 --- a/dotdrop/dictparser.py +++ b/dotdrop/dictparser.py @@ -27,9 +27,9 @@ class DictParser: except AttributeError: pass newv = cls._adjust_yaml_keys(tmp) - if not key: - return cls(**newv) - return cls(key=key, **newv) + if key: + newv[key] = key + return cls(**newv) @classmethod def parse_dict(cls, items): diff --git a/dotdrop/utils.py b/dotdrop/utils.py index 05c6f27..79bae69 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -473,7 +473,11 @@ def get_module_from_path(path): importlib.machinery.SOURCE_SUFFIXES.append('') # import module spec = importlib.util.spec_from_file_location(module_name, path) + if not spec: + return None mod = importlib.util.module_from_spec(spec) + if not mod: + return None spec.loader.exec_module(mod) return mod @@ -507,8 +511,7 @@ def dependencies_met(): name = 'docopt' err = f'missing python module \"{name}\"' try: - from docopt import docopt - assert docopt + from docopt import docopt # noqa # pylint: disable=W0611 except ImportError as exc: raise UnmetDependency(err) from exc @@ -525,8 +528,7 @@ def dependencies_met(): name = 'ruamel.yaml' err = f'missing python module \"{name}\"' try: - from ruamel.yaml import YAML - assert YAML + from ruamel.yaml import YAML # noqa # pylint: disable=W0611 except ImportError as exc: raise UnmetDependency(err) from exc diff --git a/scripts/check-syntax.sh b/scripts/check-syntax.sh index 019620c..4aaa3f6 100755 --- a/scripts/check-syntax.sh +++ b/scripts/check-syntax.sh @@ -28,12 +28,26 @@ fi echo "=> pycodestyle version:" pycodestyle --version -if ! which pyflakes >/dev/null 2>&1; then - echo "Install pyflakes" +if ! which flake8 >/dev/null 2>&1; then + echo "Install flake8" exit 1 fi -echo "=> pyflakes version:" -pyflakes --version +echo "=> flake8 version:" +flake8 --version + +if ! which pytype >/dev/null 2>&1; then + echo "Install pytype" + exit 1 +fi +echo "=> pytype version:" +pytype --version + +if ! which mypy >/dev/null 2>&1; then + echo "Install mypy" + exit 1 +fi +echo "=> mypy version:" +mypy --version # checking for TODO/FIXME echo "--------------------------------------" @@ -62,10 +76,10 @@ echo "checking dotdrop with pycodestyle" pycodestyle --ignore=W503,W504 dotdrop/ pycodestyle scripts/ -# pyflakes tests +# flake8 tests echo "------------------------------" -echo "checking dotdrop with pyflakes" -pyflakes dotdrop/ +echo "checking dotdrop with flake8" +flake8 dotdrop/ # pylint echo "----------------------------" @@ -90,6 +104,20 @@ pylint \ --disable=R0904 \ dotdrop/ +# pytype +echo "----------------------------" +echo "checking dotdrop with pytype" +pytype dotdrop/ + +# mypy +echo "----------------------------" +echo "checking dotdrop with mypy" +# --strict +mypy \ + --ignore-missing-imports \ + --allow-redefinition \ + dotdrop/ + # check shell scripts # SC2002: Useless cat # SC2126: Consider using grep -c instead of grep|wc -l diff --git a/tests-requirements.txt b/tests-requirements.txt index d496cec..b5cb37e 100644 --- a/tests-requirements.txt +++ b/tests-requirements.txt @@ -2,8 +2,11 @@ pycodestyle; python_version > '3.5' pytest; python_version > '3.5' coverage; python_version > '3.5' coveralls; python_version > '3.5' -pyflakes; python_version > '3.5' +flake8; python_version > '3.5' pylint; python_version > '3.5' halo; python_version > '3.5' distro; python_version > '3.5' -urllib3; python_version > '3.5' \ No newline at end of file +urllib3; python_version > '3.5' +pytype; python_version > '3.0' +mypy; python_version > '3.0' +types-requests; python_version > '3.0' \ No newline at end of file