`Group.__contains__` with nexus classes always returns True
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
Given a Group group, the following always evaluates to True:
NXdata in group
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
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Group.contains and the Group.getitem behavior described in the issue, then compare the proposed semantics for string names and NX classes. The work is complete when the project has chosen and verified a consistent containment behavior for missing NX classes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100