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

Fix: correct the raise behavior of function j_loader #4492

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions deepmd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,26 @@ def j_loader(filename: Union[str, Path]) -> dict[str, Any]:
Raises
------
TypeError
if the supplied file is of unsupported type
if file is not json or yaml/yml
"""
filepath = Path(filename)
if filepath.suffix.endswith("json"):

try:
with filepath.open() as fp:
return json.load(fp)
elif filepath.suffix.endswith(("yml", "yaml")):
except json.JSONDecodeError: # if not json
pass # will try the next option

# FileNotFoundError will not be caught here,
# so it will be raised if file does not exist

try:
with filepath.open() as fp:
return yaml.safe_load(fp)
else:
raise TypeError("config file must be json, or yaml/yml")
except yaml.YAMLError: # if not yaml
pass # no more options, raise TypeError

raise TypeError("config file must be json, or yaml/yml")


def expand_sys_str(root_dir: Union[str, Path]) -> list[str]:
Expand Down
Loading