Skip to content

Commit 89bb8b4

Browse files
Fix building on Python 3.14, where ast.Str was removed
setup.py read the package version with an isinstance check against ast.Str, which raises AttributeError on Python 3.14 because the class was removed there after being deprecated since Python 3.8. Neither ast.Str nor ast.Constant can cover every supported version on its own, since literals parse as ast.Str before Python 3.8 and ast.Constant after, so the string value is now extracted by duck-typing both node shapes. Fixes #588
1 parent fd4f032 commit 89bb8b4

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎CHANGES.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Bug fixes:
2020

2121
* The sanitizer now permits ``<summary>`` tags. It used to allow ``<details>``
2222
already. (#423)
23+
* Fix building and installing on Python 3.14, where the ``ast.Str`` class
24+
used to read the package version in ``setup.py`` was removed. (#588)
2325

2426
1.1
2527
~~~

‎setup.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ def default_environment():
8484
with codecs.open(join(here, 'CHANGES.rst'), 'r', 'utf8') as changes_file:
8585
long_description = readme_file.read() + '\n' + changes_file.read()
8686

87+
def string_constant(node):
88+
"""Return the value of a string literal node, or None.
89+
90+
String literals are ast.Constant since Python 3.8 and ast.Str before
91+
that, and ast.Str was removed in Python 3.14, so neither class can be
92+
used for an isinstance check on every supported version. Both node
93+
types are duck-typed here instead: ast.Str stores the string in "s",
94+
ast.Constant in "value".
95+
"""
96+
value = getattr(node, "value", getattr(node, "s", None))
97+
if isinstance(value, str):
98+
return value
99+
return None
100+
101+
87102
version = None
88103
with open(join(here, "html5lib", "__init__.py"), "rb") as init_file:
89104
t = ast.parse(init_file.read(), filename="__init__.py", mode="exec")
@@ -92,9 +107,8 @@ def default_environment():
92107
for a in assignments:
93108
if (len(a.targets) == 1 and
94109
isinstance(a.targets[0], ast.Name) and
95-
a.targets[0].id == "__version__" and
96-
isinstance(a.value, ast.Str)):
97-
version = a.value.s
110+
a.targets[0].id == "__version__"):
111+
version = string_constant(a.value) or version
98112

99113
setup(name='html5lib',
100114
version=version,

0 commit comments

Comments
 (0)