Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge
  • Loading branch information
sobolevn committed Jul 3, 2023
commit 5c39f2dcbcf14f5695798c0949028f79d04684a2
113 changes: 18 additions & 95 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,52 +124,19 @@

import ast as ast3

assert (
"kind" in ast3.Constant._fields
), f"This 3.8.0 alpha ({sys.version.split()[0]}) is too old; 3.8.0a3 required"
# TODO: Index, ExtSlice are deprecated in 3.9.
from ast import (
AST,
Attribute,
Call,
Expression as ast3_Expression,
FunctionType,
Index,
Name,
Starred,
UnaryOp,
USub,
)


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 (
AST,
Attribute,
Bytes,
Call,
Ellipsis as ast3_Ellipsis,
Expression as ast3_Expression,
FunctionType,
Index,
Name,
NameConstant,
Num,
Starred,
Str,
UnaryOp,
USub,
)
# TODO: Index, ExtSlice are deprecated in 3.9.
from ast import AST, Attribute, Call, FunctionType, Index, Name, Starred, UnaryOp, USub

def ast3_parse(
source: str | bytes, filename: str, mode: str, feature_version: int = PY_MINOR_VERSION
) -> AST:
return ast3.parse(
source,
filename,
mode,
type_comments=True, # This works the magic
feature_version=feature_version,
)


NamedExpr = ast3.NamedExpr
Expand Down Expand Up @@ -929,9 +896,8 @@ def do_func_def(
# for ellipsis arg
if (
len(func_type_ast.argtypes) == 1
and isinstance(func_type_ast.argtypes[0], ast3_Ellipsis)
# TODO: remove when Python3.7 support is dropped
and getattr(func_type_ast.argtypes[0], "value", ...) is ...
and isinstance(func_type_ast.argtypes[0], Constant)
and func_type_ast.argtypes[0].value is Ellipsis
):
if n.returns:
# PEP 484 disallows both type annotations and type comments
Expand Down Expand Up @@ -1557,28 +1523,6 @@ def visit_Constant(self, n: Constant) -> Any:
raise RuntimeError("Constant not implemented for " + str(type(val)))
return self.set_line(e, n)

# Num(object n) -- a number as a PyObject.
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
# this by throwing away the type.
val: object = n.n
if isinstance(val, int):
e: IntExpr | FloatExpr | ComplexExpr = IntExpr(val)
elif isinstance(val, float):
e = FloatExpr(val)
elif isinstance(val, complex):
e = ComplexExpr(val)
else:
raise RuntimeError("num not implemented for " + str(type(val)))
return self.set_line(e, n)

# Str(string s)
def visit_Str(self, n: Str) -> StrExpr:
e = StrExpr(n.s)
return self.set_line(e, n)

# JoinedStr(expr* values)
def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
# Each of n.values is a str or FormattedValue; we just concatenate
Expand Down Expand Up @@ -1614,21 +1558,6 @@ def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
)
return self.set_line(result_expression, n)

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

# NameConstant(singleton value)
def visit_NameConstant(self, n: NameConstant) -> NameExpr:
e = NameExpr(str(n.value))
return self.set_line(e, n)

# Ellipsis
def visit_Ellipsis(self, n: ast3_Ellipsis) -> EllipsisExpr:
e = EllipsisExpr()
return self.set_line(e, n)

# Attribute(expr value, identifier attr, expr_context ctx)
def visit_Attribute(self, n: Attribute) -> MemberExpr | SuperExpr:
value = n.value
Expand Down Expand Up @@ -1915,14 +1844,9 @@ def translate_argument_list(self, l: Sequence[ast3.expr]) -> TypeList:
return TypeList([self.visit(e) for e in l], line=self.line)

def _extract_argument_name(self, n: ast3.expr) -> str | None:
if isinstance(n, Str):
# TODO: remove this when Python3.7 support is dropped
if sys.version_info >= (3, 8):
if isinstance(n.value, str):
return n.value.strip()
else:
return n.s.strip()
if isinstance(n, NameConstant) and str(n.value) == "None":
if isinstance(n, Constant) and isinstance(n.value, str):
return n.value.strip()
elif isinstance(n, Constant) and n.value is None:
return None
self.fail(
message_registry.ARG_NAME_EXPECTED_STRING_LITERAL.format(type(n).__name__),
Expand All @@ -1948,7 +1872,6 @@ def visit_BinOp(self, n: ast3.BinOp) -> Type:
uses_pep604_syntax=True,
)

# Only for 3.8 and newer
def visit_Constant(self, n: Constant) -> Type:
val = n.value
if val is None:
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.