Repository navigation
Cosmo: to/from yaml - #12279
Cosmo: to/from yaml#12279
Conversation
|
👋 Thank you for your draft pull request! Do you know that you can use |
aea2951 to
924623f
Compare
924623f to
01baa15
Compare
f27616f to
07fbc6c
Compare
mhvk
left a comment
There was a problem hiding this comment.
This looks good, so approving.
Just in case, though: Are you sure you want to automatically register every user-defined cosmology subclass? For ESCV, I thought the idea was to be rather conservative and only accept known-safe classes. But if this is mostly for ASDF, then perhaps it is different. Let me ping @taldcroft in case.
The goal is that the astropy YAML |
Is the loader not safe to arbitrary code execution and other shenanigans? I can revert to manually registering classes. |
|
This requires getting someone to import your package and then read in a YAML file you've sent, but it seems somewhat credible: |
|
[1] - checked the code and now I see how that happens. The representer always uses a fixed tag |
|
Alright. Time to get rid of auto-registration! Thanks @mhvk and @taldcroft |
07fbc6c to
6ac43ca
Compare
|
Auto-registration is gone, leaving the question whether to publicly scope the function |
6ac43ca to
0336c95
Compare
| def setup_class(self): | ||
| self.functions = {"to": to_yaml, "from": from_yaml} | ||
|
|
||
| @pytest.fixture(scope="class", autouse=True) | ||
| def setup(self): | ||
| """Setup and teardown for tests.""" | ||
| yield # run tests |
There was a problem hiding this comment.
@nstarman - I see that you and @mhvk are big fans of layers of abstraction and layers of fixtures in testing. I will just say that it makes it quite a lot harder for independent reviewers to understand what the tests are actually doing, and so I'm not that enthusiastic about the approach. I've had a hard time understanding the tests from @mhvk and same applies here.
Simple is better than complex. Having the final "test" of a feature boil down to one or two lines may seem elegant, but I cannot understand the test code directly without reading and understanding the upstream base classes and fixtures.
What is different in these tests from simply looping over each of the built-in cosmology classes and doing dump load to_format and from_format tests? This might avoid or simplify the special-case handling of if tuple(cosmo._init_signature.parameters.values())[-1].kind == 4:.
There was a problem hiding this comment.
Haha. Guilty as charged. Apologies for that.
This test suite is structured so that
- Descriptors on Cosmology, like
meta/read/write/from_format/to_formatare parents toTestCosmologyso that all tests for the descriptor are run on every Cosmology subclass, likeTestFLRWandTestFlatLambdaCDM - For the I/O descriptors, the tests for each format, like "yaml", are parents to the descriptor test so that the format tests are run for each Cosmology subclass.
So the inheritance looks like is TestFLRW(TestCosmology(ToFromFormatTestMixin(ToFromMappingTestMixin , ToFromYAMLTestMixin)))
Where ToFromFormatTestMixin tests the descriptors to/from_format and is fed the tests from each registered format.
What is different in these tests from simply looping over each of the built-in cosmology classes and doing dump load to_format and from_format tests? This might avoid or simplify the special-case handling of if tuple(cosmo._init_signature.parameters.values())[-1].kind == 4:.
Most of these tests are just looping over each of the built-in cosmology classes. But no built-in cosmology has a kwargs in __init__ (that's the tuple(cosmo._init_signature.parameters.values())[-1].kind == 4).
I test this case because a fair number of things were broken in non-intuitive ways, like clone and I/O with metadata, especially when there's partial information allowed like a cosmology <-> dict. I added the kwargs tests a number of PRs ago and fixed things accordingly. Having kwargs necessitates a slightly different treatment of metadata, so it needs to be special cased.
Except I don't think this is the case for yaml unless someone is editing the YAML strings and adding extra args/kwargs! I have a TODO! on the partial information test because I think this quite unlikely. If someone ever hand-assembles a valid Cosmology yaml string, then I'll write the test!
To end a long explanation, tuple(cosmo._init_signature.parameters.values())[-1].kind == 4 is not necessary for this test. I'm removing the check.
| # tests are different if the last argument is a **kwarg | ||
| if tuple(cosmo._init_signature.parameters.values())[-1].kind == 4: | ||
| got = from_format(yml, format="yaml") |
There was a problem hiding this comment.
What's going on here? Needs a better comment in any case.
There was a problem hiding this comment.
See above. This isn't necessary for this test, but in general metadata is handled differently if the cosmology has kwargs.
astropy/astropy/cosmology/io/mapping.py
Lines 104 to 114 in 73c57e1
is the reason. Many of the formats route through
mapping.
There was a problem hiding this comment.
But not YAML in the same way because it is currently assumed that no one is messing with the YAML string, adding arguments.
There was a problem hiding this comment.
I plan on "public" scoping the test suite hopefully for v5.1 release. I've structured the tests so that it should be very easy for 3rd party packages and contributors. Tests for new I/O are a short mixin. Tests for new Cosmologies have a simple inheritance from the parent class tests.
But I definitely need to increase the density of in-line comments and docstrings and explanations!
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
0336c95 to
89828fd
Compare
|
Hello @nstarman 👋! It looks like you've made some changes in your pull request, so I've checked the code again for style. There are no PEP8 style issues with this pull request - thanks! 🎉 Comment last updated at 2021-11-03 21:16:28 UTC |
d12ead7 to
c0dc4c1
Compare
Co-authored-by: Tom Aldcroft <[email protected]> Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
c0dc4c1 to
17afb28
Compare
Cosmology parse from and convert to yaml string.
requires: #12209
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 CIlabel.no-changelog-entry-neededlabel. If this is a manual backport, use theskip-changelog-checkslabel unless special changelog handling is necessary.astropy-botcheck might be missing; do not let the green checkmark fool you.backport-X.Y.xlabel(s) before merge.