compat: ast.Str -> ast.Constant

Python 3.14 removed the long deprecated `ast.Str` [1]. Rewrite to use
`ast.Constant` conditionally for any version that isn't 3.6.

Since 3.6 is our lowest supported version we don't check for below.

Signed-off-by: Simon de Vlieger <supakeen@redhat.com>
This commit is contained in:
Simon de Vlieger 2025-06-12 15:59:32 +02:00 committed by Tomáš Hozza
parent 010ceb6ba5
commit daed32e462

View file

@ -39,6 +39,8 @@ from .util import osrelease
FAILED_TITLE = "JSON Schema validation failed"
FAILED_TYPEURI = "https://osbuild.org/validation-error"
IS_PY36 = sys.version_info[:2] == (3, 6)
class ValidationError:
"""Describes a single failed validation
@ -456,11 +458,21 @@ class ModuleInfo:
return {}
value = node.value
if not isinstance(value, ast.Str):
return {}
if IS_PY36:
if not isinstance(value, ast.Str):
return {}
# Get the internal value
value = value.s
else:
if not isinstance(value, ast.Constant):
return {}
value = value.value
try:
return json.loads("{" + value.s + "}")
return json.loads("{" + value + "}")
except json.decoder.JSONDecodeError as e:
msg = "Invalid schema: " + e.msg
line = e.doc.splitlines()[e.lineno - 1]
@ -474,7 +486,10 @@ class ModuleInfo:
if not node:
return set()
return {e.s for e in node.value.elts}
if IS_PY36:
return {e.s for e in node.value.elts}
return {e.value for e in node.value.elts}
@classmethod
def load(cls, root, klass, name) -> Optional["ModuleInfo"]: