From 89bb8b4d66ef00c086ef4202c88e4c9b6114ddb7 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:41:46 -0600 Subject: [PATCH] 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 --- CHANGES.rst | 2 ++ setup.py | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 47dcda3a..c72aadb1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -20,6 +20,8 @@ Bug fixes: * The sanitizer now permits ```` tags. It used to allow ``
`` already. (#423) +* Fix building and installing on Python 3.14, where the ``ast.Str`` class + used to read the package version in ``setup.py`` was removed. (#588) 1.1 ~~~ diff --git a/setup.py b/setup.py index 30ee0575..5ae6d959 100644 --- a/setup.py +++ b/setup.py @@ -84,6 +84,21 @@ def default_environment(): with codecs.open(join(here, 'CHANGES.rst'), 'r', 'utf8') as changes_file: long_description = readme_file.read() + '\n' + changes_file.read() +def string_constant(node): + """Return the value of a string literal node, or None. + + String literals are ast.Constant since Python 3.8 and ast.Str before + that, and ast.Str was removed in Python 3.14, so neither class can be + used for an isinstance check on every supported version. Both node + types are duck-typed here instead: ast.Str stores the string in "s", + ast.Constant in "value". + """ + value = getattr(node, "value", getattr(node, "s", None)) + if isinstance(value, str): + return value + return None + + version = None with open(join(here, "html5lib", "__init__.py"), "rb") as init_file: t = ast.parse(init_file.read(), filename="__init__.py", mode="exec") @@ -92,9 +107,8 @@ def default_environment(): for a in assignments: if (len(a.targets) == 1 and isinstance(a.targets[0], ast.Name) and - a.targets[0].id == "__version__" and - isinstance(a.value, ast.Str)): - version = a.value.s + a.targets[0].id == "__version__"): + version = string_constant(a.value) or version setup(name='html5lib', version=version,