Update physical type implementation#11625
Conversation
|
👋 Thank you for your draft pull request! Do you know that you can use |
97de10a to
8df8da0
Compare
- Added 'velocity' as a physical type name along with 'speed' - Removed 'sustained displacement' from 'absement' because the former is more like a definition than a name
Because the physical_type attribute of a unit was a str before astropy#11204, there may be code that depends on those str methods for the physical_type attribute. This approach was adapted from code suggested in astropy#11204 after it was merged. Co-authored-by: Marten van Kerkwijk <mhvk@astro.utoronto.ca>
I made it return 'energy density, pressure, stress' rather than using set notation. I tentatively chose commas rather than slashes for readability.
5287364 to
a77e1d4
Compare
nstarman
left a comment
There was a problem hiding this comment.
Excepting a changelog, LGTM!
| "method, expected", | ||
| [ | ||
| ("title", 'Length'), | ||
| ("isalpha", True), | ||
| ("isnumeric", False), | ||
| ("upper", 'LENGTH') | ||
| ], | ||
| ) |
There was a problem hiding this comment.
My preference is also for black but https://docs.astropy.org/en/stable/development/codeguide.html?highlight=black#coding-style-conventions.
There was a problem hiding this comment.
black isn't encouraged much in this repo, as it conflicts with existing style some times. The perception might change in the future, so who knows.
There was a problem hiding this comment.
I changed the style a bit in response, but any of the maintainers should please feel free to suggest and accept stylistic changes.
There was a problem hiding this comment.
The perception might change in the future, so who knows.
I've been itching to do >>> black astropy and git push for years now. 🤣
|
Ready for review! Some questions...
[slightly edited for clarity] |
I prefer "," (but actually not visually). This class is new behavior and it seems reasonable to prioritize
Looks nice. But it doesn't feel necessary. I know in formal logic "or" is considered inclusive, but that's not true colloquially. Identifying physical types by their units makes it impossible to differentiate between energy density and pressure, so it feels wrong to use "or". But that's just IMO.
Do we even want to set a hard deadline? This backwards compatibility is to give affiliate packages time and reason to switch. I think it's better to have this be unspecified, have someone stumble on it in a few years, say "Oh yeah!", and ask around if anyone's still using it. @pllim, you have one such affiliate package. Thoughts? |
5b22b1b to
96d9230
Compare
|
@mhvk LGTM |
|
This deprecation comes a bit too late to the release cycle than I like... Doesn't give downstream maintainers much time to catch up before branching happens. Still, hopefully minor enough that it won't cause too much trouble, and better to get this in before #11204 is released. 🤞 |
Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>
I had intended to get #11204 finished two months earlier than I did, but pandemic era brain blah got the better of me...
I just did a search of GitHub, and I found only one usage of a string method on a |
mhvk
left a comment
There was a problem hiding this comment.
Some small bits, but one slightly larger question for __str__, on what the separator should be.
| "from PhysicalType instances is deprecated since 4.3 and will be removed in a " | ||
| "subsequent release.") | ||
| warnings.warn(warning_message, AstropyDeprecationWarning) | ||
| self_str_attr = getattr(str(self), attr, None) |
There was a problem hiding this comment.
No need for the ", None" any more if attr in dir(str), right?
Note that personally I'd prefer to stick with keeping this on top - if we enter __getattr__, regular attribute access has failed already so it doesn't really matter if it is slightly slower. And I'm just not 100% sure that dir(str) and hasattr(str(self), attr) are really completely equivalent...
| self_str_attr = getattr(str(self), attr, None) | ||
| return self_str_attr | ||
| else: | ||
| raise AttributeError( |
There was a problem hiding this comment.
Just do super().__getattribute__(attr) so you get the standard error message.
| return self._physical_type_list[0] | ||
| else: | ||
| return self._name_string_as_ordered_set() | ||
| return ", ".join(self._physical_type_list) |
There was a problem hiding this comment.
If we're dropping the braces, should we make this "/".join(...) so that in principle it can be read back?
There was a problem hiding this comment.
Is there any expectation that __str__ can be read back? I thought that's what __repr__ is for
There was a problem hiding this comment.
Agreed, just that we accept / on input, so it seems perhaps more logical to re-use that. I also like that it is commonly used to mean "or", while with the , the braces helped to make that clear.
There was a problem hiding this comment.
Clarity and round-tripping are definitely good things. I don't have a strong opinion whether "/" XOR "," is superior as they have a similar set of advantages and disadvantages, depending on use case.
There was a problem hiding this comment.
It would be worthwhile to have operations like u.Pa.physical_type == str(u.Pa.physical_type) work as expected, so I'll change it to /.
We'd have to do a bit more to make it a full round trip, since the / separated physical types are allowed in def_physical_type and PhysicalType and with operators like ==, but not in get_physical_type. This was necessary for the former three cases since there was a previously a str physical type named "momentum/impulse" and we wanted to maintain backward compatibility, but this wasn't a concern for get_physical_type since it previously returned "momentum/impulse" instead accepting it as an argument. Adding the full round trip is out of scope of this PR but it's something that could be added in later.
|
Closes #11617. |
|
Re: #11625 (comment) -- @namurphy , the auto-close feature only kicks in if you state that in the original post. I have updated the original post for you. FYI. |
mhvk
left a comment
There was a problem hiding this comment.
This looks all great to me now! Let's get this in!
|
Thanks for providing a more convenient access here. It seems some operations like |
|
It should work because the |
|
OK, that does work on 4.3rc1, and even |
|
I guess |
|
I'm -0 on |
Description
This pull request is to clean up the implementation of
PhysicalTypein a few places. The changes I'm planning to make are:PhysicalTypeinstances (Make PhysicalType instances support string methods with deprecation warnings #11617)PhysicalType.__str__The changes to__str__would affect #11618, or perhaps be made in #11618 instead.EDIT: This is a follow-up of #11204 , fix #11617