Skip to content

Commit 8e36def

Browse files
committed
_
1 parent 215007b commit 8e36def

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

skrub/_data_ops/_evaluation.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,15 @@ def _check_environment(environment):
427427
)
428428
# Notes about checking the env keys:
429429
#
430-
# - env ⊂ variables: in some cases we could check that there are no extra
431-
# keys in `environment`, ie all keys in `environment` correspond to a
432-
# name in the DataOp. However in other cases we naturally end up
433-
# using a bigger environment than what is needed. For example we want to
434-
# evaluate a sub-DataOp (such as the `mark_as_X()` node), and to do
435-
# it we use the environment that was passed to evaluate the full
436-
# DataOp. So if we want such a verification it should be a separate
437-
# check done at a higher level (eg in the estimators' `fit`, `predict`
438-
# etc.) where we know we are not working with a sub-DataOp. We do perform
439-
# such a check when a key is missing from the env to provide a better
440-
# message, but it is only used for the content of the message rather than
441-
# enforcing no extra keys ahead of time.
430+
# - env ⊂ variables: we could check that there are no extra keys in
431+
# `environment`, i.e. all keys in `environment` correspond to a name in
432+
# the DataOp. However in some cases we naturally end up using a bigger
433+
# environment than what is needed. For example we want to evaluate a
434+
# sub-DataOp (such as the result of `.skb.find()` or `.skb.find_X_y()`),
435+
# and to do it we use the environment created to evaluate the full
436+
# DataOp. We do perform this check when a key is missing from the env to
437+
# provide a better error message, but it is only used for the content of
438+
# the message rather than enforcing no extra keys ahead of time.
442439
#
443440
# - variables ⊂ env: we cannot check that all variables in the DataOp
444441
# have a matching key in the `environment`, because depending on the
@@ -531,9 +528,11 @@ def _uninitialized_variable_msg(error, data_op, environment):
531528
var_names = list(named_nodes(data_op).keys())
532529
choice_names = [n for c in choices(data_op).values() if (n := c.name) is not None]
533530
unused = list(
534-
{k for k in environment.keys() if not k.startswith("_skrub_")}.difference(
535-
var_names + choice_names
536-
)
531+
{
532+
k
533+
for k in environment.keys()
534+
if isinstance(k, str) and not k.startswith("_skrub_")
535+
}.difference(var_names + choice_names)
537536
)
538537

539538
msg = (

skrub/_data_ops/tests/test_errors.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,13 +676,16 @@ def test_missing_var_message():
676676

677677
@pytest.mark.skipif(sys.version_info < (3, 11), reason="no add_note")
678678
def test_missing_var_message_train_test_split():
679-
X = (skrub.var("x", np.arange(20)) + skrub.var("a")).skb.mark_as_X() + skrub.var(
680-
"b"
681-
)
679+
b = skrub.var("b")
680+
X = (skrub.var("x", np.arange(20)) + skrub.var("a")).skb.mark_as_X() + b
682681
# 'z' is extra but not 'b', even though 'b' is not needed for collecting X
683682
# it still exists in the DataOp as a whole.
684683
with pytest.raises(KeyError, match=r"\['z'\]"):
685684
X.skb.train_test_split({"z": 0, "b": 1})
685+
# check that we still get the correct error when the env contains int (ID)
686+
# keys
687+
with pytest.raises(KeyError, match=r"\['z'\]") as exc:
688+
X.skb.train_test_split({"z": 0, b.skb.id: 1})
686689
with pytest.raises(KeyError) as exc:
687690
X.skb.train_test_split()
688691
full_msg = "\n".join(traceback.format_exception(exc.value, exc.value, exc.tb))

0 commit comments

Comments
 (0)