Skip to content

Fix QLLexer instance as argument to add_lexer #11353

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mbg
Copy link
Member

@mbg mbg commented Nov 21, 2022

add_lexer requires a type as argument in versions of sphinx >= 2.1 (docs). Currently, an instance of QLLexer is passed to it, which causes the docs build to fail if a version of Sphinx >= 3 is used. This PR fixes this so that the documentation can be built successfully.

@mbg mbg self-assigned this Nov 21, 2022
from qllexer import QLLexer
sphinx.add_lexer("ql", QLLexer())
sphinx.add_lexer("ql", QLLexer)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Try this. The callable builtin should tell us whether an object is indeed callable. This way we can remain robust to different versions of Sphinx + pygments being installed, without breaking either local or CI usage.

Suggested change
sphinx.add_lexer("ql", QLLexer)
sphinx.add_lexer("ql", QLLexer() if callable(QLLexer) else QLLexer)

There are actually 3 places in the code we make this call:

  • docs/codeql/conf.py (here)
  • docs/codeql/ql-training/conf.py
  • documentation/restructuredtext/codeql-cli/conf.py within the internal repo

Would you mind updating the first 2 locations in this PR, and then we can tackle the third in an internal PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! Unfortunately, this fails with:

Exception occurred:
  File "/opt/homebrew/lib/python3.10/site-packages/sphinx/highlighting.py", line 130, in get_lexer
    lexer = lexer_classes[lang](**opts)
TypeError: 'QLLexer' object is not callable

I think the problem may be that QLLexer is callable regardless of the Sphinx version used. It is just add_lexer that has different expectations of how you pass the lexer in, depending on the version.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmph yes good point. Thank you for trying. I wonder if we can detect from within Python what the Sphinx version is? If not I think the principled solution is the one in the internal issue - pin to a specific version of the packages.

mbg and others added 2 commits November 22, 2022 11:28
Co-authored-by: Aditya Sharad <6874315+adityasharad@users.noreply.github.com>
@smowton
Copy link
Contributor

smowton commented Feb 4, 2023

@adityasharad if pinning Sphinx or otherwise depending on a consistent version across environments is backlogged, shall we merge this?

@adityasharad
Copy link
Collaborator

As currently written I think this PR is safe enough since it will allow both local and CI usage to keep working 👍. Rerunning tests.

@mbg
Copy link
Member Author

mbg commented Feb 6, 2023

I might be wrong since this was three months ago, but if I remember correctly merging this PR wouldn't do very much. It makes effectively no change in CI and the fallback doesn't actually work with newer versions of Sphinx.

@adityasharad
Copy link
Collaborator

Just realised the same thing from #11353 (comment) !

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.

3 participants