mirror of
https://github.com/vinta/awesome-python.git
synced 2026-05-23 17:40:32 +08:00
refactor(readme_parser): add _href helper to narrow attrGet return type
Extracts a _href(link) helper that returns link.attrGet('href') narrowed
to str (falling back to '') instead of the raw str|int|float|None union.
Replaces all four attrGet('href') or '' call sites with _href(), fixing
ty errors where the widened union leaked into TypedDict url fields.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -72,7 +72,7 @@ def render_inline_html(children: list[SyntaxTreeNode]) -> str:
|
|||||||
case "softbreak":
|
case "softbreak":
|
||||||
parts.append(" ")
|
parts.append(" ")
|
||||||
case "link":
|
case "link":
|
||||||
href = str(escape(child.attrGet("href") or ""))
|
href = str(escape(_href(child)))
|
||||||
inner = render_inline_html(child.children)
|
inner = render_inline_html(child.children)
|
||||||
parts.append(
|
parts.append(
|
||||||
f'<a href="{href}" target="_blank" rel="noopener">{inner}</a>'
|
f'<a href="{href}" target="_blank" rel="noopener">{inner}</a>'
|
||||||
@@ -147,6 +147,12 @@ def _find_child(node: SyntaxTreeNode, child_type: str) -> SyntaxTreeNode | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _href(link: SyntaxTreeNode) -> str:
|
||||||
|
"""Return the link's href attribute as a string, or '' if missing."""
|
||||||
|
href = link.attrGet("href")
|
||||||
|
return href if isinstance(href, str) else ""
|
||||||
|
|
||||||
|
|
||||||
def _find_inline(node: SyntaxTreeNode) -> SyntaxTreeNode | None:
|
def _find_inline(node: SyntaxTreeNode) -> SyntaxTreeNode | None:
|
||||||
"""Find the inline node in a list_item's paragraph."""
|
"""Find the inline node in a list_item's paragraph."""
|
||||||
para = _find_child(node, "paragraph")
|
para = _find_child(node, "paragraph")
|
||||||
@@ -223,7 +229,7 @@ def _parse_list_entries(
|
|||||||
|
|
||||||
# Entry with a link
|
# Entry with a link
|
||||||
name = render_inline_text(first_link.children)
|
name = render_inline_text(first_link.children)
|
||||||
url = first_link.attrGet("href") or ""
|
url = _href(first_link)
|
||||||
desc_html = _extract_description_html(inline, first_link)
|
desc_html = _extract_description_html(inline, first_link)
|
||||||
|
|
||||||
# Collect also_see from nested bullet_list
|
# Collect also_see from nested bullet_list
|
||||||
@@ -239,7 +245,7 @@ def _parse_list_entries(
|
|||||||
if sub_link:
|
if sub_link:
|
||||||
also_see.append(AlsoSee(
|
also_see.append(AlsoSee(
|
||||||
name=render_inline_text(sub_link.children),
|
name=render_inline_text(sub_link.children),
|
||||||
url=sub_link.attrGet("href") or "",
|
url=_href(sub_link),
|
||||||
))
|
))
|
||||||
|
|
||||||
entries.append(ParsedEntry(
|
entries.append(ParsedEntry(
|
||||||
@@ -373,7 +379,7 @@ def _parse_sponsor_item(inline: SyntaxTreeNode) -> ParsedSponsor | None:
|
|||||||
if link is None:
|
if link is None:
|
||||||
return None
|
return None
|
||||||
name = render_inline_text(link.children)
|
name = render_inline_text(link.children)
|
||||||
url = link.attrGet("href") or ""
|
url = _href(link)
|
||||||
|
|
||||||
split_idx = None
|
split_idx = None
|
||||||
for i, child in enumerate(inline.children):
|
for i, child in enumerate(inline.children):
|
||||||
|
|||||||
Reference in New Issue
Block a user