Skip to content

Commit c039e7a

Browse files
authored
Merge pull request #240 from dhellmann/fix-issue-234-none-source
fix: handle TypeError when source is None in Sphinx 8.2
2 parents 7b84d6a + 92b510f commit c039e7a

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jobs:
1616
- docs:build
1717
- docs:check
1818
- test:lint
19-
- test:pkglint
2019

2120
steps:
2221
- uses: actions/checkout@v6
@@ -26,7 +25,7 @@ jobs:
2625
- name: Set up Python
2726
uses: actions/setup-python@v6
2827
with:
29-
python-version: '3.x'
28+
python-version: "3.x"
3029

3130
- name: Install dependencies
3231
run: python -m pip install hatch

docs/source/history.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
commandline
99
config
1010
docstring
11+
Dutta
1112
emacs
1213
env
14+
Gulden
1315
Homebrew
1416
libenchant
1517
macOS
1618
namespace
19+
Nico
1720
repo
1821
scm
1922
setuptools
@@ -27,6 +30,9 @@ Next
2730
- Modernize packaging using hatch and hatchling.
2831
- List Python 3.13 as supported.
2932
- Add automatically generated documentation for key modules.
33+
- `#234 <https://github.com/sphinx-contrib/spelling/issues/234>`__ Fix bug
34+
where nodes with no source information would cause a TypeError. Reported by
35+
Trevor Gross, Ronnie Dutta, and Nico Gulden.
3036

3137
Bug Fixes
3238
---------

sphinxcontrib/spelling/builder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ def filter(n):
228228
# the full path, so convert all to relative path
229229
# for consistency.
230230
source, node_lineno = docutils.utils.get_source_line(node)
231-
source = osutil.relpath(source)
231+
if source is not None:
232+
source = osutil.relpath(source)
233+
else:
234+
# Some nodes (e.g., programmatically generated) may not have
235+
# source information. Use a placeholder.
236+
source = "<unknown>"
232237

233238
# Check the text of the node.
234239
misspellings = self.checker.check(node.astext())

tests/test_builder.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,77 @@ def test_only_directive(sphinx_project):
677677
)
678678
assert "(whaat)" in output_text
679679
assert "(teh)" not in output_text
680+
681+
682+
def test_nodes_with_none_source(sphinx_project):
683+
# Reproduces https://github.com/sphinx-contrib/spelling/issues/234
684+
# Tests handling of nodes where get_source_line returns None for source
685+
srcdir, outdir = sphinx_project
686+
687+
# Create an extension that patches docutils.utils.get_source_line to return None
688+
# This directly simulates the bug condition
689+
add_file(
690+
srcdir,
691+
"mock_none_source.py",
692+
"""
693+
import docutils.utils
694+
695+
696+
# Store the original function
697+
_original_get_source_line = docutils.utils.get_source_line
698+
699+
700+
def patched_get_source_line(node):
701+
'''Return None for source to simulate issue #234'''
702+
# Call original to get line number
703+
source, lineno = _original_get_source_line(node)
704+
# But return None for source to trigger the bug
705+
return (None, lineno)
706+
707+
708+
def setup(app):
709+
# Patch the function when building
710+
def on_build_finished(app, exception):
711+
# Restore original after build
712+
docutils.utils.get_source_line = _original_get_source_line
713+
714+
# Patch before doctree-read
715+
def on_doctree_read(app, doctree):
716+
docutils.utils.get_source_line = patched_get_source_line
717+
718+
app.connect('doctree-read', on_doctree_read, priority=1)
719+
app.connect('build-finished', on_build_finished)
720+
721+
return {'version': '0.1'}
722+
""",
723+
)
724+
725+
add_file(
726+
srcdir,
727+
"conf.py",
728+
f"""
729+
import sys
730+
sys.path.insert(0, r'{srcdir}')
731+
extensions = ['sphinxcontrib.spelling', 'mock_none_source']
732+
""",
733+
)
734+
735+
add_file(
736+
srcdir,
737+
"contents.rst",
738+
"""
739+
Test Document
740+
=============
741+
742+
This text has a mispeling that should be caught.
743+
""",
744+
)
745+
746+
# Without the fix, this will crash with:
747+
# TypeError: expected str, bytes or os.PathLike object, not NoneType
748+
# With the fix, it should handle None gracefully and produce output
749+
stdout, stderr, output_text = get_sphinx_output(srcdir, outdir, "contents")
750+
751+
# The spelling check should still work and find the misspelling
752+
assert output_text is not None
753+
assert "(mispeling)" in output_text

0 commit comments

Comments
 (0)