You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error in return type for most 'autorange' properties in nidcpower library #2030
You may be thinking bool is wrong because the NI-DCPower C API defines the attribute as ViInt32. But while our Python and C APIs are very close matches, they do deviate in small specific ways enabled by the nature of the Python language. This is an example of that. Please check our Python API Design Guidelines for more detail.
I do not think that bool is wrong because of the C API, but because Python tells me so. (See the pytest code example.)
The first link that you provided, which refers to nidcpower._SessionBase in the session module, proves that the documentation is wrong, as Python sees the returned type as an int.
However bool (as documented) is right.
Returning int is wrong, we don't use magic int values in the Python API. A property like this would be bool if possible, its own enum type if not.
Based on what @marcoskirsch has told me, we should have done something like this with an enum for each of these properties:
'IsolationState': {
'codegen_method': 'private',
'converted_value_to_enum_function_name': 'convert_to_isolation_state_enum',
'enum_to_converted_value_function_name': 'convert_from_isolation_state_enum',
'values': [
{
'converts_to_value': True,
'documentation': {
'description': 'The channel is disconnected from chassis ground.'
},
'name': 'NIDCPOWER_VAL_ISOLATED',
'value': 1128
},
{
'converts_to_value': False,
'documentation': {
'description': 'The channel is connected to chassis ground.'
},
'name': 'NIDCPOWER_VAL_NON_ISOLATED',
'value': 1129
}
]
},
Key points:
Have a private enum that we convert to/from (in some or all cases that enum doesn't exist, even privately, for the Python API, I think, but does for the C API)
For that enum:
define converted_value_to_enum_function_name
define enum_to_converted_value_function_name
define converts_to_value for each value
Before we make a change like this, we need to think through the implications and do some testing to confirm it's not a breaking change.
Activity
marcoskirsch commentedon Nov 28, 2023
Hello,
Looking at the
nidcpower.Sessioncode, I see that the type of propertyautorangeisbool, which matches documentation.You may be thinking
boolis wrong because the NI-DCPower C API defines the attribute asViInt32. But while our Python and C APIs are very close matches, they do deviate in small specific ways enabled by the nature of the Python language. This is an example of that. Please check our Python API Design Guidelines for more detail.Thanks for the feedback!
Arkh42 commentedon Nov 28, 2023
Hello,
I do not think that
boolis wrong because of the C API, but because Python tells me so. (See the pytest code example.)The first link that you provided, which refers to
nidcpower._SessionBasein thesessionmodule, proves that the documentation is wrong, as Python sees the returned type as anint.marcoskirsch commentedon Nov 28, 2023
I see, I misread the code.
However
bool(as documented) is right.Returning
intis wrong, we don't use magicintvalues in the Python API. A property like this would beboolif possible, its own enum type if not.Arkh42 commentedon Nov 28, 2023
It happens.
Thanks for considering it again. :)
marcoskirsch commentedon Nov 28, 2023
Were there other properties in the API that seem wrong?
Arkh42 commentedon Nov 30, 2023
Here is a list of the properties that are documented as
boolbut are in factintin Python:autorangecurrent_limit_autorangecurrent_level_autorangevoltage_limit_autorangevoltage_level_autorangeEDIT: I tested all of them in a similar fashion as the above test script provided when I opened the issue.
ni-jfitzger commentedon Dec 6, 2023
Based on what @marcoskirsch has told me, we should have done something like this with an enum for each of these properties:
Key points:
converted_value_to_enum_function_nameenum_to_converted_value_function_nameconverts_to_valuefor each valueBefore we make a change like this, we need to think through the implications and do some testing to confirm it's not a breaking change.