Skip to content

Commit

Permalink
Run mypy in strict mode (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed May 22, 2021
1 parent 2b9146a commit 64fc18a
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ repos:
hooks:
- id: mypy
# empty args needed in order to match mypy cli behavior
args: []
args: ["--strict"]
additional_dependencies:
- ansible-base
- Sphinx>=3.1.2
Expand Down
5 changes: 4 additions & 1 deletion src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@

def get_rule_config(rule_id: str) -> Dict[str, Any]:
"""Get configurations for the rule ``rule_id``."""
return options.rules.get(rule_id, dict())
rule_config = options.rules.get(rule_id, dict())
if not isinstance(rule_config, dict):
raise RuntimeError("Invalid rule config for %s: %s" % (rule_id, rule_config))
return rule_config


@lru_cache()
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/prerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def _get_galaxy_role_ns(galaxy_infos: Dict[str, Any]) -> str:
role_namespace = ""
else:
role_namespace = f"{role_namespace}."
if not isinstance(role_namespace, str):
raise RuntimeError("Role namespace must be string, not %s" % role_namespace)
return role_namespace


Expand Down Expand Up @@ -251,7 +253,7 @@ def _install_galaxy_role() -> None:
# despite documentation stating that is_file() reports true for symlinks,
# it appears that is_dir() reports true instead, so we rely on exits().
target = pathlib.Path(options.project_dir).absolute()
if not link_path.exists() or os.readlink(link_path) != target:
if not link_path.exists() or os.readlink(link_path) != str(target):
if link_path.exists():
link_path.unlink()
link_path.symlink_to(target, target_is_directory=True)
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/VariableNamingRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def matchyaml(self, file: Lintable) -> List["MatchError"]:
results: List["MatchError"] = []
meta_data: Dict[str, Any] = {}

if file.kind == "vars":
if str(file.kind) == "vars":
meta_data = parse_yaml_from_file(str(file.path))
for key in meta_data.keys():
if self.is_invalid_variable_name(key):
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/YamllintRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def matchyaml(self, file: Lintable) -> List["MatchError"]:
"""Return matches found for a specific YAML text."""
matches: List["MatchError"] = []
filtered_matches: List["MatchError"] = []
if file.base_kind != 'text/yaml':
if str(file.base_kind) != 'text/yaml':
return matches

if YamllintRule.config:
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def matchtasks(self, file: Lintable) -> List[MatchError]:
if (
not self.matchtask
or file.kind not in ['handlers', 'tasks', 'playbook']
or file.base_kind != 'text/yaml'
or str(file.base_kind) != 'text/yaml'
):
return matches

Expand Down Expand Up @@ -143,7 +143,7 @@ def matchtasks(self, file: Lintable) -> List[MatchError]:

def matchyaml(self, file: Lintable) -> List[MatchError]:
matches: List[MatchError] = []
if not self.matchplay or file.base_kind != 'text/yaml':
if not self.matchplay or str(file.base_kind) != 'text/yaml':
return matches

yaml = ansiblelint.utils.parse_yaml_linenumbers(file)
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/skip_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from itertools import product
from typing import TYPE_CHECKING, Any, Generator, List, Optional, Sequence

import ruamel.yaml
# Module 'ruamel.yaml' does not explicitly export attribute 'YAML'; implicit reexport disabled
from ruamel.yaml import YAML # type: ignore

from ansiblelint.config import used_old_tags
from ansiblelint.constants import RENAMED_TAGS
Expand Down Expand Up @@ -86,7 +87,7 @@ def load_data(file_text: str) -> Any:
:param file_text: raw text to parse
:return: Parsed yaml
"""
yaml = ruamel.yaml.YAML()
yaml = YAML()
return yaml.load(file_text)


Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ def parse_yaml_linenumbers(lintable: Lintable) -> AnsibleBaseYAMLObject:
def compose_node(parent: yaml.nodes.Node, index: int) -> 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) # type: ignore
node = Composer.compose_node(loader, parent, index)
if not isinstance(node, yaml.nodes.Node):
raise RuntimeError("Unexpected yaml data.")
setattr(node, '__line__', line + 1)
Expand Down
4 changes: 2 additions & 2 deletions test/TestMatchError.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,6 @@ def test_matcherror_compare_with_dummy_sentinel(
"""Check that MatchError comparison runs other types fallbacks."""
dummy_obj = DummySentinelTestObject()
# NOTE: This assertion abuses the CPython property to cache short string
# NOTE: objects because the identity check is more presice and we don't
# NOTE: objects because the identity check is more precise and we don't
# NOTE: want extra operator protocol methods to influence the test.
assert operation(MatchError("foo"), dummy_obj) is expected_value
assert operation(MatchError("foo"), dummy_obj) is expected_value # type: ignore
4 changes: 2 additions & 2 deletions test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ def mockreturn(options: Namespace) -> Dict[str, Any]:
lintable_expected = Lintable(path, kind=kind)
assert lintable_detected == lintable_expected

monkeypatch.setattr(utils, 'discover_lintables', mockreturn)
result = utils.discover_lintables(options)
monkeypatch.setattr(file_utils, 'discover_lintables', mockreturn)
result = file_utils.discover_lintables(options)
# get_lintable could return additional files and we only care to see
# that the given file is among the returned list.
assert lintable_detected.name in result
Expand Down

0 comments on commit 64fc18a

Please sign in to comment.