Open LaurentAjdnik opened 3 years ago
Hi @LaurentAjdnik ! Sorry for not getting back to you on this sooner. I'm always in favour of having some systematic way of doing things, so I am on board with this idea. I do think it needs a bit more fleshing out though, so I propose we stand by until you're done with your current issue and then we can discuss it further, in case you're still up for it. Sounds good?
Hi @HGSilveri, here are some more thoughts about it.
develop
branchTypeError
and ValueError
Sometimes this information is given (when applicable), sometimes not, in error messages:
And even when we have the same info, the sentences might be built differently.
I might have missed a few but I counted, in the develop
branch:
Error | Reason | Number |
---|---|---|
AttributeError |
1 | |
IndexError |
1 | |
NotImplementedError |
7 | |
RuntimeError |
1 | |
SystemError |
4 | |
TypeError |
Instance | 12 |
TypeError |
Others | 8 |
ValueError |
Interval | 1 |
ValueError |
Set | 4 |
ValueError |
Threshold | 12 |
ValueError |
Others | 40 |
ZeroDivisionError |
1 | |
TOTAL | 92 | |
--- | --- | --- |
AttributeError
IndexError
NotImplementedError
RuntimeError
SystemError
TypeError
/ InstanceRaised when a parameter is not an instance of the expected type.
Lots of different phrasings. Should be rewritten or factored.
obs_list
must be a list of operators"TypeError
/ OthersVery specific and probably nothing to factor.
ValueError
/ IntervalRaised when a parameter does not belong to an interval.
Could be transformed into two ValueError
/ Threshold checks.
sampling_rate
must be positive and not larger than 1.0"ValueError
/ SetRaised when a parameter does not belong to a set of allowed values.
Lots of different phrasings. Should be rewritten or factored.
basis_name
must be 'ground-rydberg', 'digital' or 'all'."meas_basis
must be 'ground-rydberg' or 'digital'."ValueError
/ ThresholdRaised when a value should be above/below/equal to some allowed threshold (included/exluded).
Lots of different phrasings. Should be rewritten or factored.
sampling_rate
is too small, less than 4 data points."ValueError
/ OthersSo many of them (40) ! I won't write them down here.
ZeroDivisionError
One other thing: I believe that sometimes the built-in errors are abused, mostly out of laziness to not wanting to create a new error class. I would be in favour of being more strict with their definition and raising a custom Error whenever none of the built-in ones are a good fit. Would you agree?
One other thing: I believe that sometimes the built-in errors are abused, mostly out of laziness to not wanting to create a new error class. I would be in favour of being more strict with their definition and raising a custom Error whenever none of the built-in ones are a good fit. Would you agree?
I agree. Custom errors can be convenient when they make sense functionally.
Here, I feel like MeasurementError
is a must-have. It is closely related to quantic concepts. It would replace three occurrences of SystemError
.
Maybe also (but not so sure) some kind of DeviceError
when we manipulate it in an improper way?
Or perhaps (even less sure) something close to ChannelError
and WaveformError
for some specific cases?
Otherwise, I wouldn't specialize too much. Most errors are TypeError
and ValueError
which seem relevant to me.
Back to the original issue of consistency for error messages.
We could rewrite some of them already.
Here are a few rules for ValueError
when applied to a threshold:
must
instead of can not
, should
...equal to
, greater than
, greater than or equal to
, less than
, less than or equal to
rather than above
, below
, at least
...must be greater than or equal to
rather than must not be less than
must not be less than
rather than must be greater than or equal to
%
, format()
...)Example:
Spacing between atoms ('spacing' = 3) must be greater than or equal to the minimal distance allowed by the device (4 µm).
ORSpacing between atoms ('spacing' = 3) must not be less than the minimal distance allowed by the device (4 µm).
Another example:
The number of atoms ('n_qubits' = 125) must be less than or equal the max number of atoms allowed by the device (100).
ORThe number of atoms ('n_qubits' = 125) must not be greater than the max number of atoms allowed by the device (100).
What do you think?
I think this is great, I have no objections.
My only concern is the size of the error factory call becoming larger than the written message itself, and perhaps it being hard to decipher for someone reading the source code.
Nonetheless, I'm all for this change, I look forward to seeing what you come up with!
My only concern is the size of the error factory call becoming larger than the written message itself, and perhaps it being hard to decipher for someone reading the source code.
I totally agree. Sorry for not being more explicit. My "we could rewrite some of them already" meant "without a factory".
Nonetheless, I'm all for this change, I look forward to seeing what you come up with!
I'll start working on it soon, first with TypeError
and ValueError
(intervals / thresholds).
There are sooooooo many ways to write an Error message.
For instance when starting the message:
Then we have to decide whether to include the parameter value that was provided and the allowed values (see #148). And how.
Even the phrasing for allowed values can vary a lot:
Right now, along the code, we have a mix of different approaches.
Maybe we should enact rules to ensure consistency, especially since we intend to add lots of checks and hints.
As an option, we could even create an ErrorMessageFactory class.
One among very few functions:
ErrorMessageFactory.getMessage(subject, parameter, value, constraint, allowed)
ErrorMessageFactory.getMessage("The number of qubits", "n_qubits", -1, ErrorMessageFactory.EQUAL_OR_ABOVE, 1)
"The number of qubits ('n_qubits' = -1) must be 1 or above"
Of course, we would have to identify the common types of constraints and allowed values (single or interval) but it seems feasable.
At the same time, this would allow us, in test functions, to check for full Error messages with exact matches rather than partial matches or regular expressions.
Maybe I'm splitting hairs here, but I would LOVE to discuss this issue. :grin: