Skip to content

Commit

Permalink
FIX: Remove deprecated ast.Str
Browse files Browse the repository at this point in the history
  • Loading branch information
fumitoh committed Dec 8, 2024
1 parent fab8ac3 commit dcd8990
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
22 changes: 17 additions & 5 deletions modelx/core/formula.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017-2024 Fumito Hamamura <[email protected]>

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.
Expand Down Expand Up @@ -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:]
Expand All @@ -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:]
Expand Down
8 changes: 6 additions & 2 deletions modelx/serialize/serializer_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions modelx/serialize/serializer_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit dcd8990

Please sign in to comment.