p4p icon indicating copy to clipboard operation
p4p copied to clipboard

`NTEnum` doesn't use passed in `extra` on init

Open evvaaaa opened this issue 10 months ago • 3 comments

Currently, both NTScalar and NTEnum allow for extra to be passed in on initialisation, which means additional fields can be passed in on init. These fields can be used in NTScalar, but not on NTEnum:

NTScalar

A record with type

some_scalar_t = NTScalar("b", extra=[("description", "s")]

Allows for wrapping and unwrapping with a description:

some_scalar = some_scalar_t.wrap({
    "value": False,
    "description": "haha"
})

and

[:)] pvget -v DEVICE:Child:E
DEVICE:Child:E epics:nt/NTScalar:1.0 
    byte value 0
    time_t timeStamp 2025-02-05 09:10:21.911  
        long secondsPastEpoch 1738746621
        int nanoseconds 911180973
    string description haha

Extra is passed in:

https://github.com/epics-base/p4p/blob/9040d4bb56d58b674de1475dbb62444a056f6729/src/p4p/nt/scalar.py#L199

On wrap the type of the value is used:

https://github.com/epics-base/p4p/blob/9040d4bb56d58b674de1475dbb62444a056f6729/src/p4p/nt/scalar.py#L235-L236

NTEnum - fixed by #154

NTEnum also takes extra, and builds its type with it:

https://github.com/epics-base/p4p/blob/9040d4bb56d58b674de1475dbb62444a056f6729/src/p4p/nt/enum.py#L42

However, wrap fails to take the extra fields:

https://github.com/epics-base/p4p/blob/9040d4bb56d58b674de1475dbb62444a056f6729/src/p4p/nt/enum.py#L62-L76

EDIT

NTNDArray also has this problem. buildType will extend with extra:

https://github.com/epics-base/p4p/blob/9040d4bb56d58b674de1475dbb62444a056f6729/src/p4p/nt/ndarray.py#L91

But wrap won't add extra fields:

https://github.com/epics-base/p4p/blob/9040d4bb56d58b674de1475dbb62444a056f6729/src/p4p/nt/ndarray.py#L171-L180

evvaaaa avatar Feb 05 '25 09:02 evvaaaa

I'm also struggling to get NTEnuma().wrap # No extra= to work with any input, either

wrap(some_index, choices=["A", "B"])

or

wrap({"choices": ["A", "B"], "index": some_index})

evvaaaa avatar Feb 05 '25 09:02 evvaaaa

I believe https://github.com/epics-base/p4p/pull/154 fixes this issue, but only for NTEnum types.

m2es3h avatar Aug 08 '25 20:08 m2es3h

Yes, looks like it fixes the enum part.

evvaaaa avatar Aug 11 '25 07:08 evvaaaa