Repository navigation
ENH: overhaul cache and config dir discovery - #19575
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.
|
1c577a3 to
f661f36
Compare
666ff4b to
ebde048
Compare
|
I think I found a contradiction within the goals I originally set out to reach in this PR. I'll list them explicitly for clarity:
The first point is reached, but I'm not sure it's possible (or worth the effort) to get 2) and 3) It seems to me that an implication is that I must renounce 2), and in doing so I might also loose some of the benefit of having two competing APIs, though I expect not all of it (it's still nice to have an API that returns Path objects available, and changing the return type of existing functions is too much of a change to warrant doing it). I'll think about this problem some more. |
a99b136 to
1d9786e
Compare
|
I'm going to run with dropping 3). I'll push a couple separate commits with the following content
I know that some other test will break as a result, but locally I only get about a dozen failures, and nothing that looks unredeemable, but I want to see if CI reveals any more. |
astrofrog
left a comment
There was a problem hiding this comment.
This looks good to me now, modulo a small nit below.
From what I can tell I think the issue of the config being created in the home directory when running pytest might not be fully fixed, but I'll let you decide whether to address this here or in a follow-up PR. The cleanest fix might be to go back to defining the temporary directories in conftest.py.
I am approving this since I will not be working until Thursday and I think it's in a good enough shape to merge. Thank you for all your work on this @neutrinoceros !
I'll let you decide if you want to squash the commits, feel free to merge once you feel it's ready.
- conform to `XDG` specifications by default - enforce true isolation by never creating symlinks - avoid special-casing Windows - add support for `ASTROPY_(CACHE|CONFIG)_DIR` environment variables - add safer context managers for defining temporary cache and config directories - soft-deprecate `set_temp_cache` and `set_temp_config`
16e788e to
554df66
Compare
|
thanks a bunch Tom. I just squashed the history and will set auto-merge now, just so that it stops growing. I'll make sure to address any lingering issue with test pollution as a follow up PR, thought that one will not be that urgent since it could go in after the FF. |
| (see `here <https://wiki.archlinux.org/index.php/XDG_Base_Directory_support>`_ | ||
| for more details), but ``astropy`` will use them on any OS as a more | ||
| general means to know where user-specific configurations should be written. | ||
| ``ASTROPY_CACHE_DIR`` takes precedence over ``XDG_CACHE_HOME``, and defines an |
There was a problem hiding this comment.
The formatting here is a bit weird when rendered:
https://docs.astropy.org/en/latest/environment_variables.html
There was a problem hiding this comment.
Thanks for spotting this. I can never get rst right... I'll make sure to address it tomorrow !
|
My main concern with this PR, especially merged so close to feature freeze is that right off the bat, it silently changes where my cache and config goes. For years now, I have them here:
Now when I install astropy with this patch:
There are no warnings. A typical user won't notice and now will have dead cache and config in the old location. And might wonder why their existing cache and config are no longer in effect.
This is definitely a problem. My HOME quota is set by the IT department and is pretty small. So locally running astropy test suite filling up my HOME is very bad. This should be fixed before v8.0 is released. The best outcome is have pytest tmp_path control where the stuff is going, because just redirecting to |
|
For completeness, I opened a dedicated thread at https://groups.google.com/g/astropy-dev/c/G0cVLPbEl6M |
this is perfectly addressable in a follow up PR and could even be merged beyond the feature freeze. We have a couple decisions to make when the old default path exists but the new one doesn't:
I think what's best depends whether we want to support legacy locations indefinitely or not. At the moment I don't think that'd be a huge maintenance burden so I can give it a try. If that's the path we take, I don't see a particularly convincing reason to emit a warning. Thoughts ?
I addressed it already, and I'll double check tomorrow, but I want to clarify that the pollution in question was just about the default dir location being created if it didn't exist yet. The dir was not populated by a test session and just remained empty. It is a problem best addressed soon, but it's not nearly as bad as it seems, and it wouldn't blow away your HOME quotas. |
Hopefully there will be more opinions (than just mine) to help us decide. Philosophy of least surprise would be to use legacy dir but maybe not everyone wants that... 🤪 |
|
That's also what pip does btw:
|
| :func:`~astropy.config.get_config_dir`:: | ||
| :func:`~astropy.config.get_config_dir_path`:: | ||
|
|
||
| >>> from astropy.config import get_config_dir |
There was a problem hiding this comment.
Do we also want to mention get_config_dir? Sometimes you just want the string, not Path, and we already have a function for that.
There was a problem hiding this comment.
if you want a string, str is builtin, so you don't even need an import. Even if we wanted to deprecate the str-returning function, it'd remain trivially supported
There was a problem hiding this comment.
(long term I would like to deprecate them, which is why I'm not keen on recommending them in docs)
There was a problem hiding this comment.
Is there a follow-up issue to deprecate them in the future?
There was a problem hiding this comment.
No, but it's not a big deal if it doesn't happen, just an idea I might come back to in a while.
There was a problem hiding this comment.
I opened #19609 so we don't forget about it. We can close it as won't fix if you change your mind. Thanks.
|
👍 from me on the idea of warning users if their cache directory has changed. I also have a tightly limited $HOME drive, and astroquery caching (which uses the same system and is probably affected?) can result in large data dumps. A warning could be emitted at import time if the old cache directory exists but is not being used. |
|
|
||
| # See what happens when set_temp_cache is pointed at a file | ||
| with pytest.raises(OSError): | ||
| with pytest.raises(Exception) as _: |
There was a problem hiding this comment.
Why change OSError to general Exception here and why do we need as _?
There was a problem hiding this comment.
because the exact exception type is not in our control and changed with my rewrite. The as _ is needed to satisfy ruff, otherwise it'll flag the call to pytest.raises as imprecise because I don't use a match arg
we can't warn at import time, because the config/cache dir can really be looked up at any point, not specifically at import, and change dynamically. |
|
Just so we don't lose track, I opened #19611 that would block v8.0.0 release. |
|
@keflavich we ended up falling back to "legacy" dirname if it exists, instead of issuing warning. Are you okay with this? Please let us know if it doesn't work as expected for you. Thanks! |
|
yes, that is great! Thanks for keeping things unbroken! |
Description
This project is a deliverable on my current contract.
I've been working on this for a couple days. It isn't ready yet but I think I'm at the point where I want to see how it behaves in CI as I refine it.
based off #19559To discuss:
should we also change the default cache/config location to match XDG standards ? (Why doesn't astropy use ~/.config/astropy by default (on Unix-like systems)? #6803)(yes: this took a bit more effort because I didn't consider this idea soon enough, but the end result is more satisfying in more than one way)should xdg variables only be supported on UNIX ?(no: this was explicitly documented as cross-platform support before, no reason to change it)do we actually want to look up environment variables every time(probably not: it'd make testing much harder, which would be a shame for functions that are most useful in testing other code...)get_cache_diris called, or would it be enough to look once at import time ?(note that I ended up answering all of the above myself, though I'm still happy to take in any additional feedback)
TODO:
get_cache_dir_pathandget_config_dir_path)Notable differences between my new context managers and the existing APIs:
delete=True)deleteis keyword-onlydir_(path) is positional onlysuffixandprefixyieldvalue is aPath(not astr)About half the diff, at the time of opening, is shared between #19559 and a verbatim backport from the standard library that will be removed as soon as we drop support for Python 3.111
AI Discolure:
AI (Claude) was used in review (@astrofrog) to discover (and propose fixes to) 4 actual bugs in the implementation.
The design is entirely human and no code generation was used beyond drafting 3 regression tests associated with the aforementioned review.
Footnotes
3.12.0 also gets patches from 3.12.1, so we'd have to require
>=3.12.1to actually remove the shim ↩