Skip to content

Commit 35bbd3f

Browse files
authored
Fix mkdocsstrings config (#412)
- **Updated the deprecated `import` config key to `inventories`** - **Fix wrongly located `paths` key in mkdocs.yml**
2 parents 9406b99 + 45d1949 commit 35bbd3f

File tree

9 files changed

+85
-15
lines changed

9 files changed

+85
-15
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ But you might still need to adapt your code:
2727
### Cookiecutter template
2828

2929
- New warning ignores for protobuf gencode versions in pytest.
30+
- mkdocstrings: Updated the deprecated `import` config key to `inventories` in `mkdocs.yml`.
3031

3132
## Bug Fixes
3233

3334
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
3435

3536
### Cookiecutter template
3637

37-
<!-- Here bug fixes for cookiecutter specifically -->
38+
- mkdocstrings: Move `paths` key to the right section in `mkdocs.yml`.

cookiecutter/migrate.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ def main() -> None:
3434
# Add a separation line like this one after each migration step.
3535
print("=" * 72)
3636
migrate_filterwarnings(Path("pyproject.toml"))
37+
print(
38+
"Renaming the deprecated mkdocstrings `import` to `inventories` in `mkdocs.yml`..."
39+
)
40+
print("=" * 72)
41+
replace_file_contents_atomically(
42+
"mkdocs.yml", " import:", " inventories:"
43+
)
44+
print("=" * 72)
45+
print("Fixing wrongly located `paths` keys in mkdocs.yml...")
46+
migrate_mkdocs_yaml(Path("mkdocs.yml"))
3747
print("=" * 72)
3848
print("Migration script finished. Remember to follow any manual instructions.")
3949
print("=" * 72)
@@ -152,6 +162,65 @@ def migrate_filterwarnings(path: Path) -> None:
152162
)
153163

154164

165+
def migrate_mkdocs_yaml(file_path: Path) -> None:
166+
"""Migrate the mkdocs.yml file to fix the `paths` key location."""
167+
if not file_path.is_file():
168+
manual_step(f"File {file_path} does not exist, skipping automatic migration.")
169+
return
170+
171+
python_section = " python:"
172+
options_section = " options:"
173+
bad_paths_config = " paths:"
174+
175+
lines = file_path.read_text(encoding="utf-8").splitlines(keepends=True)
176+
needs_migration = False
177+
paths = ""
178+
in_python = False
179+
in_options = False
180+
181+
# 1) Detect whether there's a python_section followed by options_section
182+
# and then bad_paths_config in that block.
183+
for line in lines:
184+
if line.startswith(python_section):
185+
in_python = True
186+
in_options = False
187+
continue
188+
if in_python and line.startswith(options_section):
189+
in_options = True
190+
continue
191+
if in_options and line.startswith(bad_paths_config):
192+
needs_migration = True
193+
paths = line[len(bad_paths_config) :].strip()
194+
break
195+
# If indentation drops back below python-level, stop looking in this block
196+
if in_python and not line.startswith(" ") and not line.isspace():
197+
in_python = False
198+
in_options = False
199+
200+
if not needs_migration:
201+
return
202+
203+
# 2) Perform the line-based rewrite:
204+
new_lines: list[str] = []
205+
inserted_paths = False
206+
207+
for line in lines:
208+
# When we hit the python_section line, insert new paths config directly under it
209+
if line.startswith(python_section) and not inserted_paths:
210+
new_lines.append(line)
211+
new_lines.append(f" paths: {paths}\n")
212+
inserted_paths = True
213+
continue
214+
215+
# After inserting, drop the old " paths:" line
216+
if inserted_paths and line.startswith(bad_paths_config):
217+
continue
218+
219+
new_lines.append(line)
220+
221+
file_path.write_text("".join(new_lines), encoding="utf-8")
222+
223+
155224
def apply_patch(patch_content: str) -> None:
156225
"""Apply a patch using the patch utility."""
157226
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)

cookiecutter/{{cookiecutter.github_repo_name}}/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["{{cookiecutter | src_path}}"]
106107
options:
107-
paths: ["{{cookiecutter | src_path}}"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false
@@ -116,7 +116,7 @@ plugins:
116116
show_source: true
117117
show_symbol_type_toc: true
118118
signature_crossrefs: true
119-
import:
119+
inventories:
120120
# TODO(cookiecutter): You might want to add other external references here
121121
# See https://mkdocstrings.github.io/python/usage/#import for details
122122
- https://docs.python.org/3/objects.inv

mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ plugins:
101101
default_handler: python
102102
handlers:
103103
python:
104+
paths: ["src"]
104105
options:
105-
paths: ["src"]
106106
docstring_section_style: spacy
107107
inherited_members: true
108108
merge_init_into_class: false
@@ -114,7 +114,7 @@ plugins:
114114
show_source: true
115115
show_symbol_type_toc: true
116116
signature_crossrefs: true
117-
import:
117+
inventories:
118118
- https://cookiecutter.readthedocs.io/en/stable/objects.inv
119119
- https://docs.python.org/3/objects.inv
120120
- https://mkdocstrings.github.io/objects.inv

tests_golden/integration/test_cookiecutter_generation/actor/frequenz-actor-test/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false
@@ -116,7 +116,7 @@ plugins:
116116
show_source: true
117117
show_symbol_type_toc: true
118118
signature_crossrefs: true
119-
import:
119+
inventories:
120120
# TODO(cookiecutter): You might want to add other external references here
121121
# See https://mkdocstrings.github.io/python/usage/#import for details
122122
- https://docs.python.org/3/objects.inv

tests_golden/integration/test_cookiecutter_generation/api/frequenz-api-test/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["py"]
106107
options:
107-
paths: ["py"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false
@@ -116,7 +116,7 @@ plugins:
116116
show_source: true
117117
show_symbol_type_toc: true
118118
signature_crossrefs: true
119-
import:
119+
inventories:
120120
# TODO(cookiecutter): You might want to add other external references here
121121
# See https://mkdocstrings.github.io/python/usage/#import for details
122122
- https://docs.python.org/3/objects.inv

tests_golden/integration/test_cookiecutter_generation/app/frequenz-app-test/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false
@@ -116,7 +116,7 @@ plugins:
116116
show_source: true
117117
show_symbol_type_toc: true
118118
signature_crossrefs: true
119-
import:
119+
inventories:
120120
# TODO(cookiecutter): You might want to add other external references here
121121
# See https://mkdocstrings.github.io/python/usage/#import for details
122122
- https://docs.python.org/3/objects.inv

tests_golden/integration/test_cookiecutter_generation/lib/frequenz-test-python/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false
@@ -116,7 +116,7 @@ plugins:
116116
show_source: true
117117
show_symbol_type_toc: true
118118
signature_crossrefs: true
119-
import:
119+
inventories:
120120
# TODO(cookiecutter): You might want to add other external references here
121121
# See https://mkdocstrings.github.io/python/usage/#import for details
122122
- https://docs.python.org/3/objects.inv

tests_golden/integration/test_cookiecutter_generation/model/frequenz-model-test/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false
@@ -116,7 +116,7 @@ plugins:
116116
show_source: true
117117
show_symbol_type_toc: true
118118
signature_crossrefs: true
119-
import:
119+
inventories:
120120
# TODO(cookiecutter): You might want to add other external references here
121121
# See https://mkdocstrings.github.io/python/usage/#import for details
122122
- https://docs.python.org/3/objects.inv

0 commit comments

Comments
 (0)