Skip to content
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

don't warn on fallback if no highlight theme was requested #1264

Merged
merged 6 commits into from
Mar 28, 2023

Conversation

drammock
Copy link
Collaborator

@drammock
Copy link
Collaborator Author

on second thought, there's a deeper problem here: @HealthyPear says they have accessible-pygments installed, and yet it's setting fallback Tango or Monokai. So below, line 958 ought to have picked up that the theme default (specified in our theme.conf) is a11y-high-contrast-{light,dark}:

# globalcontext sometimes doesn't exist so this ensures we do not error
theme_name = _get_theme_options(app).get(style_key, None)
if theme_name is None and hasattr(app.builder, "globalcontext"):
theme_name = app.builder.globalcontext.get(f"theme_{style_key}")

...but it didn't, which means app.builder did not have a globalcontext attribute. To really fix this we need to figure out some way to reliably get the values from theme.conf other than through app.builder.globalcontext

@drammock drammock marked this pull request as draft March 23, 2023 14:32
@HealthyPear
Copy link
Contributor

accessible-pygments v0.0.3 if it can be of any help

@drammock drammock marked this pull request as ready for review March 26, 2023 22:54
@drammock
Copy link
Collaborator Author

@HealthyPear could you test this branch to see if it now sets a11y-high-contrast-{light|dark} as your highlighting theme, and also doesn't generate the warning?

@drammock
Copy link
Collaborator Author

argh nevermind, what I have now works for rebuilds but not for first builds.

@drammock drammock marked this pull request as draft March 26, 2023 22:58
@drammock drammock marked this pull request as ready for review March 26, 2023 23:01
@drammock
Copy link
Collaborator Author

ok locally this seems to work for both builds and rebuilds. @HealthyPear can you test it on your site please?

theme_name = app.builder.globalcontext.get(f"theme_{style_key}")

if theme_name is None:
theme_name = app.builder.theme.get_options()[style_key]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that you might be able to use this helper function we use elsewhere:

def _get_theme_options(app):

If calling get_options() is the "right" way to do this, maybe we should update that function too. Gah there are way too many ways to do the same thing in Sphinx

Copy link
Collaborator Author

@drammock drammock Mar 28, 2023

Choose a reason for hiding this comment

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

I am already using that helper function 2 lines above; this line is only called if the style key wasn't found by the helper. Most of this you already know @choldgraf, but for completeness / future selves:

  • app.builder.get_theme_config() returns

    • app.builder.config.html_theme (just the theme name)
    • app.builder.config.html_theme_options (dict of theme options). initialized here as an empty dict, it's not clear to me when it's populated, and with what it's populated directly from user's conf.py during app.config.read().
  • app.builder.theme_options is a copy of app.builder.config.html_theme_options.

    • The copy is made during builder.init_templates()
    • Apparently there are cases where that copy never happens (e.g., linkcheck?) so we have that helper function that checks app.builder.config.html_theme_options too.
  • Calling app.builder.theme.get_options() will get just the values in theme.conf without any user overrides.

    • Normally it's called by sphinx as .get_options(app.builder.theme_options), which makes me strongly suspect that app.builder.theme_options includes only the user-provided html_theme_options from their conf.py (and not the theme's defaults). EDIT see strikethrough edit above, it's clear now that html_theme_options does not include theme default values from theme.conf
    • Here's where it's called during sphinx builds: during builder.prepare_writing(), right after globalcontext is created.
    • I suspect our problem has been that builder.prepare_writing() is sometimes never called (e.g. if no files have changed?). That would explain why we had to work around missing globalcontext and also why the theme's defaults weren't available/accessible.

In this case, I think it would be possible / sensible to update the helper function to also check the theme.conf values. Might be a good use case for ChainMap?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sphinx never ceases to amaze me lol. Thanks for putting all of this together!

@drammock
Copy link
Collaborator Author

after further thought I don't think it's good to add app.builder.theme.get_options() to the helper func. There are basically two use cases:

  1. we want the options dict so we can modify it. I don't think we ever want to modify during a build the options that were part of theme.conf
  2. we want the options dict so we can query it.

I considered adding a separate helper func for the query case (to prevent accidental modification) but it seemed like overkill because in the end we only access the theme options 3 times in our __init__, and they're all different:

  • modify theme options in update_config()
  • query pygment_{light|dark}_style
  • query logo and use {} as fallback

the second one we fall back to theme.conf values, but in the third one we actually don't want to check/fall back to theme.conf values (which will be None), we want {} instead.

Ready for merge when CIs are done

Copy link
Collaborator

@12rambau 12rambau left a comment

Choose a reason for hiding this comment

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

thanks for putting all this together !

@HealthyPear
Copy link
Contributor

ok locally this seems to work for both builds and rebuilds. @HealthyPear can you test it on your site please?

sorry for the delay!

I copy/pasted the edits of this merge request into the corresponding file within my conda environment and the problem seems to have disappeared - you have my delayed positive feedback :)

@HealthyPear
Copy link
Contributor

Do you plan to make a bugfix release?

I am sure someone set their Sphinx build to error on warning and this bug would be a stopper for them (obviously totally unrelated to my use case)

@drammock
Copy link
Collaborator Author

Do you plan to make a bugfix release?

hopefully tomorrow. @12rambau if you have bandwidth to do this today, I'd say go for it.

@12rambau
Copy link
Collaborator

0.13.2 including your fix is out

@drammock
Copy link
Collaborator Author

@12rambau 🙏🏻🙏🏻🙏🏻

12rambau added a commit to 12rambau/pydata-sphinx-theme that referenced this pull request Mar 29, 2023
12rambau added a commit that referenced this pull request Mar 30, 2023
* sanitize babel calls

* move the gallery directive to an extention folder

* clean test folder

* split __init__.py

* add links between tests

* drop flake8 from pyproject.toml

* rename folder

* update docstring

* only import modules instead of functions

* ignore bootstrap license

* reintegrate modifications from #1264

* typos / docstring improvements / whitespace

* fix: expose traverse_or_findall in utils

---------

Co-authored-by: Daniel McCloy <dan@mccloy.info>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants