#1194 - fix str(err) -> str(err.value)#1195
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1195 +/- ##
=======================================
Coverage 98.86% 98.87%
=======================================
Files 209 210 +1
Lines 32673 32716 +43
=======================================
+ Hits 32303 32348 +45
+ Misses 370 368 -2
Continue to review full report at Codecov.
|
arporter
left a comment
There was a problem hiding this comment.
Thanks for fixing these Adam. I think it raises two things though:
- The tests on GitHub Actions are not picking up the incorrect usage, even in tests that (presumably) are being run. Presumably we can change something so that they do?
- Some of the test utility code is not covered by the tests. That's not in the scope of this PR though so I'll create a new issue.
|
I have managed to reproduce the difference in behaviour for the two systems, and it is dependant on the version of pytest used. I have further tracked it down to the particular change that cased this - pytest-dev/pytest#5934. What appears to be happening is on the older versions of pytest, the name of the exception class is used within To combat this, I have added a With these changes, the assertions pass when Hopefully this is an acceptable solution to the problem. |
arporter
left a comment
There was a problem hiding this comment.
Thanks for taking the time to dig into this and find the root cause of the problem, it's much appreciated.
Although the changes fix the problem, I think it might be better to refactor things a bit and introduce an abstract 'base' class PSycloneException which all of our other exceptions should subclass. I think we could just then implement repr in this base class as:
def __repr__(self):
return type(self).__name__ + "()"
and therefore not need it anywhere else. Would you be happy to do this or would you prefer to create a new Issue for someone else to tackle?
I like the idea of the new test but found it hard to follow and codecov (https://app.codecov.io/gh/stfc/PSyclone/compare/1195/diff) points out that there are a few lines not covered. I had a play and came up with something a bit simpler (still using your all_sub_exceptions() routine):
def import_submodules(package, recursive=True):
""" Import all submodules of a module, recursively, including subpackages
:param package: package (name or actual module)
:type package: str | module
:rtype: dict[str, types.ModuleType]
"""
if isinstance(package, str):
package = importlib.import_module(package)
results = {}
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
full_name = package.__name__ + '.' + name
if "test" not in full_name:
results[full_name] = importlib.import_module(full_name)
if recursive and is_pkg:
results.update(import_submodules(full_name))
return results
if __name__ == "__main__":
mods = import_submodules("psyclone")
for mod in mods:
package = importlib.import_module(mod)
exceptions = all_sub_exceptions(Exception)
for exc in exceptions:
if "psyclone." in str(exc):
print(exc)
… make code clearer)
|
Sorry this took so long to respond to - I was called away to work on other things. I have now simplified the test and added a new abstract base class for the exceptions as suggested. |
arporter
left a comment
There was a problem hiding this comment.
Thanks very much for re-factoring Adam.
Just some minor things to tweak and I think we should call it PSycloneError for consistency with other classes (apologies if I suggested PsycloneError in the first place). Once that's done this can be merged.
| package = importlib.import_module(mod) | ||
|
|
||
| all_excpetions = all_sub_exceptions(Exception) | ||
| psy_excepts = [exc for exc in all_excpetions if "psyclone." in str(exc)] |
There was a problem hiding this comment.
Could we now have PSycloneError instead of Exception on the line above and then remove this line entirely?
There was a problem hiding this comment.
In principle yes - but I think I would argue this should remain as it is to make the test more robust. If a future change added an exception that (accidentally) did not inherit PSycloneError, it would be still be picked up by the current formulation; however it would be missed if suggested change was made.
There was a problem hiding this comment.
Actually, could we therefore go one better and check that every exception we find is an instance of PSycloneError. Should just be one or two extra lines?
| self.value = "Transformation Error: "+value | ||
|
|
||
| def __str__(self): | ||
| return repr(self.value) |
There was a problem hiding this comment.
Please could you add:
# For AutoAPI documentation generation
__all__ = ["TransformationError"]
to the bottom of this file.
There was a problem hiding this comment.
Thanks for doing this. Unfortunately I've discovered that we now get a truckload of warnings when building the reference guide I've asked a question on the sphinx user group (https://groups.google.com/g/sphinx-users/c/vuW6OOb96Yo) so we'll see what they say.
There was a problem hiding this comment.
Please could you just comment-out this __all__ line for now with a comment to say that it causes 'more than one target for cross-reference' warnings when building the reference guide and add a TODO referring to #1280.
|
I think I've made all the requested changes except the |
There was a problem hiding this comment.
Very, very nearly there now.
Please could you extend the exception testing to check that all exception classes defined by PSyclone sub-class PSycloneError.
There's also a problem with warnings generated when building the documentation. This is not your problem though - I've created #1280 for it and just ask that you add a TODO referencing it.
|
Please could you also bring this branch up-to-date with master. |
|
Changes now made |
arporter
left a comment
There was a problem hiding this comment.
All requested changes have been made.
All tests and examples OK with compilation.
Will proceed to merge.
fixes #1194 - change instances of
str(err)tostr(err.value)