Skip to content

RFC: deduplicate implementations for get_config_dir and get_cache_dir#17188

Merged
pllim merged 1 commit intoastropy:mainfrom
neutrinoceros:config/rfc/unify_get_config_dir_get_cache_dir
Oct 17, 2024
Merged

RFC: deduplicate implementations for get_config_dir and get_cache_dir#17188
pllim merged 1 commit intoastropy:mainfrom
neutrinoceros:config/rfc/unify_get_config_dir_get_cache_dir

Conversation

@neutrinoceros
Copy link
Contributor

Description

Follow up to #16997, as requested by @nstarman

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions
Copy link
Contributor

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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@github-actions
Copy link
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@neutrinoceros neutrinoceros added this to the v7.0.0 milestone Oct 16, 2024
@neutrinoceros neutrinoceros force-pushed the config/rfc/unify_get_config_dir_get_cache_dir branch from 503c1d0 to baf2e24 Compare October 16, 2024 09:35
@neutrinoceros neutrinoceros changed the title RFC: deduplicate implementations for get_config_dir and get_cache_dir RFC: deduplicate implementations for get_config_dir and get_cache_dir Oct 16, 2024
if cls._temp_path is not None:
xch = cls._temp_path
path = xch / rootname
if not path.is_file():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this condition didn't exist in get_config_dir, but this implementation seems to pass all tests (while unconditionally calling path.mkdir doesn't), so I figured it's preferable to special casing cache.

@neutrinoceros neutrinoceros marked this pull request as ready for review October 16, 2024 10:29
@neutrinoceros neutrinoceros force-pushed the config/rfc/unify_get_config_dir_get_cache_dir branch 2 times, most recently from 659512d to e0ab985 Compare October 16, 2024 11:43

def _get_dir_path(rootname: str, dirtype: _DirectoryType) -> Path:
match dirtype:
case _DirectoryType.CONFIG:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a little more complicated than what I had in mind. It is very unlikely we will ever expand this, so I don't think we need to make it so flexible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mean it to become more flexible, though is it a bad thing if it merely emerges as such ? this match/case is just a fancy if/elif/else, and the reason I'm combining it with an enum is that it makes the dispatching logic checkable by a type-checker (see #16603). Admittedly it's not required, but should I throw it away after the work is done ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'll let @nstarman have a look then since he initially requested such refactoring and is very into typing stuff. Thanks for the clarification!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit with @pllim here, and would just have the call signature be _get_dir_path(fallback, cls, rootname="astropy") so that one can avoid the whole matching/ifelsing (fine to omit error checking since it is just used internally).

Copy link
Member

@nstarman nstarman Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the Enum + match is cool, but unnecessary in this case. An if-else would be best here unless we plan to add more options to the enum. The flag can be a bool, which is equally good type-wise for 2 options.
As soon as there's a 3rd option we should use the Enum (or another Literal-compatible flag)!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, by popular demand, here's a simpler impl (enum and match/case-free)

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments. Main review probably best done by @nstarman.

raise ValueError(f"Unexpected {dirtype=}")

# If using set_temp_x, that overrides all
if cls._temp_path is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolute nit, but a good opportunity for the walrus: if (xch := cls._tmp_path) is not None: (and nice for symmetry with the below).


def _get_dir_path(rootname: str, dirtype: _DirectoryType) -> Path:
match dirtype:
case _DirectoryType.CONFIG:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit with @pllim here, and would just have the call signature be _get_dir_path(fallback, cls, rootname="astropy") so that one can avoid the whole matching/ifelsing (fine to omit error checking since it is just used internally).

@neutrinoceros neutrinoceros force-pushed the config/rfc/unify_get_config_dir_get_cache_dir branch 2 times, most recently from 659512d to 5a80ec2 Compare October 17, 2024 08:18
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

@neutrinoceros neutrinoceros force-pushed the config/rfc/unify_get_config_dir_get_cache_dir branch from 5a80ec2 to a52c00f Compare October 17, 2024 13:32
@pllim pllim merged commit aa714f5 into astropy:main Oct 17, 2024
@pllim
Copy link
Member

pllim commented Oct 17, 2024

Thanks, all!

@neutrinoceros neutrinoceros deleted the config/rfc/unify_get_config_dir_get_cache_dir branch October 17, 2024 19:02

# first look for XDG_CACHE_HOME
if (
(xdg_config_home := os.getenv("XDG_CONFIG_HOME")) is not None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should have been XDG_CACHE_HOME here I think, so currently the code is using only XDG_CONFIG_HOME I think, not XDG_CACHE_HOME.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. For posterity this is being addressed in #17514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants