pasqal-io / Pulser

Library for pulse-level/analog control of neutral atom devices. Emulator with QuTiP.
Apache License 2.0
172 stars 63 forks source link

Consistency for `Error` messages #157

Open LaurentAjdnik opened 3 years ago

LaurentAjdnik commented 3 years ago

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:

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:

HGSilveri commented 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?

LaurentAjdnik commented 3 years ago

Hi @HGSilveri, here are some more thoughts about it.

TL;DR

Diversity

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.

Stats

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 / Instance

Raised when a parameter is not an instance of the expected type.

Lots of different phrasings. Should be rewritten or factored.

TypeError / Others

Very specific and probably nothing to factor.

ValueError / Interval

Raised when a parameter does not belong to an interval.

Could be transformed into two ValueError / Threshold checks.

ValueError / Set

Raised when a parameter does not belong to a set of allowed values.

Lots of different phrasings. Should be rewritten or factored.

ValueError / Threshold

Raised when a value should be above/below/equal to some allowed threshold (included/exluded).

Lots of different phrasings. Should be rewritten or factored.

ValueError / Others

So many of them (40) ! I won't write them down here.

ZeroDivisionError

HGSilveri commented 3 years ago

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?

LaurentAjdnik commented 3 years ago

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.

LaurentAjdnik commented 3 years ago

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:

Example:

Another example:

What do you think?

HGSilveri commented 3 years ago

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!

LaurentAjdnik commented 3 years ago

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).