Skip to content

Commit 1adacd0

Browse files
authored
Eliminate some conditional method defs in fastparse (#5499)
mypyc doesn't support this. One isn't necessary anymore, and the other can be worked around in a different way.
1 parent a314c0a commit 1adacd0

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

mypy/fastparse.py

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@
7272
TYPE_COMMENT_AST_ERROR = 'invalid type comment or annotation'
7373

7474

75+
# Older versions of typing don't allow using overload outside stubs,
76+
# so provide a dummy.
77+
if not MYPY and sys.version_info < (3, 6):
78+
def overload(x: Any) -> Any: # noqa
79+
return x
80+
81+
7582
def parse(source: Union[str, bytes],
7683
fnam: str,
7784
module: Optional[str],
@@ -887,42 +894,40 @@ def visit_Str(self, n: ast3.Str) -> Union[UnicodeExpr, StrExpr]:
887894
# unicode.
888895
return StrExpr(n.s)
889896

890-
# Only available with typed_ast >= 0.6.2
891-
if hasattr(ast3, 'JoinedStr'):
892-
# JoinedStr(expr* values)
893-
@with_line
894-
def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
895-
# Each of n.values is a str or FormattedValue; we just concatenate
896-
# them all using ''.join.
897-
empty_string = StrExpr('')
898-
empty_string.set_line(n.lineno, n.col_offset)
899-
strs_to_join = ListExpr(self.translate_expr_list(n.values))
900-
strs_to_join.set_line(empty_string)
901-
join_method = MemberExpr(empty_string, 'join')
902-
join_method.set_line(empty_string)
903-
result_expression = CallExpr(join_method,
904-
[strs_to_join],
905-
[ARG_POS],
906-
[None])
907-
return result_expression
908-
909-
# FormattedValue(expr value)
910-
@with_line
911-
def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
912-
# A FormattedValue is a component of a JoinedStr, or it can exist
913-
# on its own. We translate them to individual '{}'.format(value)
914-
# calls -- we don't bother with the conversion/format_spec fields.
915-
exp = self.visit(n.value)
916-
exp.set_line(n.lineno, n.col_offset)
917-
format_string = StrExpr('{}')
918-
format_string.set_line(n.lineno, n.col_offset)
919-
format_method = MemberExpr(format_string, 'format')
920-
format_method.set_line(format_string)
921-
result_expression = CallExpr(format_method,
922-
[exp],
923-
[ARG_POS],
924-
[None])
925-
return result_expression
897+
# JoinedStr(expr* values)
898+
@with_line
899+
def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
900+
# Each of n.values is a str or FormattedValue; we just concatenate
901+
# them all using ''.join.
902+
empty_string = StrExpr('')
903+
empty_string.set_line(n.lineno, n.col_offset)
904+
strs_to_join = ListExpr(self.translate_expr_list(n.values))
905+
strs_to_join.set_line(empty_string)
906+
join_method = MemberExpr(empty_string, 'join')
907+
join_method.set_line(empty_string)
908+
result_expression = CallExpr(join_method,
909+
[strs_to_join],
910+
[ARG_POS],
911+
[None])
912+
return result_expression
913+
914+
# FormattedValue(expr value)
915+
@with_line
916+
def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
917+
# A FormattedValue is a component of a JoinedStr, or it can exist
918+
# on its own. We translate them to individual '{}'.format(value)
919+
# calls -- we don't bother with the conversion/format_spec fields.
920+
exp = self.visit(n.value)
921+
exp.set_line(n.lineno, n.col_offset)
922+
format_string = StrExpr('{}')
923+
format_string.set_line(n.lineno, n.col_offset)
924+
format_method = MemberExpr(format_string, 'format')
925+
format_method.set_line(format_string)
926+
result_expression = CallExpr(format_method,
927+
[exp],
928+
[ARG_POS],
929+
[None])
930+
return result_expression
926931

927932
# Bytes(bytes s)
928933
@with_line
@@ -1003,7 +1008,13 @@ def __init__(self, errors: Optional[Errors], line: int = -1) -> None:
10031008
self.line = line
10041009
self.node_stack = [] # type: List[ast3.AST]
10051010

1006-
def _visit_implementation(self, node: Optional[ast3.AST]) -> Optional[Type]:
1011+
@overload
1012+
def visit(self, node: ast3.expr) -> Type: ...
1013+
1014+
@overload # noqa
1015+
def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: ...
1016+
1017+
def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: # noqa
10071018
"""Modified visit -- keep track of the stack of nodes"""
10081019
if node is None:
10091020
return None
@@ -1013,19 +1024,6 @@ def _visit_implementation(self, node: Optional[ast3.AST]) -> Optional[Type]:
10131024
finally:
10141025
self.node_stack.pop()
10151026

1016-
if sys.version_info >= (3, 6):
1017-
@overload
1018-
def visit(self, node: ast3.expr) -> Type: ...
1019-
1020-
@overload # noqa
1021-
def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: ...
1022-
1023-
def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: # noqa
1024-
return self._visit_implementation(node)
1025-
else:
1026-
def visit(self, node: Optional[ast3.AST]) -> Any:
1027-
return self._visit_implementation(node)
1028-
10291027
def parent(self) -> Optional[ast3.AST]:
10301028
"""Return the AST node above the one we are processing"""
10311029
if len(self.node_stack) < 2:

0 commit comments

Comments
 (0)