Skip to content

Commit

Permalink
validate against non-model params
Browse files Browse the repository at this point in the history
  • Loading branch information
sbittrich committed Dec 20, 2024
1 parent 9a46576 commit 8292237
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions molviewspec/molviewspec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ def make_params(params_type: Type[TParams], values=None, /, **more_values: objec
if values is None:
values = {}
result = {}
consumed = set()

# propagate custom properties
if values:
custom_values = values.get("custom")
if custom_values is not None:
result["custom"] = custom_values
consumed.add("custom")
ref = values.get("ref")
if ref is not None:
result["ref"] = ref
consumed.add("ref")

for field in params_type.__fields__.values():
# must use alias here to properly resolve goodies like `schema_`
key = field.alias

if more_values.get(key) is not None:
result[key] = more_values[key]
consumed.add(key)
elif values.get(key) is not None:
result[key] = values[key]
consumed.add(key)
elif field.default is not None: # currently not used
result[key] = field.default
consumed.add(key)

non_model_keys = set(more_values.keys()) - consumed
if non_model_keys:
raise ValueError(f"Encountered unknown attribute on {params_type}: {non_model_keys}")

return result # type: ignore

Expand Down

0 comments on commit 8292237

Please sign in to comment.