From 329a3100e9d5c9c5d64009579fb2fce462e09204 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 30 Jun 2023 21:14:07 +0300 Subject: [PATCH 1/8] Fix `ast` warnings for Python3.12 --- mypy/fastparse.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index fe59ff48bdfc..0f0e6bb6adf0 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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, ) @@ -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 = Any + Str = Any + Bytes = Any + NameConstant = Any + ast3_Ellipsis = Any else: from typed_ast import ast3 from typed_ast.ast3 import ( @@ -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! @@ -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 @@ -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) From 5a70bde8ca40162b3a8fc643ad3677d7120e5522 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 30 Jun 2023 21:20:53 +0300 Subject: [PATCH 2/8] Fix CI --- mypy/fastparse.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 0f0e6bb6adf0..586591920efc 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -159,11 +159,11 @@ def ast3_parse( 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 = Any - Str = Any - Bytes = Any - NameConstant = Any - ast3_Ellipsis = Any + 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 ( From 842085247e46a49e78ac4235d88951212448bdf9 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 30 Jun 2023 22:07:46 +0300 Subject: [PATCH 3/8] Fix CI --- mypy/fastparse.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 586591920efc..e1fac1623c3a 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1583,7 +1583,7 @@ def visit_Constant(self, n: Constant) -> Any: elif isinstance(val, str): e = StrExpr(val) elif isinstance(val, bytes): - e = BytesExpr(bytes_to_human_readable_repr(n.s)) + e = BytesExpr(bytes_to_human_readable_repr(val)) elif isinstance(val, bool): # Must check before int! e = NameExpr(str(val)) elif isinstance(val, int): @@ -1957,7 +1957,11 @@ def translate_argument_list(self, l: Sequence[ast3.expr]) -> TypeList: def _extract_argument_name(self, n: ast3.expr) -> str | None: if isinstance(n, Str): - return n.s.strip() + # TODO: remove this when Python3.7 support is dropped + if sys.version_info >= (3, 8): + return n.value.strip() + else: + return n.s.strip() elif isinstance(n, NameConstant) and str(n.value) == "None": return None self.fail( @@ -1998,7 +2002,7 @@ def visit_Constant(self, n: Constant) -> Type: return UnboundType("None", line=self.line) if isinstance(val, str): # Parse forward reference. - return parse_type_string(n.s, "builtins.str", self.line, n.col_offset) + return parse_type_string(val, "builtins.str", self.line, n.col_offset) if val is Ellipsis: # '...' is valid in some types. return EllipsisType(line=self.line) From 0bb6e99bd3dbc9c7b140cc7276dc43d5b3be9418 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 30 Jun 2023 22:29:30 +0300 Subject: [PATCH 4/8] Fix CI --- mypy/fastparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index e1fac1623c3a..54c6b9a18ed8 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1958,7 +1958,7 @@ def translate_argument_list(self, l: Sequence[ast3.expr]) -> TypeList: 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 sys.version_info >= (3, 8) and isinstance(n.value, str): return n.value.strip() else: return n.s.strip() From fc11b9658f279bdccd47e2c05d8248d57efa10a2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Jul 2023 00:22:37 +0300 Subject: [PATCH 5/8] Fix CI --- mypy/fastparse.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 54c6b9a18ed8..a96f43ab53b2 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1958,8 +1958,9 @@ def translate_argument_list(self, l: Sequence[ast3.expr]) -> TypeList: 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) and isinstance(n.value, str): - return n.value.strip() + if sys.version_info >= (3, 8): + if isinstance(n.value, str): + return n.value.strip() else: return n.s.strip() elif isinstance(n, NameConstant) and str(n.value) == "None": From 73c729ad7260995a0e250c5792f917bb0d5f162e Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Jul 2023 00:49:41 +0300 Subject: [PATCH 6/8] Fix CI --- mypy/fastparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index a96f43ab53b2..b462f9defd52 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1963,7 +1963,7 @@ def _extract_argument_name(self, n: ast3.expr) -> str | None: return n.value.strip() else: return n.s.strip() - elif isinstance(n, NameConstant) and str(n.value) == "None": + if isinstance(n, NameConstant) and str(n.value) == "None": return None self.fail( message_registry.ARG_NAME_EXPECTED_STRING_LITERAL.format(type(n).__name__), From 85e6b61d97044d958336e0e0b5fcacfaa5fb5c9a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Jul 2023 01:24:28 +0300 Subject: [PATCH 7/8] Fix CI --- mypy/fastparse.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index b462f9defd52..bff0a49ca454 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -962,8 +962,11 @@ def do_func_def( func_type_ast = ast3_parse(n.type_comment, "", "func_type") assert isinstance(func_type_ast, FunctionType) # for ellipsis arg - if len(func_type_ast.argtypes) == 1 and isinstance( - func_type_ast.argtypes[0], ast3_Ellipsis + 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 ... ): if n.returns: # PEP 484 disallows both type annotations and type comments From 1e50969c876f08139ee788ddfc6f7037e82ae11c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jul 2023 16:08:20 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/fastparse.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index fc0d80303ad6..fa185be21f9e 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -127,6 +127,7 @@ # 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: