Skip to content

Commit

Permalink
[doc] Add the possibility to set dynamic values in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Aug 13, 2023
1 parent 267ee57 commit c4dda40
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
34 changes: 28 additions & 6 deletions doc/exts/pylint_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ class OptionsData(NamedTuple):
PYLINT_USERGUIDE_PATH = PYLINT_BASE_PATH / "doc" / "user_guide"
"""Path to the messages documentation folder."""

DYNAMICALLY_DEFINED_OPTIONS: dict[str, dict[str, str]] = {
# Option name, key / values we want to modify
# At least default need to be defined
"py-version": {"default": "sys.version_info[:2]"},
"spelling-dict": {
"choices": "enchant.Broker().list_dicts()",
"default": "",
"help": "Spelling dictionary name. Available dictionaries depends on your local enchant installation",
},
}


def _register_all_checkers_and_extensions(linter: PyLinter) -> None:
"""Registers all checkers and extensions found in the default folders."""
Expand All @@ -49,12 +60,20 @@ def _get_all_options(linter: PyLinter) -> OptionsDataDict:
"""Get all options registered to a linter and return the data."""
all_options: OptionsDataDict = defaultdict(list)
for checker in sorted(linter.get_checkers()):
ch_name = checker.name
for option in checker.options:
all_options[ch_name].append(
checker_name = checker.name
for option_name, option_info in checker.options:
changes_to_do = DYNAMICALLY_DEFINED_OPTIONS.get(option_name, {})
if changes_to_do:
for key_to_change, new_value in changes_to_do.items():
print(
f"Doc value for {option_name!r}['{key_to_change}'] changed to "
f"{new_value!r} (instead of {option_info[key_to_change]!r})"
)
option_info[key_to_change] = new_value
all_options[checker_name].append(
OptionsData(
option[0],
option[1],
option_name,
option_info,
checker,
getmodule(checker).__name__.startswith("pylint.extensions."), # type: ignore[union-attr]
)
Expand Down Expand Up @@ -90,7 +109,10 @@ def _create_checker_section(
continue

# Get current value of option
value = getattr(linter.config, option.name.replace("-", "_"))
if option.name not in DYNAMICALLY_DEFINED_OPTIONS:
value = getattr(linter.config, option.name.replace("-", "_"))
else:
value = DYNAMICALLY_DEFINED_OPTIONS[option.name]["default"]

# Create a comment if the option has no value
if value is None:
Expand Down
6 changes: 3 additions & 3 deletions doc/user_guide/configuration/all-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Standard Checkers
""""""""""""
*Minimum Python version to use for version dependent checks. Will default to the version used to run pylint.*

**Default:** ``(3, 11)``
**Default:** ``sys.version_info[:2]``


--recursive
Expand Down Expand Up @@ -271,7 +271,7 @@ Standard Checkers
persistent = true
py-version = [3, 11]
py-version = "sys.version_info[:2]"
recursive = false
Expand Down Expand Up @@ -1285,7 +1285,7 @@ Standard Checkers

--spelling-dict
"""""""""""""""
*Spelling dictionary name. No available dictionaries : You need to install both the python package and the system dependency for enchant to work..*
*Spelling dictionary name. Available dictionaries depends on your local enchant installation*

**Default:** ``""``

Expand Down

0 comments on commit c4dda40

Please sign in to comment.