Autostronomy / AstroPhot

A fast, flexible, automated, and differentiable astronomical image 2D forward modelling tool for precise parallel multi-wavelength photometry
https://astrophot.readthedocs.io
GNU General Public License v3.0
79 stars 9 forks source link

Consider replacing asserts with raises #117

Closed wmwv closed 7 months ago

wmwv commented 9 months ago

Is your feature request related to a problem? Please describe. I'm surprised to see and get AssertionError generated by assert statements.

Describe the solution you'd like I would expect infomative Errors to be raised. This seems more Pythonic to me.

I might suggest creating a informative hierarchy of Errors to raise. That will allow a user to catch errors they want and can deal with, and let others continue on to raise.

I just spent 10 minutes failing to find a good example that wasn't just search-bait fluff. But here's the official documentation along with an example that uses custom exceptions the way I'm thinking; basically it's as simple as creating a class with a name -- and it's that name that's going to be informative about what type of error was raised and whether or not you want to do anything about it.

https://docs.python.org/3/library/exceptions.html#exception-hierarchy https://breadcrumbscollector.tech/custom-exceptions-in-python-how-and-what-for/

This one's kind of okay too, just note that it explicitly raises the exceptions in the try part of the examples, where in a real application it would be some operation that should have worked that would be in the try block and the except block would just choose to deal with selected errors (and ignore other ones).

https://www.geeksforgeeks.org/user-defined-exceptions-python-examples/#

ConnorStoneAstro commented 9 months ago

That sounds like a great idea! Let me know if there are any particular cases you would like to catch. Otherwise I'll just start by replacing the assert statements and other raises and go from there.

ConnorStoneAstro commented 7 months ago

Closed by #137