From a19416cda6c8b4f489a0502cf9a509861a045e85 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 8 Dec 2020 10:05:18 +0000 Subject: [PATCH] Adopt pylint --- .pre-commit-config.yaml | 16 ++++++++++++---- .pylintrc | 21 +++++++++++++++++++++ src/tox_ansible/ansible/scenario.py | 3 +-- src/tox_ansible/hooks.py | 9 ++++----- src/tox_ansible/matrix/__init__.py | 2 +- src/tox_ansible/tox_base_case.py | 5 ++++- src/tox_ansible/tox_helper.py | 2 ++ src/tox_ansible/tox_lint_case.py | 7 +++---- src/tox_ansible/tox_molecule_case.py | 6 +++--- tests/test_ansible.py | 2 +- tests/test_by_driver.py | 6 +----- tests/test_by_scenario.py | 9 ++------- tests/test_everything.py | 16 ++++------------ tests/test_filter.py | 9 ++------- tests/test_options.py | 6 +----- tests/test_tox_lint_case.py | 7 ++----- tests/util.py | 2 +- 17 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 .pylintrc diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 00942a2e..63bc1249 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - - repo: https://github.com/pre-commit/mirrors-isort - rev: v5.5.3 + - repo: https://github.com/PyCQA/isort + rev: 5.6.4 hooks: - id: isort - repo: https://github.com/python/black.git @@ -9,7 +9,7 @@ repos: - id: black language_version: python3 - repo: https://github.com/pre-commit/pre-commit-hooks.git - rev: v3.2.0 + rev: v3.3.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace @@ -19,7 +19,7 @@ repos: - id: check-merge-conflict - id: debug-statements - repo: https://gitlab.com/pycqa/flake8.git - rev: 3.8.3 + rev: 3.8.4 hooks: - id: flake8 - repo: https://github.com/adrienverge/yamllint.git @@ -29,3 +29,11 @@ repos: files: \.(yaml|yml)$ types: [file, yaml] entry: yamllint --strict + - repo: https://github.com/pre-commit/mirrors-pylint + rev: v2.6.0 + hooks: + - id: pylint + additional_dependencies: + - pytest + - pyyaml + - tox diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 00000000..57c79e6a --- /dev/null +++ b/.pylintrc @@ -0,0 +1,21 @@ +[MESSAGES CONTROL] + +disable = + # TODO(ssbarnea): remove temporary skips adding during initial adoption: + invalid-name, + missing-class-docstring, + missing-function-docstring, + missing-module-docstring, + no-member, + no-self-use, + redefined-outer-name, + too-few-public-methods, + unused-argument, + useless-object-inheritance, + +[REPORTS] +output-format = colorized + +[IMPORTS] +preferred-modules = + ansible:, diff --git a/src/tox_ansible/ansible/scenario.py b/src/tox_ansible/ansible/scenario.py index a2dbbbeb..8dffc87a 100644 --- a/src/tox_ansible/ansible/scenario.py +++ b/src/tox_ansible/ansible/scenario.py @@ -48,5 +48,4 @@ def requirements(self): requirements_txt = path.join(self.directory, "requirements.txt") if path.isfile(requirements_txt): return requirements_txt - else: - return None + return None diff --git a/src/tox_ansible/hooks.py b/src/tox_ansible/hooks.py index 85c08309..3fb457fc 100644 --- a/src/tox_ansible/hooks.py +++ b/src/tox_ansible/hooks.py @@ -5,11 +5,10 @@ from tox import hookimpl -from tox_ansible import tox_helper as tox_helper - -from .ansible import Ansible -from .filter import Filter -from .options import ( +from tox_ansible import tox_helper +from tox_ansible.ansible import Ansible +from tox_ansible.filter import Filter +from tox_ansible.options import ( DRIVER_ENV_NAME, DRIVER_OPTION_NAME, SCENARIO_ENV_NAME, diff --git a/src/tox_ansible/matrix/__init__.py b/src/tox_ansible/matrix/__init__.py index 244f5dec..4a23b8a6 100644 --- a/src/tox_ansible/matrix/__init__.py +++ b/src/tox_ansible/matrix/__init__.py @@ -3,7 +3,7 @@ def __init__(self): self.axes = [] def add_axis(self, axis): - """Add an exansion axis to this matrix. Axes can be found in the + """Add an extension axis to this matrix. Axes can be found in the axes.py file and are subclasses of the MatrixAxisBase class. :param axis: An expansion axis to add to this matrix.""" diff --git a/src/tox_ansible/tox_base_case.py b/src/tox_ansible/tox_base_case.py index b09ab11b..fc3a6012 100644 --- a/src/tox_ansible/tox_base_case.py +++ b/src/tox_ansible/tox_base_case.py @@ -25,9 +25,12 @@ def _expand(self, name): :param name: An additional field to be added to the name factors :return: A copy of this object with the additional name factor""" if hasattr(self, "scenario"): + # pylint: disable=too-many-function-args copy = self.__class__(self.scenario, [name] + self._name_parts) else: - copy = self.__class__(self._cases, [name] + self._name_parts) + copy = self.__class__( # pylint: disable=too-many-function-args + self._cases, [name] + self._name_parts + ) copy.python = self.python copy.ansible = self.ansible return copy diff --git a/src/tox_ansible/tox_helper.py b/src/tox_ansible/tox_helper.py index f7f52023..02ca7a22 100644 --- a/src/tox_ansible/tox_helper.py +++ b/src/tox_ansible/tox_helper.py @@ -33,6 +33,7 @@ def get_reader(self, section, prefix=None): :param section: Config section name to read from :param prefix: Any applicable prefix to the ini section name. Default None""" + # pylint: disable=protected-access reader = SectionReader(section, self.config._cfg, prefix=prefix) distshare_default = join(str(self.config.homedir), ".tox", "distshare") reader.addsubstitutions( @@ -88,6 +89,7 @@ def add_envconfigs(self, tox_cases, options): self.config.ansible_envlist = [] for tox_case in tox_cases: section = testenvprefix + tox_case.get_name() + # pylint: disable=protected-access config = make_envconfig( self.config, tox_case.get_name(), section, reader._subs, self.config ) diff --git a/src/tox_ansible/tox_lint_case.py b/src/tox_ansible/tox_lint_case.py index 4a10ef4e..4edd155e 100644 --- a/src/tox_ansible/tox_lint_case.py +++ b/src/tox_ansible/tox_lint_case.py @@ -7,11 +7,11 @@ class ToxLintCase(ToxBaseCase): - def __init__(self, cases, name_parts=[]): + def __init__(self, cases, name_parts=None): self._cases = copy(cases) - self._name_parts = name_parts + self._name_parts = name_parts or [] self._config = Tox() - super(ToxLintCase, self).__init__() + super().__init__() def get_commands(self, options): cmds = [] @@ -47,4 +47,3 @@ def get_name(self): @property def description(self): return "Auto-generated environment to run: molecule lint on all scenarios" - "scenarios" diff --git a/src/tox_ansible/tox_molecule_case.py b/src/tox_ansible/tox_molecule_case.py index a6fcb22a..4a35eecd 100644 --- a/src/tox_ansible/tox_molecule_case.py +++ b/src/tox_ansible/tox_molecule_case.py @@ -23,7 +23,7 @@ class ToxMoleculeCase(ToxBaseCase): """Represents a generalized Test Case for an Ansible structure.""" - def __init__(self, scenario, name_parts=[]): + def __init__(self, scenario, name_parts=None): """Create the base test case. :param scenario: The scenario that this test case should run""" @@ -36,8 +36,8 @@ def __init__(self, scenario, name_parts=[]): "pytest", "testinfra", ] - self._name_parts = name_parts - super(ToxMoleculeCase, self).__init__() + self._name_parts = name_parts or [] + super().__init__() def get_commands(self, options): """Get the commands that this scenario should execute. diff --git a/tests/test_ansible.py b/tests/test_ansible.py index 42547e40..d5d0c073 100644 --- a/tests/test_ansible.py +++ b/tests/test_ansible.py @@ -6,5 +6,5 @@ @pytest.mark.parametrize("scenarios,expected", [([1], True), ([], False)]) def test_with_scenarios(mocker, scenarios, expected): ansible = Ansible() - ansible._scenarios = scenarios + ansible._scenarios = scenarios # pylint: disable=protected-access assert ansible.is_ansible == expected diff --git a/tests/test_by_driver.py b/tests/test_by_driver.py index 124efb76..c6055456 100644 --- a/tests/test_by_driver.py +++ b/tests/test_by_driver.py @@ -1,12 +1,8 @@ from unittest import TestCase +from unittest.mock import Mock from tox_ansible.filter.by_driver import ByDriver -try: - from unittest.mock import Mock -except ImportError: - from mock import Mock - class TestByDriver(TestCase): def test_no_case(self): diff --git a/tests/test_by_scenario.py b/tests/test_by_scenario.py index e8830284..c32055e6 100644 --- a/tests/test_by_scenario.py +++ b/tests/test_by_scenario.py @@ -1,11 +1,6 @@ -from unittest import TestCase - -try: - from unittest.mock import Mock -except ImportError: - from mock import Mock - from collections import namedtuple +from unittest import TestCase +from unittest.mock import Mock from tox_ansible.filter.by_scenario import ByScenario diff --git a/tests/test_everything.py b/tests/test_everything.py index 1db2e8d7..112a5ea7 100644 --- a/tests/test_everything.py +++ b/tests/test_everything.py @@ -3,15 +3,10 @@ import contextlib import os import subprocess +from unittest.mock import patch import pytest -try: - from unittest.mock import patch -except ImportError: - from mock import patch - - EXPECTED = { "tests/fixtures/collection": "\n".join( [ @@ -71,8 +66,6 @@ def cd(directory): os.chdir(directory) try: yield - except Exception: - raise finally: os.chdir(cwd) @@ -81,13 +74,12 @@ def run_tox(args, capture): tox = ["tox"] tox.extend(args) try: - subprocess.run(tox) + subprocess.run(tox, check=True) except SystemExit as s: if s.code != 0: raise - finally: - outs = capture.readouterr() - return outs.out.strip() + outs = capture.readouterr() + return outs.out.strip() @pytest.mark.parametrize( diff --git a/tests/test_filter.py b/tests/test_filter.py index 253cca0c..383fe728 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -1,12 +1,7 @@ # For rennamed/moved assert(Count|Item)Equal -from unittest import TestCase - -try: - from unittest.mock import Mock -except ImportError: - from mock import Mock - from collections import namedtuple +from unittest import TestCase +from unittest.mock import Mock from tox_ansible.filter import Filter diff --git a/tests/test_options.py b/tests/test_options.py index 3ee7553a..00c3058c 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -1,12 +1,8 @@ from unittest import TestCase +from unittest.mock import Mock from tox_ansible.options import Options -try: - from unittest.mock import Mock -except ImportError: - from mock import Mock - class TestOptions(TestCase): def test_do_filter_scenario(self): diff --git a/tests/test_tox_lint_case.py b/tests/test_tox_lint_case.py index 5e6e3795..47bc8e14 100644 --- a/tests/test_tox_lint_case.py +++ b/tests/test_tox_lint_case.py @@ -1,11 +1,8 @@ +from unittest.mock import Mock + from tox_ansible.ansible.scenario import Scenario from tox_ansible.tox_lint_case import ToxLintCase -try: - from unittest.mock import Mock -except ImportError: - from mock import Mock - def test_names_are_correct(mocker): tc = ToxLintCase([]) diff --git a/tests/util.py b/tests/util.py index e5daa406..d06c5950 100644 --- a/tests/util.py +++ b/tests/util.py @@ -97,4 +97,4 @@ def tox_envlist(self, arguments=None): self.assertEqual(returncode, 0, stderr) - return [env for env in stdout.strip().splitlines()] + return stdout.strip().splitlines()