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
structured unit / quantity stuff #13676
structured unit / quantity stuff #13676
Conversation
👋 Thank you for your draft pull request! Do you know that you can use |
0fc4711
to
3b6abc3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, just changing the default would certainly seem fine!
A suggestion for making this easier, although that changes API slightly. But I think it is fine.
3efa533
to
083797b
Compare
astropy/units/structured.py
Outdated
@@ -3,6 +3,8 @@ | |||
This module defines structured units and quantities. | |||
""" | |||
|
|||
from __future__ import annotations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an associated version of python when this will no longer be needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The nice thing is that pyupgrade
in pre-commit
should remove this when its obsolete and we bump the minimum version and run pre-commit
on the repository.
Maybe that should be a step in the release process...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the line here just to avoid importing typing.Union
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 563 has been postponed indefinitely, so I wouldn't expect pyupgrade
to remove the line in question any time soon. However, writing union types as X | Y
instead of Union[X, Y]
proposed by PEP 604 was adopted in Python 3.10, so if the import is only needed for that then it could be removed when support for Python 3.9 is dropped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clearing up the timeline!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mark with a comment # For python < 3.10.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
astropy/units/structured.py
Outdated
------- | ||
StructuredUnit | ||
""" | ||
# Make sure dtype is structured |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for a private function we should be able to assume that the input is correct (i.e., just let the AttributeError
or whatever else happen if not). python is not LBYL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stuck this in for type narrowing since type hints are LBYL. There's no way to indicate that a dtype is structured except by checking. My editor is configured to check on type hints (Astropy makes it very unhappy). Hopefully NumPy will parametrize dtype as a Generic[...]
or have a StructuredDtype
protocol.
We could add that protocol...
@runtime_checkable
class StructuredDtype(Protocol):
@property
def names(self) -> tuple: ...
@property
def fields(self) -> Mapping: ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood, but can you nevertheless remove it? It is one thing to label things to help people and computers understand intent, quite another to make the code slower and more complex... I find it a beauty of python that one does not presume to know beforehand all possible input one might get.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably more typing
than Astropy wants right now, but the suggested Protocol should solve both problems. This function knows that the dtype is structured and there's no checks necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, having that Protocol
would indeed solve it. And could live in separate typing files, where it wouldn't bug me 😺 More seriously, something to suggest to numpy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot it does not do ANY signature checking. The protocol will not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a pity. But let's leave all of this out of this PR. As is, this private function is only called with well-defined input so no checks should be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b10d6d5
to
466ee33
Compare
466ee33
to
bebdbf2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks all great. Approving since what remains is just a question of use of case.
astropy/units/quantity.py
Outdated
@@ -481,13 +481,15 @@ def __new__(cls, value, unit=None, dtype=np.inexact, copy=True, order=None, | |||
# structured dtype of the value. | |||
dtype = unit._recursively_get_dtype(value) | |||
|
|||
USING_DEFAULT_UNIT = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rereading this, I wonder why you use all-caps here. I tended to treat those as "environment variables", i.e., the caps indicate that the variable is defined outside of the scope. But that is not the case here, so maybe one should use lower case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM. I'll repush and merge on pass.
bebdbf2
to
3cebd7d
Compare
Signed-off-by: Nathaniel Starkman (@nstarman) nstarkman@protonmail.com
Necessary for #13669
unit=None
in the constructor and the value is structured.TODO:
Checklist for package maintainer(s)
This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.
Extra CI
label. Codestyle issues can be fixed by the bot.no-changelog-entry-needed
label. If this is a manual backport, use theskip-changelog-checks
label unless special changelog handling is necessary.astropy-bot
check might be missing; do not let the green checkmark fool you.backport-X.Y.x
label(s) before merge.