Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Code contributions:
- gyanu2507 (gyanu2507)
- Rahul (rahul05ranjan)
- Anish Pentyala (anishpentyala)
- SU, Yanyu (TohruSu)

Suggestions and bug reporting:

Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Changelog

Unreleased
----------

* Fixing box_from_string(string_type="yaml") raising TypeError instead of BoxError when no YAML library is installed and Box is not compiled with Cython (thanks to SU, Yanyu)

Version 8.0.1
-------------

Expand Down
2 changes: 2 additions & 0 deletions box/from_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def box_from_string(content: str, string_type: str = "json") -> Box | BoxList:
raise BoxError("File is not TOML as expected")
# A TOML document is always a table, so unlike the other formats it is never a BoxList
elif string_type == "yaml":
if not yaml_available:
raise BoxError('yaml is unavailable on this system, please install the "ruamel.yaml" or "PyYAML" package')
try:
return Box.from_yaml(yaml_string=content)
except YAMLError:
Expand Down
14 changes: 14 additions & 0 deletions test/test_coverage_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,20 @@ def test_toon_unavailable(self, tmp_path, monkeypatch):
with pytest.raises(BoxError, match="toon"):
box_from_string("a: 1", string_type="toon")

def test_box_from_string_yaml_unavailable(self, monkeypatch):
# What from_file and Box look like without ruamel.yaml or PyYAML installed
def unavailable(cls, *args, **kwargs):
raise BoxError("yaml is unavailable on this system")

monkeypatch.setattr(from_file, "yaml_available", False)
monkeypatch.setattr(from_file, "YAMLError", False)
monkeypatch.setattr(Box, "from_yaml", classmethod(unavailable))
monkeypatch.setattr(BoxList, "from_yaml", classmethod(unavailable))
with pytest.raises(BoxError, match="yaml is unavailable"):
box_from_string("a: 1", string_type="yaml")
with pytest.raises(BoxError, match="yaml is unavailable"):
box_from_string("- 1\n- 2\n", string_type="yaml")

def test_box_from_string_json(self):
assert box_from_string('{"a": 1}').to_dict() == {"a": 1}
assert box_from_string("[1, 2]").to_list() == [1, 2]
Expand Down