Skip to content

Commit

Permalink
Check whether to use function default values
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Oct 11, 2022
1 parent 777c6d0 commit adb08bd
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions adaptive/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import os
import pickle
import warnings
from contextlib import contextmanager
from itertools import product

Expand Down Expand Up @@ -135,4 +136,19 @@ def partial_function_from_dataframe(function, df, function_prefix: str = "functi
kwargs[k] = v
if not kwargs:
return function

sig = inspect.signature(function)
for k, v in kwargs.items():
if k not in sig.parameters:
raise ValueError(
f"The DataFrame contains a default parameter"
f" ({k}={v}) but the function does not have that parameter."
)
default = sig.parameters[k].default
if default != inspect._empty and kwargs[k] != default:
warnings.warn(
f"The DataFrame contains a default parameter"
f" ({k}={v}) but the function already has a default ({k}={default})."
" The DataFrame's value will be used."
)
return functools.partial(function, **kwargs)

0 comments on commit adb08bd

Please sign in to comment.