From dcd8990f7b3a52881a8af1a80f089e532245dc35 Mon Sep 17 00:00:00 2001 From: Fumito Hamamura Date: Sun, 8 Dec 2024 18:12:23 +0900 Subject: [PATCH] FIX: Remove deprecated ast.Str --- modelx/core/formula.py | 22 +++++++++++++++++----- modelx/serialize/serializer_4.py | 8 ++++++-- modelx/serialize/serializer_5.py | 8 ++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/modelx/core/formula.py b/modelx/core/formula.py index 541bee9..95faf97 100644 --- a/modelx/core/formula.py +++ b/modelx/core/formula.py @@ -1,5 +1,5 @@ # Copyright (c) 2017-2024 Fumito Hamamura - +import sys # This library is free software: you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation version 3. @@ -202,8 +202,14 @@ def replace_docstring(source: str, docstr: str, insert_indents=False): docstr = "\n".join(lines) - if isinstance(first_stmt, ast.Expr) and isinstance( - first_stmt.value, ast.Str): # Has docstring + if sys.version_info >= (3, 8, 0): + has_docstring = isinstance(first_stmt, ast.Expr) and isinstance( + first_stmt.value, ast.Constant) and isinstance(first_stmt.value.value, str) + else: + has_docstring = isinstance(first_stmt, ast.Expr) and isinstance( + first_stmt.value, ast.Str) + + if has_docstring: # Has docstring src_front = source[:prev_token.startpos] src_back = source[first_stmt.first_token.endpos:] @@ -216,8 +222,14 @@ def replace_docstring(source: str, docstr: str, insert_indents=False): else: # single line - if isinstance(first_stmt, ast.Expr) and isinstance( - first_stmt.value, ast.Str): # Has docstring + if sys.version_info >= (3, 8, 0): + has_docstring = isinstance(first_stmt, ast.Expr) and isinstance( + first_stmt.value, ast.Constant) and isinstance(first_stmt.value.value, str) + else: + has_docstring = isinstance(first_stmt, ast.Expr) and isinstance( + first_stmt.value, ast.Str) + + if has_docstring: # Has docstring src_front = source[:first_stmt.first_token.startpos] src_back = source[first_stmt.first_token.endpos:] diff --git a/modelx/serialize/serializer_4.py b/modelx/serialize/serializer_4.py index 900e6f0..05f6400 100644 --- a/modelx/serialize/serializer_4.py +++ b/modelx/serialize/serializer_4.py @@ -1236,8 +1236,12 @@ class DocstringParser(BaseNodeParser): @classmethod def condition(cls, node, section, atok): if isinstance(node, cls.AST_NODE): - if isinstance(node.value, ast.Str): - return True + if sys.version_info >= (3, 8, 0): + if isinstance(node.value, ast.Constant) and isinstance(node.value.value, str): + return True + else: + if isinstance(node.value, ast.Str): + return True return False diff --git a/modelx/serialize/serializer_5.py b/modelx/serialize/serializer_5.py index cb84e10..f2f79a9 100644 --- a/modelx/serialize/serializer_5.py +++ b/modelx/serialize/serializer_5.py @@ -1242,8 +1242,12 @@ class DocstringParser(BaseNodeParser): @classmethod def condition(cls, node, section, atok): if isinstance(node, cls.AST_NODE): - if isinstance(node.value, ast.Str): - return True + if sys.version_info >= (3, 8, 0): + if isinstance(node.value, ast.Constant) and isinstance(node.value.value, str): + return True + else: + if isinstance(node.value, ast.Str): + return True return False