Skip to content

Commit b658f32

Browse files
committed
Improve types
1 parent cad4259 commit b658f32

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/ansiblelint/yaml_utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ruamel.yaml.comments import CommentedMap, CommentedSeq, Format
1818
from ruamel.yaml.composer import ComposerError
1919
from ruamel.yaml.constructor import RoundTripConstructor
20+
from ruamel.yaml.docinfo import Version
2021
from ruamel.yaml.emitter import Emitter, ScalarAnalysis
2122

2223
# Module 'ruamel.yaml' does not explicitly export attribute 'YAML'; implicit reexport disabled
@@ -1028,15 +1029,23 @@ def version(self) -> tuple[int, int] | None:
10281029
return None
10291030

10301031
@version.setter
1031-
def version(self, val: tuple[int, int] | None) -> None:
1032+
def version(self, val: str | tuple[int, int] | list[int] | Version | None) -> None:
10321033
"""Ensure that yaml version uses our default value.
10331034
10341035
The yaml Reader updates this value based on the ``%YAML`` directive in files.
10351036
So, if a file does not include the directive, it sets this to None.
10361037
But, None effectively resets the parsing version to YAML 1.2 (ruamel's default).
10371038
"""
10381039
if val is not None:
1039-
self._yaml_version = val
1040+
if isinstance(val, tuple):
1041+
self._yaml_version = val
1042+
elif isinstance(val, list):
1043+
self._yaml_version = (val[0], val[1])
1044+
elif isinstance(val, Version):
1045+
self._yaml_version = (val.major, val.minor)
1046+
else:
1047+
msg = f"Unsupported argument {val}"
1048+
raise TypeError(msg)
10401049
elif hasattr(self, "_yaml_version_default"):
10411050
self._yaml_version = self._yaml_version_default
10421051
# We do nothing if the object did not have a previous default version defined

0 commit comments

Comments
 (0)