Repository navigation
RFC: replace nested if/else logic tree with flat pattern matching in discretize_model - #17825
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
0c67df0 to
015723a
Compare
|
Note that it actually fixes a bug of sorts: previously the return value may be |
015723a to
644cf40
Compare
if/else logic tree with flat pattern matching in discretize_model
|
If this changes a look-up behavior, perhaps a bugfix change log? Thought might not want to backport. |
|
I'm going to come across as old and conservative here, but this looks nothing like python to me and I don't know how to read it. I'll admit it's cleaner, briefer text, which I like, but it's a new language to me. I'll probably end up using it in a couple years. So, I'm :+0: on this. Regarding the corner case - I don't think it can occur. ndim is set by astropy/astropy/convolution/utils.py Line 177 in 644cf40 i.e., it is the model dimensionality, and ndim > 2 is already checked for:astropy/astropy/convolution/utils.py Line 178 in 644cf40 the only other case to consider is ndim <= 0, which I think should not be possible (astropy doesn't have 0D models, right?).
|
Right now it doesn't, but the refactor still makes it more robust if this ever changed. In the old version, the function would just silently return |
As clarified by @keflavich, it doesn't change behavior in any existing case. It does preclude the introduction of a bug if we were to support 0D models1, and to some extent makes the following guard clause redundant (but keeping it doesn't seem to harm either), so I don't think we need a changelog at this point. astropy/astropy/convolution/utils.py Lines 178 to 179 in 644cf40
Happy to explain any specific aspect that is not clear to you, btw. Footnotes
|
|
I agree, this refactor enforces consistency. Also, agree on no changelog needed. |
| raise ValueError("Invalid mode for discretize_model.") | ||
| case _: | ||
| raise ValueError( | ||
| "Invalid (mode, ndim) combination for discretize_model. " |
There was a problem hiding this comment.
Can we at least keep the error message starting out the same as the old message? In case downstream is doing a match in their test? No reason to change that?
There was a problem hiding this comment.
I could write it as "Invalid mode, or ndim for discretize_model", so it would match two words instead of one, but then, what happens if someone tries to match the whole string, and where does it end ? IMO, this is why downstream has no business testing a message they do not control.
There was a problem hiding this comment.
Ah right, the ndim... I tried to suggest something but it didn't flow right. Since Adam approved... let's keep what you have. Thanks!
|
@keflavich , with typing and so on, does Python even look like Python anymore? 🤪 The |
| raise ValueError("Invalid mode for discretize_model.") | ||
| case _: | ||
| raise ValueError( | ||
| "Invalid (mode, ndim) combination for discretize_model. " |
There was a problem hiding this comment.
Ah right, the ndim... I tried to suggest something but it didn't flow right. Since Adam approved... let's keep what you have. Thanks!
Description
Following a suggestion from @pllim in https://github.com/astropy/astropy/pull/17823/files#r1971954084, I was curious to see if it helped readibility, and I think it does, so, submitting.