Skip to content

Commit 2988ede

Browse files
committed
Fixed type annotation in config.
1 parent 2850aa7 commit 2988ede

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

bumpversion/config/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Union
5+
from typing import TYPE_CHECKING, Any, Union
66

77
from bumpversion.config.files import read_config_file
88
from bumpversion.config.models import Config
@@ -37,7 +37,7 @@
3737
}
3838

3939

40-
def get_configuration(config_file: Union[str, Path, None] = None, **overrides) -> Config:
40+
def get_configuration(config_file: Union[str, Path, None] = None, **overrides: Any) -> Config:
4141
"""
4242
Return the configuration based on any configuration files and overrides.
4343

docsrc/reference/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
- [Formatting context](formatting-context.md)
77
- [Version parts](version-parts.md)
88
- [Search and replace configuration](search-and-replace-config.md)
9-
- [API](api/bumpversion.md)
9+
- [API](api/bumpversion/index.md)

mkdocs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ plugins:
7070
- git-revision-date-localized
7171
- git-authors:
7272
show_email_address: false
73+
exclude:
74+
- reference/api/*
7375
- include-markdown
7476
- drawio
7577
- literate-nav:
@@ -98,6 +100,7 @@ plugins:
98100
merge_init_into_class: true
99101
separate_signature: true
100102
show_docstring_parameters: true
103+
show_root_toc_entry: true
101104
show_signature_annotations: true
102105
show_source: false
103106
show_symbol_type_heading: true

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dev = [
5858
"pre-commit",
5959
]
6060
docs = [
61+
"black",
6162
"markdown-customblocks",
6263
"mdx-truly-sane-lists",
6364
"mkdocs",
@@ -69,7 +70,6 @@ docs = [
6970
"mkdocs-include-markdown-plugin",
7071
"mkdocs-literate-nav",
7172
"mkdocs-material",
72-
"mkdocstrings",
7373
"mkdocstrings[python]",
7474
"python-frontmatter",
7575
]

tools/update_frontmatter.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
"""Update frontmatter of markdown files."""
3+
4+
import argparse
5+
from pathlib import Path
6+
from typing import Any, Dict, Optional
7+
8+
import frontmatter
9+
10+
11+
def extract_main_heading(markdown_content: str) -> Optional[str]:
12+
"""
13+
Extracts the first level 1 heading from the provided Markdown content.
14+
15+
Args:
16+
markdown_content: A string containing Markdown text.
17+
18+
Returns:
19+
The text of the first level 1 heading, or None if no such heading is found.
20+
"""
21+
lines = markdown_content.split("\n")
22+
23+
return next((line[2:] for line in lines if line.startswith("# ")), None)
24+
25+
26+
def calc_title(post: frontmatter.Post) -> str:
27+
"""Calculate the title of the post."""
28+
return extract_main_heading(post.content) or post.get("title", "")
29+
30+
31+
def calc_comment(post: frontmatter.Post) -> bool:
32+
"""Calculate if the post has comments."""
33+
return bool(post.get("comments", True))
34+
35+
36+
def calculate_update(post: frontmatter.Post) -> dict:
37+
"""Calculate if the frontmatter needs to be updated."""
38+
expected_title = calc_title(post)
39+
expected_comment = calc_comment(post)
40+
update: Dict[str, Any] = {}
41+
if expected_title and expected_title != post.get("title"):
42+
update["title"] = expected_title
43+
if expected_comment != post.get("comments"):
44+
update["comments"] = expected_comment
45+
return update
46+
47+
48+
def process_file(markdown_path: Path) -> None:
49+
"""Process a single file."""
50+
if not (markdown_path.is_file() and markdown_path.suffix == ".md"):
51+
return
52+
raw_text = markdown_path.read_text()
53+
post = frontmatter.loads(raw_text)
54+
55+
update = calculate_update(post)
56+
if update:
57+
for key, value in update.items():
58+
post[key] = value
59+
new_text = frontmatter.dumps(post)
60+
print(f"Updating {markdown_path}") # noqa: T201
61+
markdown_path.write_text(new_text)
62+
63+
64+
def parse_args() -> argparse.Namespace:
65+
"""Parse command line arguments."""
66+
parser = argparse.ArgumentParser(description="Update frontmatter of markdown files")
67+
parser.add_argument("markdown_path", type=str, nargs="+", help="Path or glob to markdown files")
68+
return parser.parse_args()
69+
70+
71+
if __name__ == "__main__":
72+
args = parse_args()
73+
documents = args.markdown_path
74+
for document in documents:
75+
for path in Path().glob(document):
76+
process_file(path)

0 commit comments

Comments
 (0)