Skip to content

check that key is valid on axis[key] #596

Description

@gdementen

Currently, keys are only checked when used in an array, so when creating a group, it happily accepts keys which do not exist.

>>> arr = ndtest((2, 3))
>>> # this should fail
... g = arr.a['yada']
>>> g
a['yada']
>>> arr[g]
ValueError: a['yada'] is not a valid label for any axis

FWIW, it was done this way initially for two reasons:

  1. performance (to avoid translating the key if it is not used), but in the end, it is better to be correct than fast and, in the usual case where the resulting group is actually used, it would be even faster this way (assuming we translate it directly to positional).
  2. to be correct in case the axis labels are modified in-place. But I now think we should either forbid this anyway, or allow this with a big fat warning that you might be shooting yourself in the foot.

related to #200. Please see the comments there.

Activity

  1. added this to the 0.30 milestone on Mar 1, 2018
  2. modified the milestones: 0.30, 0.31 on Jul 18, 2018
  3. modified the milestones: 0.31, 0.30 on Aug 31, 2018
  4. modified the milestones: 0.30, 0.31 on Jan 31, 2019
  5. removed this from the 0.31 milestone on Aug 1, 2019
  6. added this to the 0.36 milestone on Apr 2, 2026
  7. gdementen commented on Apr 3, 2026

    @gdementen
    ContributorAuthor

    I tried implementing this and fixed a few buggy tests thanks to this. I assume it would uncover less buggy code for users because they probably use the groups they define but... you never know.

    I commented the checking code so far because a couple of tests are still failing with this and I am undecided how to fix them and besides a bugfix release does not seem like the right time to implement such a change.

    Two class of test failures:

    • tests which are expected to fail/raise but now raise "too soon" and thus the error message is not correct/the expected one. I hope it will be just a matter of catching the exception and re-raising it.
    • a few tests revolving around the question of where we accept "query strings" and where we accept only raw values. For example:
      age = Axis('age=0..10')
      arr = ndtest(age)
      g = age[['2', '4', '7']]  # the group creation worked before but fails with the check (but it failed when using the group)
      arr[g]                    # failed before
      g = age['2,4,7']          # works before and after
      LGroup('1', axis=age)     # group creation worked before but fails with the check
      LGroup('1:3', axis=age)   # works before and after
      I think I remember there were some cases where "query strings" are parsed within a list but I cannot find any anymore. I don't think we should, so that would only leave us with the single integer string being refused oddity. It's not really an ambiguity but that's weird anyway.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions