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

Add yamllint check for RST code listings #2385

Open
wants to merge 7 commits into
base: devel
Choose a base branch
from

Conversation

felixfontein
Copy link
Collaborator

@felixfontein felixfontein commented Jan 31, 2025

This was harder than I expected since the line number information is bonkers literal_block nodes, and the column information not there completely.

This currently fails in quite a few places. Once #2382 is merged this should get better though...

Also I currently disabled most problems - we also have to figure out the config we want to use. I guess the most pressing issues are syntax errors when it's simply not a YAML file at all ;)

Ref: #2382 (comment)

Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

Have you considered using sphinxawesome-codelinter? Their README has yamllint as an example: https://github.com/kai687/sphinxawesome-codelinter?tab=readme-ov-file#configure.

@felixfontein
Copy link
Collaborator Author

Have you considered using sphinxawesome-codelinter? Their README has yamllint as an example: https://github.com/kai687/sphinxawesome-codelinter?tab=readme-ov-file#configure.

I haven't because I wasn't aware of it. Looking at it, I also think it would perform worse than the code in this PR:

  1. It does not adjust line and column offsets, you have to do your own calculations.
  2. It does not warn about codeblocks with missing language, unsupported languages, or use of ::.

@felixfontein
Copy link
Collaborator Author

Currently contains #2408. Will rebase once that's merged.

Copy link
Contributor

@oraNod oraNod left a comment

Choose a reason for hiding this comment

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

@felixfontein Looks really good to me. I had a couple minor suggestions on warnings. Other than that I think it's great. Maybe we could add some docstrings to classes to improve things further.

Thanks very much for working on this!

@webknjaz
Copy link
Member

Have you considered using sphinxawesome-codelinter? Their README has yamllint as an example: kai687/sphinxawesome-codelinter#configure.

I haven't because I wasn't aware of it. Looking at it, I also think it would perform worse than the code in this PR:

1. It does not adjust line and column offsets, you have to do your own calculations.

2. It does not warn about codeblocks with missing language, unsupported languages, or use of `::`.

Might be worth contributing this code there then?

"path": self.__path,
"line": node.line or "unknown",
"col": 0,
"message": f"Warning: found unknown literal block! Check for double colons '::'. If that is not the cause, please report this warning. It might indicate a bug in the checker or an unsupported Sphinx directive. Node: {node!r}; attributes: {node.attributes}; content: {node.rawsource!r}",
Copy link
Collaborator Author

@felixfontein felixfontein Feb 28, 2025

Choose a reason for hiding this comment

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

Suggested change
"message": f"Warning: found unknown literal block! Check for double colons '::'. If that is not the cause, please report this warning. It might indicate a bug in the checker or an unsupported Sphinx directive. Node: {node!r}; attributes: {node.attributes}; content: {node.rawsource!r}",
"message": f"Warning: found unknown literal block! Check for double colons '::' and convert them to code blocks. If that is not the cause, please report this warning. It might indicate a bug in the checker or an unsupported Sphinx directive. Node: {node!r}; attributes: {node.attributes}; content: {node.rawsource!r}",

@oraNod ^ is that better?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right. It's implicit code blocks (I guess that's the right way to say it). I forgot you could just have naked :: double colons. Anyway this looks like a good improvement to me.

@felixfontein
Copy link
Collaborator Author

Have you considered using sphinxawesome-codelinter? Their README has yamllint as an example: kai687/sphinxawesome-codelinter#configure.

I haven't because I wasn't aware of it. Looking at it, I also think it would perform worse than the code in this PR:

1. It does not adjust line and column offsets, you have to do your own calculations.

2. It does not warn about codeblocks with missing language, unsupported languages, or use of `::`.

Might be worth contributing this code there then?

Definitely. I don't think I have time for that anytime soon, though...

@felixfontein
Copy link
Collaborator Author

I've also made sure that regular YAML files in the repository pass yamllint .. (I think if we have a yamllint config, it should apply to all YAML files.) I'm not sure though where to add it into CI. Should it be a separate GitHub workflow? A separate Nox session (part of lint)? Should it be a separate checker (these should usually only work on files in docs/, not in other parts of the repo)?

@oraNod
Copy link
Contributor

oraNod commented Mar 3, 2025

I've also made sure that regular YAML files in the repository pass yamllint .. (I think if we have a yamllint config, it should apply to all YAML files.) I'm not sure though where to add it into CI. Should it be a separate GitHub workflow? A separate Nox session (part of lint)? Should it be a separate checker (these should usually only work on files in docs/, not in other parts of the repo)?

I think the current scope is good, just the docs/ folder. I also think it is fine to leave as part of the checkers session instead of a new nox session. I like that the checkers session is a light enough wrapper. Maybe we don't want to have too much overlapping logic.

Maybe the only thing really missing is an entry in the README to explain how folks can use the checkers test to verify their changes as part of a PR? https://github.com/ansible/ansible-documentation/blob/devel/README.md#running-automated-tests

@webknjaz
Copy link
Member

webknjaz commented Mar 6, 2025

I've also made sure that regular YAML files in the repository pass yamllint .. (I think if we have a yamllint config, it should apply to all YAML files.) I'm not sure though where to add it into CI. Should it be a separate GitHub workflow? A separate Nox session (part of lint)? Should it be a separate checker (these should usually only work on files in docs/, not in other parts of the repo)?

In a typical Python project, I'd have it as a part of the pre-commit config. Here, it might make sense to add it to lint. And the config would have to be in the repo root. Example: https://github.com/ansible/awx-plugins/blob/e5f7468/.yamllint.

@@ -1,7 +1,7 @@
---

name: Ansible package docs build
on:
"on":
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, dropping a config like https://github.com/ansible/awx-plugins/blob/e5f7468/.yamllint into the repo would allow these.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Generally enabling on just for a handful of special YAML files sounds like a bad idea.

@webknjaz
Copy link
Member

webknjaz commented Mar 6, 2025

Have you considered using sphinxawesome-codelinter? Their README has yamllint as an example: kai687/sphinxawesome-codelinter#configure.

I haven't because I wasn't aware of it. Looking at it, I also think it would perform worse than the code in this PR:

1. It does not adjust line and column offsets, you have to do your own calculations.

2. It does not warn about codeblocks with missing language, unsupported languages, or use of `::`.

Might be worth contributing this code there then?

Definitely. I don't think I have time for that anytime soon, though...

Ack. I just realized I'd love to use this elsewhere myself.

@felixfontein
Copy link
Collaborator Author

Here, it might make sense to add it to lint.

Either it needs a new session, with new lock files, or an existing session needs to be extended (with new requirements). (Neither is anything I want to do in this PR.)

And the config would have to be in the repo root.

It's already there.

@webknjaz
Copy link
Member

webknjaz commented Mar 6, 2025

And the config would have to be in the repo root.

It's already there.

Are you talking about this repo? I don't see it.

@felixfontein
Copy link
Collaborator Author

webknjaz added a commit to webknjaz/ansible--awx-plugins that referenced this pull request Mar 6, 2025
This uses a third-party Sphinx extension now [[1]]. In the future,
though, we should explore using the solution from
ansible-documentation [[2]].

[1]: https://github.com/kai687/sphinxawesome-codelinter
[2]: ansible/ansible-documentation#2385
webknjaz added a commit to webknjaz/ansible--awx-plugins that referenced this pull request Mar 6, 2025
This uses a third-party Sphinx extension now [[1]]. In the future,
though, we should explore using the solution from
ansible-documentation [[2]].

[1]: https://github.com/kai687/sphinxawesome-codelinter
[2]: ansible/ansible-documentation#2385
@webknjaz
Copy link
Member

webknjaz commented Mar 6, 2025

#2385 (files)

Oh, I thought you were talking about something pre-existing :) Thanks for pointing this out!

Copy link
Contributor

@oraNod oraNod left a comment

Choose a reason for hiding this comment

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

Maybe some of the more experienced Pythonistas around here will have more feedback, but this looks really solid to me.

Thanks for doing this @felixfontein It's a great improvement to the quality of the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants