scipp / scipp/scippnexus

`Group.__contains__` with nexus classes always returns True

Open
#241 1 comment 0 reactions 0 assignees View on GitHub

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:

  1. 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__.
  2. 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.