Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion quartodoc/renderers/md_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,26 @@ def render_annotation(self, el: str) -> str:
Parameters
----------
el:
An object representing a type annotation.
An object representing a type annotation. Can be a structural string
(like '[', ']', ' | '), literal contents, or "None" when None is used
as in "x: int | None".
"""
# Special case for None - it's used as shorthand for NoneType in type annotations
# e.g., "int | None" is common for Optional types
if el == "None":
if self.render_interlinks:
# Render as markdown link like other types
return f"[None](`None`)"
else:
# Render without backticks like any instance (e.g. 1, "a")
return "None"

# For structural strings (brackets, operators, etc.), use existing logic
return sanitize(el, escape_quotes=True)

@dispatch
def render_annotation(self, el: None) -> str:
# this is used to indicate no annotation, not the literal None
return ""

@dispatch
Expand Down
18 changes: 9 additions & 9 deletions quartodoc/tests/__snapshots__/test_renderers.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

## Parameters {.doc-section .doc-section-parameters}

+--------+--------------------------------------------------------------------------------------+-------------------------------+------------+
| Name | Type | Description | Default |
+========+======================================================================================+===============================+============+
| x | [list](`list`)\[[C](`quartodoc.tests.example_signature.C`) \| [int](`int`) \| None\] | The x parameter | _required_ |
+--------+--------------------------------------------------------------------------------------+-------------------------------+------------+
| y | [pathlib](`pathlib`).[Pathlib](`pathlib.Pathlib`) | The y parameter | _required_ |
+--------+--------------------------------------------------------------------------------------+-------------------------------+------------+
| z | | The z parameter (unannotated) | _required_ |
+--------+--------------------------------------------------------------------------------------+-------------------------------+------------+
+--------+------------------------------------------------------------------------------------------------+-------------------------------+------------+
| Name | Type | Description | Default |
+========+================================================================================================+===============================+============+
| x | [list](`list`)\[[C](`quartodoc.tests.example_signature.C`) \| [int](`int`) \| [None](`None`)\] | The x parameter | _required_ |
+--------+------------------------------------------------------------------------------------------------+-------------------------------+------------+
| y | [pathlib](`pathlib`).[Pathlib](`pathlib.Pathlib`) | The y parameter | _required_ |
+--------+------------------------------------------------------------------------------------------------+-------------------------------+------------+
| z | | The z parameter (unannotated) | _required_ |
+--------+------------------------------------------------------------------------------------------------+-------------------------------+------------+
'''
# ---
# name: test_render_annotations_complex_no_interlinks
Expand Down
21 changes: 21 additions & 0 deletions quartodoc/tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ def test_render_annotations_complex_no_interlinks(snapshot):
assert res == snapshot


def test_render_annotation_none():
"""Test rendering of None in type annotations.

None is special in type annotations - it's used as shorthand for NoneType
in annotations like 'int | None'. It should be rendered consistently with
other types like int and str.
"""
renderer_with_links = MdRenderer(render_interlinks=True)
renderer_no_links = MdRenderer(render_interlinks=False)

# Test None with interlinks: should be markdown link
with_links = renderer_with_links.render_annotation("None")
assert (
with_links == "[None](`None`)"
), "None with interlinks should be markdown link"

# Test None without interlinks: should have backticks
without_links = renderer_no_links.render_annotation("None")
assert without_links == "None", "None without interlinks should have no backticks"


@pytest.mark.parametrize("children", ["embedded", "flat"])
def test_render_doc_class(snapshot, renderer, children):
bp = blueprint(Auto(name="quartodoc.tests.example_class.C", children=children))
Expand Down
Loading