Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ast warnings for Python3.12 #15558

Merged
merged 9 commits into from
Jul 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,16 @@
assert (
"kind" in ast3.Constant._fields
), f"This 3.8.0 alpha ({sys.version.split()[0]}) is too old; 3.8.0a3 required"
# TODO: Num, Str, Bytes, NameConstant, Ellipsis are deprecated in 3.8.
# TODO: Index, ExtSlice are deprecated in 3.9.
from ast import (
AST,
Attribute,
Bytes,
Call,
Ellipsis as ast3_Ellipsis,
Expression as ast3_Expression,
FunctionType,
Index,
Name,
NameConstant,
Num,
Starred,
Str,
UnaryOp,
USub,
)
Expand All @@ -163,6 +157,13 @@ def ast3_parse(

NamedExpr = ast3.NamedExpr
Constant = ast3.Constant
# TODO: We can delete these aliases after Python3.7 support is dropped.
# Right now, they are only needed to have the same global names as 3.7
Num = ast3.Constant
Str = ast3.Constant
Bytes = ast3.Constant
NameConstant = ast3.Constant
ast3_Ellipsis = ast3.Constant
else:
from typed_ast import ast3
from typed_ast.ast3 import (
Expand Down Expand Up @@ -1580,7 +1581,7 @@ def visit_Constant(self, n: Constant) -> Any:
if val is None:
e = NameExpr("None")
elif isinstance(val, str):
e = StrExpr(n.s)
e = StrExpr(val)
elif isinstance(val, bytes):
e = BytesExpr(bytes_to_human_readable_repr(n.s))
elif isinstance(val, bool): # Must check before int!
Expand All @@ -1598,7 +1599,7 @@ def visit_Constant(self, n: Constant) -> Any:
return self.set_line(e, n)

# Num(object n) -- a number as a PyObject.
def visit_Num(self, n: ast3.Num) -> IntExpr | FloatExpr | ComplexExpr:
def visit_Num(self, n: Num) -> IntExpr | FloatExpr | ComplexExpr:
# The n field has the type complex, but complex isn't *really*
# a parent of int and float, and this causes isinstance below
# to think that the complex branch is always picked. Avoid
Expand Down Expand Up @@ -1655,7 +1656,7 @@ def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
return self.set_line(result_expression, n)

# Bytes(bytes s)
def visit_Bytes(self, n: ast3.Bytes) -> BytesExpr | StrExpr:
def visit_Bytes(self, n: Bytes) -> BytesExpr | StrExpr:
e = BytesExpr(bytes_to_human_readable_repr(n.s))
return self.set_line(e, n)

Expand Down