Given a Group group, the following always evaluates to True:
And the same applies to all NX classes.
The reason is that we don't define Group.__contains__. So Python uses the fallback for mappings:
def __contains__(self, key):
try:
self[key]
except KeyError:
return False
else:
return True
But group[NXdata] returns an empty dict if there is no NXdata in the group. So __contains__ is always True.
This is surprising behaviour. I think we have two options to fix it without breaking anything:
- Actually check whether there are any children with the given class. This fixes the use case I have. But it creates an inconsistency between
__contains__ and __getitem__.
- Raise a
TypeError when __contains__ is called with anything but a `str. This also creates an inconsistency. But I think this one is less severe.
Alternatively, we could change the behaviour of __getitem__ to raise when there is no child with the given class.
Or we leave it as. This keeps the mapping API consistent. But this can easily lead to errors downstream.
Given a
Groupgroup, the following always evaluates toTrue:And the same applies to all NX classes.
The reason is that we don't define
Group.__contains__. So Python uses the fallback for mappings:But
group[NXdata]returns an empty dict if there is noNXdatain the group. So__contains__is alwaysTrue.This is surprising behaviour. I think we have two options to fix it without breaking anything:
__contains__and__getitem__.TypeErrorwhen__contains__is called with anything but a `str. This also creates an inconsistency. But I think this one is less severe.Alternatively, we could change the behaviour of
__getitem__to raise when there is no child with the given class.Or we leave it as. This keeps the mapping API consistent. But this can easily lead to errors downstream.