From 56e4a2aea687ba0ab26ee71e5630a353a4e5fe92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 21 Jul 2019 00:35:22 +0300 Subject: [PATCH] Fix util.ruamel_yaml type errors (#25338) --- homeassistant/util/ruamel_yaml.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/homeassistant/util/ruamel_yaml.py b/homeassistant/util/ruamel_yaml.py index 0659e3d8054..ffdc4bf6b84 100644 --- a/homeassistant/util/ruamel_yaml.py +++ b/homeassistant/util/ruamel_yaml.py @@ -3,7 +3,7 @@ import logging import os from os import O_CREAT, O_TRUNC, O_WRONLY, stat_result from collections import OrderedDict -from typing import Union, List, Dict +from typing import Union, List, Dict, Optional import ruamel.yaml from ruamel.yaml import YAML @@ -22,6 +22,8 @@ JSON_TYPE = Union[List, Dict, str] # pylint: disable=invalid-name class ExtSafeConstructor(SafeConstructor): """Extended SafeConstructor.""" + name = None # type: Optional[str] + class UnsupportedYamlError(HomeAssistantError): """Unsupported YAML.""" @@ -31,22 +33,25 @@ class WriteError(HomeAssistantError): """Error writing the data.""" -def _include_yaml(constructor: SafeConstructor, node: ruamel.yaml.nodes.Node) \ - -> JSON_TYPE: +def _include_yaml(constructor: ExtSafeConstructor, + node: ruamel.yaml.nodes.Node) -> JSON_TYPE: """Load another YAML file and embeds it using the !include tag. Example: device_tracker: !include device_tracker.yaml """ + if constructor.name is None: + raise HomeAssistantError( + "YAML include error: filename not set for %s" % node.value) fname = os.path.join(os.path.dirname(constructor.name), node.value) return load_yaml(fname, False) -def _yaml_unsupported(constructor: SafeConstructor, node: +def _yaml_unsupported(constructor: ExtSafeConstructor, node: ruamel.yaml.nodes.Node) -> None: raise UnsupportedYamlError( 'Unsupported YAML, you can not use {} in {}' - .format(node.tag, os.path.basename(constructor.name))) + .format(node.tag, os.path.basename(constructor.name or '(None)'))) def object_to_yaml(data: JSON_TYPE) -> str: @@ -80,7 +85,7 @@ def load_yaml(fname: str, round_trip: bool = False) -> JSON_TYPE: yaml = YAML(typ='rt') yaml.preserve_quotes = True else: - if not hasattr(ExtSafeConstructor, 'name'): + if ExtSafeConstructor.name is None: ExtSafeConstructor.name = fname yaml = YAML(typ='safe') yaml.Constructor = ExtSafeConstructor