Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORM: replace InputValidationError with ValueError and TypeError #4888

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Apr 29, 2021

Fixes #3812

The front-end ORM code threw an InputValidationError in a number a
places. However, this exception was intended for calculation plugins to
throw in the case of invalid input nodes. Where it was being used in the
ORM plain ValueError or TypeError exceptions should have been used.

Since the InputValidationError does not subclass either of the base
exception types, code that was catching this particular AiiDA-specific
exception will break after these changes. However, a scan of the most
used plugins revealed that this exception is actually not being caught
so this change should have little to no actual consequences.

Copy link
Contributor

@CasperWA CasperWA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you intend to completely remove the use of these exceptions (InputValidationError and ValidationError), maybe they should also be removed from aiida.common.exceptions?

@sphuber
Copy link
Contributor Author

sphuber commented Apr 29, 2021

If you intend to completely remove the use of these exceptions (InputValidationError and ValidationError), maybe they should also be removed from aiida.common.exceptions?

No, as discussed during the meeting, the exceptions still have their use (they are still used in the CalcJob.presubmit and that is what it was intended for, validating calculation job input nodes) and are also still used by plugins to raise. Here we are just making sure our ORM doesn't abuse it.

@CasperWA
Copy link
Contributor

(...) the exceptions still have their use (they are still used in the CalcJob.presubmit and that is what it was intended for, validating calculation job input nodes) and are also still used by plugins to raise. Here we are just making sure our ORM doesn't abuse it.

Just read through the related issue. Indeed, you're right.

Looking through the doc strings of the exceptions as well, I think ValidationError could get a refresh, but the InputValidationError seems clear enough as is. Although it's unclear to me why this cannot be used for workflows as well?

@sphuber
Copy link
Contributor Author

sphuber commented Apr 29, 2021

Although it's unclear to me why this cannot be used for workflows as well?

I mean you could, I just don't see the point. Who would be catching these exceptions? And when you want them to be caught, unless there is a specific reason they need to be very distinguishable, I think custom exceptions often make things more complicated than necessary.

@CasperWA
Copy link
Contributor

CasperWA commented Apr 29, 2021

(...) And when you want them to be caught, unless there is a specific reason they need to be very distinguishable, I think custom exceptions often make things more complicated than necessary.

I quite disagree. As long as you keep a proper hierarchy it's quite useful to have explanatory exception names in the code. For me it's not so much about the "catchability" (for this, a proper hierarchy and a parent exception can be used), but for more readable code, quickly making it clear why someone thinks it's necessary (what the intention is) to raise an exception in the code. But that's just me :)

But for the first part, I would update the doc strings of the exceptions to make it clear where it should be used then, to avoid similar things happening in the future. Although, it can be argued in this case that it doesn't matter, since the doc string was already quite explicit for InputValidationError, yet it was still ignored... 😅 But one can always try.

@sphuber sphuber force-pushed the fix/3812/orm-replace-input-validation-error branch from cf2c39c to 3e5b39b Compare April 29, 2021 10:29
@codecov
Copy link

codecov bot commented Apr 29, 2021

Codecov Report

Merging #4888 (84ac26b) into develop (dc686c5) will decrease coverage by 0.01%.
The diff coverage is 16.49%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #4888      +/-   ##
===========================================
- Coverage    80.08%   80.07%   -0.00%     
===========================================
  Files          518      518              
  Lines        36680    36671       -9     
===========================================
- Hits         29371    29361      -10     
- Misses        7309     7310       +1     
Flag Coverage Δ
django 74.56% <16.49%> (+0.03%) ⬆️
sqlalchemy 73.50% <16.49%> (-<0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiida/orm/implementation/django/querybuilder.py 77.22% <0.00%> (-0.14%) ⬇️
aiida/orm/implementation/querybuilder.py 78.19% <0.00%> (-0.26%) ⬇️
...iida/orm/implementation/sqlalchemy/querybuilder.py 81.04% <0.00%> (-0.10%) ⬇️
aiida/orm/nodes/data/array/trajectory.py 44.26% <0.00%> (+0.11%) ⬆️
aiida/tools/graph/age_rules.py 89.19% <0.00%> (-0.05%) ⬇️
aiida/orm/nodes/data/array/xy.py 16.18% <9.10%> (ø)
aiida/orm/querybuilder.py 80.21% <13.34%> (-0.02%) ⬇️
aiida/tools/data/orbital/realhydrogen.py 69.87% <25.00%> (ø)
aiida/cmdline/commands/cmd_code.py 89.87% <50.00%> (-0.08%) ⬇️
aiida/orm/nodes/data/orbital.py 94.88% <50.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc686c5...84ac26b. Read the comment docs.

@sphuber
Copy link
Contributor Author

sphuber commented May 3, 2021

@ltalirz could you please follow up on the discussion in the issue of this PR. And maybe some one else still wants to review this? @chrisjsewell @ramirezfranciscof ?

Copy link
Member

@ramirezfranciscof ramirezfranciscof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sphuber ! I have a couple of small things (some may be nitpicks) to mention. One of the comments though might be worth generalizing: since you are adapting all of these raises, would it make sense to turn most (if not all) of them into re-raises? (so, except ... as ... and raise ... from ...)

aiida/cmdline/commands/cmd_code.py Show resolved Hide resolved
aiida/orm/nodes/data/array/xy.py Outdated Show resolved Hide resolved
aiida/orm/querybuilder.py Outdated Show resolved Hide resolved
aiida/orm/utils/builders/code.py Show resolved Hide resolved
@@ -208,7 +207,7 @@ def _init_run(self, operational_set):
for proj_tag, projectionlist in projections.items():
try:
self._querybuilder.add_projection(proj_tag, projectionlist)
except InputValidationError:
except ValueError:
Copy link
Member

@ramirezfranciscof ramirezfranciscof May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_projection can also raise TypeError, right? (line 1180 of aiida.orm.querybuilder or whereabouts) Are you not catching that one because there is no point in adding the extra info*?

Btw, shouldn't we except ... as ... and raise ... from ... here?

(EDIT) * Or also, changing the raise to a KeyError...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, you are right, should also catch the TypeError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, since I think you wrote this code: why do you raise a KeyError here and why has the string an explicit newline?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, now I don't remember if I added this or it was originally like that on the base AGE branch; in any case I can't think of any good reason so I was probably just blundering 😅

If you want to change it though, is there a way to re-raise the same kind of exception? (since you are catching both ValueException and TypeException, you don't know a-priori which one to re-raise...)

@sphuber sphuber force-pushed the fix/3812/orm-replace-input-validation-error branch from 3e5b39b to 2cee3cd Compare May 5, 2021 12:19
Copy link
Member

@ramirezfranciscof ramirezfranciscof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a couple more of raise ... from ... that I think could be added; you can check them out but other than that this is ready!

The only other thing I noticed is that there seem to be a lot of validation pathways that are not being unit-tested. I was going to propose to open an issue because I thought this was mostly for Querybuilder error handling, but I see this is also in the data plugins so I'm not sure. Adding one issue for each or one issue for all seem both not ideal, although if I had to choose I would probably go for the latter, no?

aiida/orm/implementation/querybuilder.py Show resolved Hide resolved
aiida/orm/nodes/data/array/xy.py Outdated Show resolved Hide resolved
The front-end ORM code threw an `InputValidationError` in a number a
places. However, this exception was intended for calculation plugins to
throw in the case of invalid input nodes. Where it was being used in the
ORM plain `ValueError` or `TypeError` exceptions should have been used.

Since the `InputValidationError` does not subclass either of the base
exception types, code that was catching this particular AiiDA-specific
exception will break after these changes. However, a scan of the most
used plugins revealed that this exception is actually not being caught
so this change should have little to no actual consequences.

In addition, the `CodeValidationError` custom exception of the class
`aiida.orm.utils.builders.code.CodeBuilder` was changed to subclass the
`ValueError` instead of the base `Exception`.
@sphuber sphuber force-pushed the fix/3812/orm-replace-input-validation-error branch from 2cee3cd to 84ac26b Compare May 5, 2021 14:50
@sphuber
Copy link
Contributor Author

sphuber commented May 5, 2021

The only other thing I noticed is that there seem to be a lot of validation pathways that are not being unit-tested. I was going to propose to open an issue because I thought this was mostly for Querybuilder error handling, but I see this is also in the data plugins so I'm not sure. Adding one issue for each or one issue for all seem both not ideal, although if I had to choose I would probably go for the latter, no?

I think one for the QueryBuilder would be useful maybe since there is a lot of testing missing I think. But if I remember correctly, there may already be an issue related to this, or at least that the code should be cleaned/refactored. Maybe add it just to that one that the test coverage should also be improved.

@sphuber sphuber merged commit 12570ea into aiidateam:develop May 5, 2021
@sphuber sphuber deleted the fix/3812/orm-replace-input-validation-error branch May 5, 2021 15:10
@ramirezfranciscof
Copy link
Member

I have found nothing with the tags refactoring, testing or querybuilder. I even went through the whole list of a simple "test" search and nothing. I also had the idea that there was one, but if it is there is very well camouflaged =S.

Should I create two issues then, one for general coverage and one for the querybuilder in particular?

@sphuber
Copy link
Contributor Author

sphuber commented May 6, 2021

Sure, if you can't find anything, go ahead and open an issue for it. How it should be split depends really. We had one single issue before about test coverage that @ltalirz opened and then closed once the biggest offenders were fixed. Maybe we need to reinstate it or create a new one where you add these new offenders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ORM: replace InputValidationError with ValueError or TypeError
3 participants