From d27ac1f9360275db1294218feadc9f88c1c017d0 Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Fri, 21 Nov 2025 14:44:24 +0000 Subject: [PATCH 1/2] Fixed ruamel.yaml load function for 0.15+ support --- setup.py | 4 ++-- tests/spec_test.py | 13 ++++++++----- yapconf/__init__.py | 23 +++++++++++++++-------- yapconf/sources.py | 5 ++++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 9ae4cc5..7fce2e2 100644 --- a/setup.py +++ b/setup.py @@ -35,10 +35,10 @@ "pytest-cov", "pytest-runner", "python-etcd", - "ruamel.yaml", + "ruamel.yaml>=0.15", "tox", ], - "yaml": ["ruamel.yaml"], + "yaml": ["ruamel.yaml>=0.15"], } setup( diff --git a/tests/spec_test.py b/tests/spec_test.py index f3ffbfc..ab75e31 100644 --- a/tests/spec_test.py +++ b/tests/spec_test.py @@ -293,15 +293,18 @@ def test_migrate_config_file_does_not_exist_create(basic_spec): @patch('os.path.isfile', Mock(return_value=False)) def test_migrate_config_file_create_yaml(basic_spec): open_path = 'yapconf.open' - with patch('yapconf.yaml.safe_dump') as dump_mock: + with patch('yapconf.yaml.dump') as dump_mock: with patch(open_path, mock_open()) as mock_file: new_config = basic_spec.migrate_config_file( '/path/to/file', create=True, output_file_type='yaml') - dump_mock.assert_called_with( + if type(yapconf.yaml).__name__== "YAML": + dump_mock.assert_called_with( + {"foo": None}, + mock_file()) + else: + dump_mock.assert_called_with( {"foo": None}, - mock_file(), - default_flow_style=False, - encoding='utf-8') + mock_file(), default_flow_style=False, encoding='utf-8') assert new_config == {"foo": None} diff --git a/yapconf/__init__.py b/yapconf/__init__.py index bd48405..5ac45ff 100644 --- a/yapconf/__init__.py +++ b/yapconf/__init__.py @@ -34,13 +34,10 @@ # ruamel.yaml is installed. import ruamel.yaml as yaml - # ruamel.yaml depricated support for safe_load in 0.17.0 - if packaging.version.parse(yaml.__version__) < packaging.version.parse("0.18.0"): - yaml.load = yaml.safe_load - else: - from ruamel.yaml import YAML + from ruamel.yaml import YAML - yaml = YAML(typ="safe", pure=True) + yaml = YAML(typ="safe", pure=True) + except ImportError: @@ -183,7 +180,13 @@ def _dump(data, stream, file_type, **kwargs): else: stream.write(six.u(dumped)) elif str(file_type).lower() == "yaml": - yaml.safe_dump(data, stream, **kwargs) + + # Depending on the yaml module loaded, need to handle arguments differently + if type(yaml).__name__== "YAML": + yaml.default_flow_style = kwargs.get("default_flow_style", False) + yaml.dump(data, stream) + else: + yaml.dump(data, stream, **kwargs) else: raise NotImplementedError( "Someone forgot to implement dump for file " "type: %s" % file_type @@ -225,7 +228,11 @@ def load_file( if str(file_type).lower() == "json": data = json.load(conf_file, **load_kwargs) elif str(file_type).lower() == "yaml": - data = yaml.load(conf_file.read()) + # Depending on the yaml module loaded, need to handle arguments differently + if type(yaml).__name__== "YAML": + data = yaml.load(conf_file.read()) + else: + data = yaml.safe_load(conf_file.read()) else: raise NotImplementedError( "Someone forgot to implement how to load a %s file_type." % file_type diff --git a/yapconf/sources.py b/yapconf/sources.py index be7b213..7bd17b2 100644 --- a/yapconf/sources.py +++ b/yapconf/sources.py @@ -491,7 +491,10 @@ def get_data(self): if self.config_type == "json": return json.loads(nested_config,) elif self.config_type == "yaml": - return yapconf.yaml.load(nested_config) + if type(yapconf.yaml).__name__== "YAML": + return yapconf.yaml.load(nested_config) + else: + return yapconf.yaml.safe_load(nested_config) else: raise NotImplementedError( "Cannot load config with type %s" % self.config_type From b1c130d841705493b32361186728c12a122fa8d8 Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Fri, 21 Nov 2025 14:51:30 +0000 Subject: [PATCH 2/2] formatting --- yapconf/__init__.py | 6 ++---- yapconf/sources.py | 6 ++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/yapconf/__init__.py b/yapconf/__init__.py index 5ac45ff..103555d 100644 --- a/yapconf/__init__.py +++ b/yapconf/__init__.py @@ -33,11 +33,9 @@ # we want our code to be the same whether or not PyYaml or # ruamel.yaml is installed. import ruamel.yaml as yaml - from ruamel.yaml import YAML yaml = YAML(typ="safe", pure=True) - except ImportError: @@ -182,7 +180,7 @@ def _dump(data, stream, file_type, **kwargs): elif str(file_type).lower() == "yaml": # Depending on the yaml module loaded, need to handle arguments differently - if type(yaml).__name__== "YAML": + if type(yaml).__name__ == "YAML": yaml.default_flow_style = kwargs.get("default_flow_style", False) yaml.dump(data, stream) else: @@ -229,7 +227,7 @@ def load_file( data = json.load(conf_file, **load_kwargs) elif str(file_type).lower() == "yaml": # Depending on the yaml module loaded, need to handle arguments differently - if type(yaml).__name__== "YAML": + if type(yaml).__name__ == "YAML": data = yaml.load(conf_file.read()) else: data = yaml.safe_load(conf_file.read()) diff --git a/yapconf/sources.py b/yapconf/sources.py index 7bd17b2..42e308e 100644 --- a/yapconf/sources.py +++ b/yapconf/sources.py @@ -489,9 +489,11 @@ def get_data(self): nested_config = result.data[self.key] if self.config_type == "json": - return json.loads(nested_config,) + return json.loads( + nested_config, + ) elif self.config_type == "yaml": - if type(yapconf.yaml).__name__== "YAML": + if type(yapconf.yaml).__name__ == "YAML": return yapconf.yaml.load(nested_config) else: return yapconf.yaml.safe_load(nested_config)