Skip to content

Commit

Permalink
More type
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Mar 27, 2021
1 parent facedd9 commit d9ce687
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 43 deletions.
11 changes: 9 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ repos:
- flake8-docstrings>=1.5.0
- flake8-pytest-style>=1.2.2
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.800
rev: v0.812
hooks:
- id: mypy
# empty args needed in order to match mypy cli behavior
Expand All @@ -85,8 +85,15 @@ repos:
- Sphinx>=3.1.2
- enrich
- flaky
- yamllint
- pytest
- types-PyYAML
- wcmatch
- yamllint
exclude: >
(?x)^(
test/.*|
plugins/.*
)$
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.6.0
hooks:
Expand Down
4 changes: 3 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import re
import sys

from typing import List

os.environ["NO_COLOR"] = "1"
pytest_plugins = ["ansiblelint.testing.fixtures"]


def pytest_cmdline_preparse(args):
def pytest_cmdline_preparse(args: List[str]) -> None:
"""Pytest hook."""
# disable xdist when called with -k args (filtering)
# https://stackoverflow.com/questions/66407583/how-to-disable-pytest-xdist-only-when-pytest-is-called-with-filters
Expand Down
2 changes: 1 addition & 1 deletion docs/rules_table_generator_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _nodes_from_rst(
),
node=node,
)
return node.children
return node.children # type: ignore


class AnsibleLintDefaultRulesDirective(SphinxDirective):
Expand Down
14 changes: 13 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ python_version = 3.6
color_output = True
error_summary = True
disallow_untyped_calls = True
; warn_redundant_casts=True
disallow_untyped_defs = True
; disallow_any_generics = True
; disallow_any_unimported = True
; warn_redundant_casts = True
; warn_return_any = True
; warn_unused_configs = True

[mypy-ansiblelint.*]
ignore_missing_imports = True

[mypy-ansiblelint.testing.*]
disallow_untyped_defs = False

[mypy-test.*]
disallow_untyped_defs = False


# 3rd party ignores
[mypy-ansible]
ignore_missing_imports = True
Expand Down
8 changes: 5 additions & 3 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
# THE SOFTWARE.
"""Command line implementation."""

from argparse import Namespace
import errno
import logging
import os
import pathlib
import subprocess
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, List, Iterator, Optional


from enrich.console import should_do_markup

Expand Down Expand Up @@ -108,7 +110,7 @@ def initialize_options(arguments: Optional[List[str]] = None) -> None:
options.configured = True


def report_outcome(result: "LintResult", options, mark_as_success=False) -> int:
def report_outcome(result: "LintResult", options: Namespace, mark_as_success: bool = False) -> int:
"""Display information about how to skip found rules.
Returns exit code, 2 if errors were found, 0 when only warnings were found.
Expand Down Expand Up @@ -249,7 +251,7 @@ def main(argv: List[str] = None) -> int:


@contextmanager
def _previous_revision():
def _previous_revision() -> Iterator[None]:
"""Create or update a temporary workdir containing the previous revision."""
worktree_dir = ".cache/old-rev"
revision = subprocess.run(
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ansiblelint import formatters
from ansiblelint.color import console
from ansiblelint.errors import MatchError

if TYPE_CHECKING:
from argparse import Namespace
Expand All @@ -26,7 +27,7 @@ def __init__(self, options: "Namespace"):
formatter_factory = choose_formatter_factory(options)
self.formatter = formatter_factory(options.cwd, options.display_relative_path)

def render_matches(self, matches: List) -> None:
def render_matches(self, matches: List[MatchError]) -> None:
"""Display given matches."""
if isinstance(self.formatter, formatters.CodeclimateJSONFormatter):
# If formatter CodeclimateJSONFormatter is chosen,
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
console_stderr = Console(**console_options_stderr)


def reconfigure(new_options: Dict[str, Any]):
def reconfigure(new_options: Dict[str, Any]) -> None:
"""Reconfigure console options."""
global console_options # pylint: disable=global-statement
global console_stderr # pylint: disable=global-statement
Expand All @@ -39,6 +39,6 @@ def reconfigure(new_options: Dict[str, Any]):
console_stderr.__dict__ = tmp_console.__dict__


def render_yaml(text: str):
def render_yaml(text: str) -> Syntax:
"""Colorize YAMl for nice display."""
return Syntax(text, 'yaml', theme="ansi_dark")
4 changes: 2 additions & 2 deletions src/ansiblelint/rules/AnsibleSyntaxCheckRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import subprocess
import sys
from typing import List
from typing import List, Any

from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
from ansiblelint.config import options
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_empty_playbook() -> None:
assert result[0].message == "Empty playbook, nothing to do"
assert len(result) == 1

def test_extra_vars_passed_to_command(config_options) -> None:
def test_extra_vars_passed_to_command(config_options: Any) -> None:
"""Validate `extra-vars` are passed to syntax check command."""
config_options.extra_vars = {
'foo': 'bar',
Expand Down
5 changes: 4 additions & 1 deletion src/ansiblelint/rules/NoSameOwnerRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def handle_unarchive(task: Any) -> bool:

import pytest

from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports

@pytest.mark.parametrize(
Expand All @@ -122,7 +123,9 @@ def handle_unarchive(task: Any) -> bool:
),
),
)
def test_no_same_owner_rule(default_rules_collection, test_file, failures) -> None:
def test_no_same_owner_rule(
default_rules_collection: RulesCollection, test_file: str, failures: int
) -> None:
"""Test rule matches."""
results = Runner(test_file, rules=default_rules_collection).run()
assert len(results) == failures
Expand Down
8 changes: 0 additions & 8 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ def matchtasks(self, file: Lintable) -> List[MatchError]:
matches.append(m)
return matches

@staticmethod
def _matchplay_linenumber(play, optional_linenumber):
try:
(linenumber,) = optional_linenumber
except ValueError:
linenumber = play[ansiblelint.utils.LINE_NUMBER_KEY]
return linenumber

def matchyaml(self, file: Lintable) -> List[MatchError]:
matches: List[MatchError] = []
if not self.matchplay or file.base_kind != 'text/yaml':
Expand Down
16 changes: 9 additions & 7 deletions src/ansiblelint/skip_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_rule_skips_from_line(line: str) -> List[str]:

def append_skipped_rules(
pyyaml_data: "AnsibleBaseYAMLObject", lintable: Lintable
) -> Sequence[Any]:
) -> "AnsibleBaseYAMLObject":
"""Append 'skipped_rules' to individual tasks or single metadata block.
For a file, uses 2nd parser (ruamel.yaml) to pull comments out of
Expand All @@ -65,12 +65,14 @@ def append_skipped_rules(
:returns: original pyyaml_data altered with a 'skipped_rules' list added
to individual tasks, or added to the single metadata block.
"""
try:
yaml_skip = _append_skipped_rules(pyyaml_data, lintable)
except RuntimeError:
# Notify user of skip error, do not stop, do not change exit code
_logger.error('Error trying to append skipped rules', exc_info=True)
return pyyaml_data
if isinstance(pyyaml_data, Sequence):
try:
yaml_skip = _append_skipped_rules(pyyaml_data, lintable)
except RuntimeError:
# Notify user of skip error, do not stop, do not change exit code
_logger.error('Error trying to append skipped rules', exc_info=True)
return pyyaml_data

return yaml_skip


Expand Down
21 changes: 7 additions & 14 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _playbook_items(pb_data: dict) -> ItemsView:
return [item for play in pb_data if play for item in play.items()]


def _set_collections_basedir(basedir: str):
def _set_collections_basedir(basedir: str) -> None:
# Sets the playbook directory as playbook_paths for the collection loader
try:
# Ansible 2.10+
Expand Down Expand Up @@ -480,16 +480,7 @@ def _look_for_role_files(basedir: str, role: str, main='main') -> List[Lintable]
return results


def rolename(filepath):
idx = filepath.find('roles/')
if idx < 0:
return ''
role = filepath[idx + 6 :]
role = role[: role.find('/')]
return role


def _kv_to_dict(v):
def _kv_to_dict(v: str) -> Dict[str, Any]:
(command, args, kwargs) = tokenize(v)
return dict(__ansible_module__=command, __ansible_arguments__=args, **kwargs)

Expand Down Expand Up @@ -560,7 +551,7 @@ def normalize_task_v1(task): # noqa: C901
for (k, v) in task.items():
if k in VALID_KEYS or k.startswith('with_'):
if k in ('local_action', 'action'):
if not isinstance(v, dict):
if isinstance(v, str):
v = _kv_to_dict(v)
v['__ansible_arguments__'] = v.get('__ansible_arguments__', list())
result['action'] = v
Expand Down Expand Up @@ -712,11 +703,13 @@ def parse_yaml_linenumbers(lintable: Lintable) -> AnsibleBaseYAMLObject:
The line numbers are stored in each node's LINE_NUMBER_KEY key.
"""

def compose_node(parent, index):
def compose_node(parent, index) -> yaml.nodes.Node:
# the line number where the previous token has ended (plus empty lines)
line = loader.line
node = Composer.compose_node(loader, parent, index)
node = Composer.compose_node(loader, parent, index) # type: ignore
node.__line__ = line + 1
if not isinstance(node, yaml.nodes.Node):
raise RuntimeError("Unexpected yaml data.")
return node

def construct_mapping(node, deep=False):
Expand Down

0 comments on commit d9ce687

Please sign in to comment.