Skip to content

Conversation

@jokasimr
Copy link
Contributor

Fixes #3182.

Is there any case when non str slices are allowed in the DataGroup.__setitem__ method?

@jokasimr jokasimr requested a review from nvaytet September 14, 2023 10:50
if isinstance(name, str):
self._items[name] = value
else:
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError(
raise TypeError(

self._items[name] = value
else:
raise ValueError(
'Assigning to slice with implicit dimension label is not possible.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 'implicit dim labels'? I would expect the message to talk about the type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I read the example in the issue again. Maybe the message should be a little longer and be more explicit about types and slicing.

Copy link
Contributor Author

@jokasimr jokasimr Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree the error message is a bit confusing, because, with the implementation in this PR, assigning to a slice with an explicit dimension label is also not possible ;)

Maybe it would be better to have something simpler like KeyError: non 'str' keys not allowed in datagroup. Did you mean to assign to one of the underlying variables in the datagroup?.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about simply: "Keys must be strings"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be KeyError because that is used for failed lookups. But otherwise, I'm fine with either message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll make it simply "TypeError: Keys must be strings"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The slightly confusing thing from an api perspective is that you can get items using slicing without specifying the label:

import scipp as sc
ds = sc.DataGroup(a=sc.arange('x', 4), b=sc.arange('y', 6))
ds['x', 2] # This works, returns a DataGroup

but if you try to set something without specifying the label

ds['x', 2] = sc.scalar(6) 

it raises TypeError: Keys must be strings.

What they should do of course is:

ds['a']['x', 2] = sc.scalar(6)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. This is why I mentioned using a longer error message. But it might be fine the way it is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe you are right that we should mention that setting slices is not supported?

@jl-wynen
Copy link
Member

Is there any case when non str slices are allowed in the DataGroup.setitem method?

Doesn't look like it at the moment. Setting slices would be more complicated to implement...

@SimonHeybrock
Copy link
Member

Is there any case when non str slices are allowed in the DataGroup.setitem method?

Doesn't look like it at the moment. Setting slices would be more complicated to implement...

While we have not made an ultimate decision, my feeling is that we will not support this.

Comment on lines 127 to 130
def test_setitem_nonstr_key_fails():
dg = sc.DataGroup(a=sc.arange('x', 2))
with pytest.raises(ValueError):
dg[0] = sc.scalar(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another test, verifying that this is not supported in __init__ either (unless we have that elsewhere already)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that test already exists here.

@jokasimr jokasimr force-pushed the prevent-non-str-keys branch from 740f958 to f9109b3 Compare September 14, 2023 13:55
@jokasimr jokasimr force-pushed the prevent-non-str-keys branch from f9109b3 to 88d2374 Compare September 15, 2023 12:04
@jokasimr jokasimr force-pushed the prevent-non-str-keys branch from 88d2374 to fc03055 Compare September 15, 2023 12:39
@jokasimr jokasimr merged commit 84952a6 into main Sep 15, 2023
@jokasimr jokasimr deleted the prevent-non-str-keys branch September 15, 2023 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataGroup.__setitem__ fails to prevent non-string keys

4 participants