Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure consistent error message about missing YAML parser #281

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
10 changes: 6 additions & 4 deletions box/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
except ImportError:
pyyaml_available = False

MISSING_PARSER_ERROR = "No YAML Parser available, please install ruamel.yaml>=0.17 or PyYAML"

toml_read_library: Optional[Any] = None
toml_write_library: Optional[Any] = None
toml_decode_error: Optional[Callable] = None
Expand Down Expand Up @@ -196,7 +198,7 @@ def _to_yaml(
elif pyyaml_available:
return yaml.dump(obj, stream=f, default_flow_style=default_flow_style, **yaml_kwargs)
else:
raise BoxError("No YAML Parser available, please install ruamel.yaml>0.17 or PyYAML")
raise BoxError(MISSING_PARSER_ERROR)

else:
if ruamel_available:
Expand All @@ -210,7 +212,7 @@ def _to_yaml(
elif pyyaml_available:
return yaml.dump(obj, default_flow_style=default_flow_style, **yaml_kwargs)
else:
raise BoxError("No YAML Parser available, please install ruamel.yaml>0.17 or PyYAML")
raise BoxError(MISSING_PARSER_ERROR)


def _from_yaml(
Expand All @@ -237,7 +239,7 @@ def _from_yaml(
kwargs["Loader"] = yaml.SafeLoader
data = yaml.load(f, **kwargs)
else:
raise BoxError("No YAML Parser available, please install ruamel.yaml>0.15 or PyYAML")
raise BoxError(MISSING_PARSER_ERROR)
elif yaml_string:
if ruamel_available:
yaml_loader = YAML(typ=ruamel_typ)
Expand All @@ -249,7 +251,7 @@ def _from_yaml(
kwargs["Loader"] = yaml.SafeLoader
data = yaml.load(yaml_string, **kwargs)
else:
raise BoxError("No YAML Parser available, please install ruamel.yaml>0.17 or PyYAML")
raise BoxError(MISSING_PARSER_ERROR)
else:
raise BoxError("from_yaml requires a string or filename")
return data
Expand Down
Loading