From 742d47fa15a5418f98abf9aaf07edf466e871c81 Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Tue, 6 Jun 2023 11:22:26 -0400 Subject: [PATCH] replace deprecated ast.value.s with ast.value.value (#80968) * replace deprecated ast.value.s with ast.value.value the s attribute is deprecated since Python 3.8 and emits a warning in 3.12 causing some test failures --- .../fragments/80968-replace-deprecated-ast-attr.yml | 2 ++ lib/ansible/parsing/plugin_docs.py | 4 ++-- lib/ansible/playbook/conditional.py | 6 +++--- .../sanity/validate-modules/validate_modules/main.py | 12 ++++++------ .../_util/controller/sanity/yamllint/yamllinter.py | 6 +++--- 5 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/80968-replace-deprecated-ast-attr.yml diff --git a/changelogs/fragments/80968-replace-deprecated-ast-attr.yml b/changelogs/fragments/80968-replace-deprecated-ast-attr.yml new file mode 100644 index 00000000000000..13100ded3d1987 --- /dev/null +++ b/changelogs/fragments/80968-replace-deprecated-ast-attr.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix ``ast`` deprecation warnings for ``Str`` and ``value.s`` when using Python 3.12. diff --git a/lib/ansible/parsing/plugin_docs.py b/lib/ansible/parsing/plugin_docs.py index 7d3dca015cb341..253f62af68e2b1 100644 --- a/lib/ansible/parsing/plugin_docs.py +++ b/lib/ansible/parsing/plugin_docs.py @@ -151,10 +151,10 @@ def read_docstring_from_python_file(filename, verbose=True, ignore_errors=True): if theid == 'EXAMPLES': # examples 'can' be yaml, but even if so, we dont want to parse as such here # as it can create undesired 'objects' that don't display well as docs. - data[varkey] = to_text(child.value.s) + data[varkey] = to_text(child.value.value) else: # string should be yaml if already not a dict - data[varkey] = AnsibleLoader(child.value.s, file_name=filename).get_single_data() + data[varkey] = AnsibleLoader(child.value.value, file_name=filename).get_single_data() display.debug('Documentation assigned: %s' % varkey) diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index 6b685ef6a8555d..163f9129c94935 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -144,9 +144,9 @@ def generic_visit(self, node, inside_call=False, inside_yield=False): inside_call = True elif isinstance(node, ast.Yield): inside_yield = True - elif isinstance(node, ast.Str): + elif isinstance(node, ast.Constant) and isinstance(node.value, text_type): if disable_lookups: - if inside_call and node.s.startswith("__"): + if inside_call and node.value.startswith("__"): # calling things with a dunder is generally bad at this point... raise AnsibleError( "Invalid access found in the conditional: '%s'" % conditional @@ -154,7 +154,7 @@ def generic_visit(self, node, inside_call=False, inside_yield=False): elif inside_yield: # we're inside a yield, so recursively parse and traverse the AST # of the result to catch forbidden syntax from executing - parsed = ast.parse(node.s, mode='exec') + parsed = ast.parse(node.value, mode='exec') cnv = CleansingNodeVisitor() cnv.visit(parsed) # iterate over all child nodes diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py index fd5ea3ae788e17..2b92a56c2055dd 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py @@ -808,22 +808,22 @@ def _get_py_docs(self): continue if grandchild.id == 'DOCUMENTATION': - docs['DOCUMENTATION']['value'] = child.value.s + docs['DOCUMENTATION']['value'] = child.value.value docs['DOCUMENTATION']['lineno'] = child.lineno docs['DOCUMENTATION']['end_lineno'] = ( - child.lineno + len(child.value.s.splitlines()) + child.lineno + len(child.value.value.splitlines()) ) elif grandchild.id == 'EXAMPLES': - docs['EXAMPLES']['value'] = child.value.s + docs['EXAMPLES']['value'] = child.value.value docs['EXAMPLES']['lineno'] = child.lineno docs['EXAMPLES']['end_lineno'] = ( - child.lineno + len(child.value.s.splitlines()) + child.lineno + len(child.value.value.splitlines()) ) elif grandchild.id == 'RETURN': - docs['RETURN']['value'] = child.value.s + docs['RETURN']['value'] = child.value.value docs['RETURN']['lineno'] = child.lineno docs['RETURN']['end_lineno'] = ( - child.lineno + len(child.value.s.splitlines()) + child.lineno + len(child.value.value.splitlines()) ) return docs diff --git a/test/lib/ansible_test/_util/controller/sanity/yamllint/yamllinter.py b/test/lib/ansible_test/_util/controller/sanity/yamllint/yamllinter.py index d6de6117b2328a..ed1afcf3a5efc0 100644 --- a/test/lib/ansible_test/_util/controller/sanity/yamllint/yamllinter.py +++ b/test/lib/ansible_test/_util/controller/sanity/yamllint/yamllinter.py @@ -181,15 +181,15 @@ def check_assignment(statement, doc_types=None): if doc_types and target.id not in doc_types: continue - fmt_match = fmt_re.match(statement.value.s.lstrip()) + fmt_match = fmt_re.match(statement.value.value.lstrip()) fmt = 'yaml' if fmt_match: fmt = fmt_match.group(1) docs[target.id] = dict( - yaml=statement.value.s, + yaml=statement.value.value, lineno=statement.lineno, - end_lineno=statement.lineno + len(statement.value.s.splitlines()), + end_lineno=statement.lineno + len(statement.value.value.splitlines()), fmt=fmt.lower(), )