Skip to content

Commit 7ce9d89

Browse files
SilverRainZDeepSeek
andcommitted
fix: Sort ENUM candidates by repr string to handle None correctly
ENUM(None, 'day', 'month', 'year') would crash sorted() because Python 3 cannot compare str with None using '<'. Convert candidates to repr strings before sorting to handle any type safely. Co-authored-by: DeepSeek <service@deepseek.com>
1 parent 43144ee commit 7ce9d89

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/sphinxnotes/project/sphinxnotes_render_ext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def _fmt_type(t) -> str:
4141

4242
def _format_autoconfval_types(valid_types) -> list[str]:
4343
if isinstance(valid_types, ENUM):
44-
return [f'``{c!r}``' for c in sorted(valid_types._candidates)] # pyright: ignore[reportPrivateUsage]
44+
reprs = [repr(c) for c in valid_types._candidates] # pyright: ignore[reportPrivateUsage]
45+
return [f':py:`{r}`' for r in sorted(reprs)]
4546
return [f':py:`{_fmt_type(t)}`' for t in valid_types]
4647

4748

tests/test_render_ext.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Tests for sphinxnotes_render_ext."""
22

3-
from sphinxnotes.project.sphinxnotes_render_ext import _fmt_type, _format_autoconfval_types
3+
from sphinxnotes.project.sphinxnotes_render_ext import (
4+
_fmt_type,
5+
_format_autoconfval_types,
6+
)
47
from sphinx.config import ENUM
58

69

@@ -34,7 +37,11 @@ def test_union_type(self):
3437
class TestFormatAutoconfvalTypes:
3538
def test_enum(self):
3639
result = _format_autoconfval_types(ENUM('tab', 'grid'))
37-
assert result == ["``'grid'``", "``'tab'``"]
40+
assert result == [":py:`'grid'`", ":py:`'tab'`"]
41+
42+
def test_enum_with_none(self):
43+
result = _format_autoconfval_types(ENUM(None, 'day', 'month', 'year'))
44+
assert result == [":py:`'day'`", ":py:`'month'`", ":py:`'year'`", ':py:`None`']
3845

3946
def test_simple_types(self):
4047
result = _format_autoconfval_types(frozenset({str, int}))

0 commit comments

Comments
 (0)