diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index dba32478da..6b22168185 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -326,7 +326,7 @@ def content(self) -> str: """Retrieve file content, from internal cache or disk.""" if self._content is None: self._populate_content_cache_from_disk() - return cast(str, self._content) + return cast("str", self._content) @content.setter def content(self, value: str) -> None: diff --git a/src/ansiblelint/rules/__init__.py b/src/ansiblelint/rules/__init__.py index a77899d7dd..847791554c 100644 --- a/src/ansiblelint/rules/__init__.py +++ b/src/ansiblelint/rules/__init__.py @@ -327,9 +327,9 @@ def transform(self, match, lintable, data): # The cast() calls tell mypy what types we expect. # Essentially this does: if isinstance(segment, str): - target = cast(MutableMapping[str, Any], target)[segment] + target = cast("MutableMapping[str, Any]", target)[segment] elif isinstance(segment, int): - target = cast(MutableSequence[Any], target)[segment] + target = cast("MutableSequence[Any]", target)[segment] return target diff --git a/src/ansiblelint/transformer.py b/src/ansiblelint/transformer.py index ed2cd93917..9d0057e42d 100644 --- a/src/ansiblelint/transformer.py +++ b/src/ansiblelint/transformer.py @@ -150,14 +150,14 @@ def _do_transforms( _logger.debug("%s %s", self.FIX_NA_MSG, match_id) continue if self.write_set != {"all"}: - rule = cast(AnsibleLintRule, match.rule) + rule = cast("AnsibleLintRule", match.rule) rule_definition = set(rule.tags) rule_definition.add(rule.id) if rule_definition.isdisjoint(self.write_set): _logger.debug("%s %s", self.FIX_NE_MSG, match_id) continue if file_is_yaml and not match.yaml_path: - data = cast(CommentedMap | CommentedSeq, data) + data = cast("CommentedMap | CommentedSeq", data) if match.match_type == "play": match.yaml_path = get_path_to_play(file, match.lineno, data) elif match.task or file.kind in ( diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 44a88cbbe8..56a557a26c 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -247,12 +247,12 @@ def _nested_items_path( # convert_to_tuples_type = Callable[[], Iterator[tuple[str | int, Any]]] if isinstance(data_collection, dict): convert_data_collection_to_tuples = cast( - Callable[[], Iterator[tuple[str | int, Any]]], + "Callable[[], Iterator[tuple[str | int, Any]]]", functools.partial(data_collection.items), ) elif isinstance(data_collection, list): convert_data_collection_to_tuples = cast( - Callable[[], Iterator[tuple[str | int, Any]]], + "Callable[[], Iterator[tuple[str | int, Any]]]", functools.partial(enumerate, data_collection), ) else: @@ -914,11 +914,11 @@ def __init__( # pylint: disable=too-many-arguments self.explicit_start: bool = config["explicit_start"] # type: ignore[assignment] self.explicit_end: bool = config["explicit_end"] # type: ignore[assignment] self.width: int = config["width"] # type: ignore[assignment] - indent_sequences: bool = cast(bool, config["indent_sequences"]) - preferred_quote: str = cast(str, config["preferred_quote"]) # either ' or " + indent_sequences: bool = cast("bool", config["indent_sequences"]) + preferred_quote: str = cast("str", config["preferred_quote"]) # either ' or " - min_spaces_inside: int = cast(int, config["min_spaces_inside"]) - max_spaces_inside: int = cast(int, config["max_spaces_inside"]) + min_spaces_inside: int = cast("int", config["min_spaces_inside"]) + max_spaces_inside: int = cast("int", config["max_spaces_inside"]) self.default_flow_style = False self.compact_seq_seq = True # type: ignore[assignment] # dash after dash @@ -996,7 +996,7 @@ def _defaults_from_yamllint_config() -> dict[str, bool | int | str]: elif quote_type == "double": config["preferred_quote"] = '"' - return cast(dict[str, bool | int | str], config) + return cast("dict[str, bool | int | str]", config) @property def version(self) -> tuple[int, int] | None: @@ -1095,17 +1095,17 @@ def _predict_indent_length(self, parent_path: list[str | int], key: Any) -> int: indent += self.sequence_dash_offset elif isinstance(parent_key, int): # next level is a sequence - indent += cast(int, self.sequence_indent) + indent += cast("int", self.sequence_indent) elif isinstance(parent_key, str): # next level is a map - indent += cast(int, self.map_indent) + indent += cast("int", self.map_indent) if isinstance(key, int) and indent == 0: # flow map is an item in a root-level sequence indent += self.sequence_dash_offset elif isinstance(key, int) and indent > 0: # flow map is in a sequence - indent += cast(int, self.sequence_indent) + indent += cast("int", self.sequence_indent) elif isinstance(key, str): # flow map is in a map indent += len(key + ": ") diff --git a/test/test_yaml_utils.py b/test/test_yaml_utils.py index ae225ee179..46ea506eb0 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -1029,7 +1029,7 @@ def test_document_start( yaml = ansiblelint.yaml_utils.FormattedYAML( version=yaml_version, - config=cast(dict[str, bool | int | str], config), + config=cast("dict[str, bool | int | str]", config), ) assert ( yaml.dumps(yaml.load(_SINGLE_QUOTE_WITHOUT_INDENTS)).startswith("---")