Loading
Handle edge cases in utils helpers
TL;DR
- An empty attribute
dtypesuch as[]or((),)now reaches the error explaining the expected spelling, instead of raisingIndexError. tango.utils.get_attrsno longer writes to standard output, and raises theTypeErrorit intends to.tango.utils.EventCallbackrenders an unrecognised event type instead of printingNone, and acceptsmax_buf=0.__build_to_tango_typeno longer rebuilds part of its map once perCmdArgTypeattribute.
Reported in #789.
Key Facts
is_devstate_sequnwrapped nested sequences without checking anything was left. It matters becauseget_attribute_type_formatcalls it before the code that raises "Spectrum attribute type must be specified as (,)", so a user who wrote the dtype wrongly got anIndexErrorinstead of the message telling them how to write it.is_enum_seqalready carried the guard; this makes the pair symmetric.get_attrshad a second defect beyond the strayprint: its message interpolatedobj.__name__, which only exists on classes, so for an instance the intendedTypeErrorwas replaced by anAttributeErrorraised while formatting it. The message now reports the type of the bad mapping._appendcompared buffer length with==, somax_buf=0popped from an empty list on the first event. Now it trims with a loop and only stores when the buffer has room, which is also robust if the buffer ever exceeds its bound.__build_to_tango_typeis behaviour-preserving. The inner loop sat inside thedir(CmdArgType)loop and reused both loop variables, so it rebuilt the numpy half of the map on every iteration and produced the same dict at the end. Ruff flags the variable reuse asPLW2901. Existing coverage ofTO_TANGO_TYPEguards the result.
Caveats
- The first commit adds the tests and fails on its own; the second makes them pass.
- The
get_attrstest needs an object whose__dict__is not a mapping, which takes a metaclass with a__dict__property. That is contrived, but it is the only way to reach the branch. EventCallback._get_valueand_appendare marked "Internal usage only" and are tested directly. The publicpush_eventswallows exceptions, so a test through it could not distinguish these fixes from the existing error handling.- Builds on !1074 (merged) and targets its branch. Retarget as the chain merges.