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
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class VersionVisitor(ast.NodeVisitor):
def visit_Assign(self, node):
for target in node.targets:
if getattr(target, 'id', None) == '__version__':
self.version = node.value.s
self.version = node.value.value

visitor = VersionVisitor()
visitor.visit(pt)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_docs_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Tests for helpers defined in the Sphinx configuration."""
import ast
from os.path import dirname, exists, join

import pytest

CONF_FPATH = join(dirname(dirname(__file__)), 'docs', 'source', 'conf.py')


def _load_parse_version():
"""Extract ``parse_version`` from ``conf.py`` without importing sphinx."""
if not exists(CONF_FPATH):
pytest.skip('docs/source/conf.py is not available')
tree = ast.parse(open(CONF_FPATH, 'r').read())
for node in tree.body:
if isinstance(node, ast.FunctionDef) and node.name == 'parse_version':
ns = {'exists': exists, 'join': join, 'dirname': dirname}
exec(compile(ast.Module([node], []), CONF_FPATH, 'exec'), ns)
return ns['parse_version']
pytest.skip('conf.py does not define parse_version')


def test_parse_version(tmp_path):
"""``parse_version`` works on Python 3.14, where ``ast.Constant.s`` is gone.

See https://github.com/pyutils/line_profiler/issues/429
"""
parse_version = _load_parse_version()
fpath = tmp_path / 'mod.py'
fpath.write_text("__version__ = '1.2.3'\n")
assert parse_version(str(fpath)) == '1.2.3'