Skip to content

Commit

Permalink
[BE][Easy] Remove usage of deprecated ast.Str, ast.Ellipsis and `…
Browse files Browse the repository at this point in the history
…ast.NameConstant` (pytorch#125912)

`ast.Str`, `ast.Ellipsis`, and `ast.NameConstant` are deprecated in Python 3.8 and will be removed in Python 3.14. Replace them with `ast.Constant`.

Ref: https://docs.python.org/3/library/ast.html#node-classes

> **Changed in version 3.8:** Class [ast.Constant](https://docs.python.org/3/library/ast.html#ast.Constant) is now used for all constants.
>
> **Deprecated since version 3.8:** Old classes ast.Num, ast.Str, ast.Bytes, ast.NameConstant and ast.Ellipsis are still available, but they will be removed in future Python releases. In the meantime, instantiating them will return an instance of a different class.

CI log: https://github.com/metaopt/torchopt/actions/runs/9031146681/job/24816802280?pr=216#step:11:6706
Pull Request resolved: pytorch#125912
Approved by: https://github.com/soulitzer
  • Loading branch information
XuehaiPan authored and pytorchmergebot committed May 10, 2024
1 parent 53a64e4 commit 4996a3f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions torch/_jit_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def get_annotation_str(annotation):
return f"{get_annotation_str(annotation.value)}[{get_annotation_str(subscript_slice)}]"
elif isinstance(annotation, ast.Tuple):
return ",".join([get_annotation_str(elt) for elt in annotation.elts])
elif isinstance(annotation, (ast.Constant, ast.NameConstant)):
elif isinstance(annotation, ast.Constant):
return f"{annotation.value}"

# If an AST node is not handled here, it's probably handled in ScriptTypeParser.
Expand Down Expand Up @@ -879,7 +879,11 @@ def is_pass(x):
return isinstance(x, ast.Pass)

def is_ellipsis(x):
return isinstance(x, ast.Expr) and isinstance(x.value, ast.Ellipsis)
return (
isinstance(x, ast.Expr)
and isinstance(x.value, ast.Constant)
and x.value.value is Ellipsis
)

if len(body) != 1 or not (is_pass(body[0]) or is_ellipsis(body[0])):
msg = (
Expand Down
6 changes: 3 additions & 3 deletions torch/jit/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ def build_ExtSlice(ctx, base, extslice):
sub_exprs.append(build_Index(ctx, base, expr))
elif sub_type is ast.Slice:
sub_exprs.append(build_SliceExpr(ctx, base, expr))
elif sub_type is ast.Ellipsis:
elif sub_type is ast.Constant and expr.value is Ellipsis:
sub_exprs.append(Dots(base.range()))
else:
raise NotSupportedError(
Expand Down Expand Up @@ -1206,8 +1206,8 @@ def build_JoinedStr(ctx, expr):
raise NotSupportedError(r, "Don't support formatting in JoinedStr")
s += "{}"
args.append(build_expr(ctx, value.value))
elif isinstance(value, ast.Str):
s += value.s
elif isinstance(value, ast.Constant):
s += value.value
else:
raise NotSupportedError(r, "Unsupported value in JoinedStr")

Expand Down

0 comments on commit 4996a3f

Please sign in to comment.