Skip to content

Eliminate some conditional method defs in fastparse #5499

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

Merged
merged 6 commits into from
Aug 17, 2018
Merged
Changes from all 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
98 changes: 48 additions & 50 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
TYPE_COMMENT_AST_ERROR = 'invalid type comment or annotation'


# Older versions of typing don't allow using overload outside stubs,
# so provide a dummy.
if not MYPY and sys.version_info < (3, 6):
def overload(x: Any) -> Any: # noqa
return x


def parse(source: Union[str, bytes],
fnam: str,
module: Optional[str],
Expand Down Expand Up @@ -887,42 +894,40 @@ def visit_Str(self, n: ast3.Str) -> Union[UnicodeExpr, StrExpr]:
# unicode.
return StrExpr(n.s)

# Only available with typed_ast >= 0.6.2
if hasattr(ast3, 'JoinedStr'):
# JoinedStr(expr* values)
@with_line
def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
# Each of n.values is a str or FormattedValue; we just concatenate
# them all using ''.join.
empty_string = StrExpr('')
empty_string.set_line(n.lineno, n.col_offset)
strs_to_join = ListExpr(self.translate_expr_list(n.values))
strs_to_join.set_line(empty_string)
join_method = MemberExpr(empty_string, 'join')
join_method.set_line(empty_string)
result_expression = CallExpr(join_method,
[strs_to_join],
[ARG_POS],
[None])
return result_expression

# FormattedValue(expr value)
@with_line
def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
# A FormattedValue is a component of a JoinedStr, or it can exist
# on its own. We translate them to individual '{}'.format(value)
# calls -- we don't bother with the conversion/format_spec fields.
exp = self.visit(n.value)
exp.set_line(n.lineno, n.col_offset)
format_string = StrExpr('{}')
format_string.set_line(n.lineno, n.col_offset)
format_method = MemberExpr(format_string, 'format')
format_method.set_line(format_string)
result_expression = CallExpr(format_method,
[exp],
[ARG_POS],
[None])
return result_expression
# JoinedStr(expr* values)
@with_line
def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
# Each of n.values is a str or FormattedValue; we just concatenate
# them all using ''.join.
empty_string = StrExpr('')
empty_string.set_line(n.lineno, n.col_offset)
strs_to_join = ListExpr(self.translate_expr_list(n.values))
strs_to_join.set_line(empty_string)
join_method = MemberExpr(empty_string, 'join')
join_method.set_line(empty_string)
result_expression = CallExpr(join_method,
[strs_to_join],
[ARG_POS],
[None])
return result_expression

# FormattedValue(expr value)
@with_line
def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
# A FormattedValue is a component of a JoinedStr, or it can exist
# on its own. We translate them to individual '{}'.format(value)
# calls -- we don't bother with the conversion/format_spec fields.
exp = self.visit(n.value)
exp.set_line(n.lineno, n.col_offset)
format_string = StrExpr('{}')
format_string.set_line(n.lineno, n.col_offset)
format_method = MemberExpr(format_string, 'format')
format_method.set_line(format_string)
result_expression = CallExpr(format_method,
[exp],
[ARG_POS],
[None])
return result_expression

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

def _visit_implementation(self, node: Optional[ast3.AST]) -> Optional[Type]:
@overload
def visit(self, node: ast3.expr) -> Type: ...

@overload # noqa
def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: ...

def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: # noqa
"""Modified visit -- keep track of the stack of nodes"""
if node is None:
return None
Expand All @@ -1013,19 +1024,6 @@ def _visit_implementation(self, node: Optional[ast3.AST]) -> Optional[Type]:
finally:
self.node_stack.pop()

if sys.version_info >= (3, 6):
@overload
def visit(self, node: ast3.expr) -> Type: ...

@overload # noqa
def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: ...

def visit(self, node: Optional[ast3.AST]) -> Optional[Type]: # noqa
return self._visit_implementation(node)
else:
def visit(self, node: Optional[ast3.AST]) -> Any:
return self._visit_implementation(node)

def parent(self) -> Optional[ast3.AST]:
"""Return the AST node above the one we are processing"""
if len(self.node_stack) < 2:
Expand Down