Skip to content

Commit

Permalink
refactor: refactor unnecessary else / elif when if block has a …
Browse files Browse the repository at this point in the history
…`return` statement (#20)

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
deepsource-autofix[bot] committed Sep 21, 2023
1 parent 1cec855 commit f392e24
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
3 changes: 1 addition & 2 deletions examples/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ def tokenize(json_string):
if new_index is None:
print("Parsing error at index", index)
return None
else:
index = new_index
index = new_index

return tokens

Expand Down
13 changes: 6 additions & 7 deletions src/interpreted/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def visit_Compare(self, node: Compare) -> Value:
if node.op == "in":
if isinstance(right, str):
return Value(left in right)
elif isinstance(right, (list, tuple, deque, dict)):
if isinstance(right, (list, tuple, deque, dict)):
return Value(
any(
isinstance(element, Value) and element.value == left
Expand All @@ -555,7 +555,7 @@ def visit_Compare(self, node: Compare) -> Value:
if node.op == "not in":
if isinstance(right, str):
return Value(left not in right)
elif isinstance(right, (list, tuple, deque)):
if isinstance(right, (list, tuple, deque)):
return Value(
not any(
isinstance(element, Value) and element.value == left
Expand Down Expand Up @@ -614,9 +614,9 @@ def visit_UnaryOp(self, node: UnaryOp) -> Value:

if node.op == "not":
return Value(not value.value)
elif node.op == "+":
if node.op == "+":
return value
elif node.op == "-":
if node.op == "-":
return Value(-value.value)

raise AssertionError(f"node.op must be '+', '-', or 'not', found {node!r}")
Expand Down Expand Up @@ -648,8 +648,7 @@ def visit_Subscript(self, node: Subscript) -> Object:
f"Slice indices should be integers or 'None', got {start.repr()}, {end.repr()}"
)
return Value(obj.value[start.value : end.value])
else:
raise NotImplementedError(node)
raise NotImplementedError(node)

key = self.visit(node.key)
if isinstance(obj, (List, Tuple, Deque)):
Expand Down Expand Up @@ -677,7 +676,7 @@ def visit_Attribute(self, node: Attribute) -> Object:
if attribute_name in obj.attributes:
return obj.attributes[attribute_name]

elif attribute_name in obj.methods:
if attribute_name in obj.methods:
return obj.methods[attribute_name]

raise InterpreterError(
Expand Down
10 changes: 3 additions & 7 deletions src/interpreted/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ def parse_statement(self) -> Statement:

if self.match_name("def", "if", "for", "while"):
return self.parse_multiline_statement()
else:
return self.parse_single_line_statement()
return self.parse_single_line_statement()

def parse_multiline_statement(self) -> FunctionDef | For | If | While:
keyword = self.current().string
Expand Down Expand Up @@ -520,16 +519,13 @@ def parse_literal(self) -> Expression:
return Constant(None)

return Name(token.string)

else:
raise ParseError(f"Unexpected keyword {token.string!r}", self.index - 1)
raise ParseError(f"Unexpected keyword {token.string!r}", self.index - 1)

if self.match_type(TokenType.NUMBER):
token = self.current()
if token.string.isdigit():
return Constant(int(token.string))
else:
return Constant(float(token.string))
return Constant(float(token.string))

if self.match_type(TokenType.STRING):
token = self.current()
Expand Down
2 changes: 1 addition & 1 deletion src/interpreted/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def scan_string(self, quote_char: str) -> None:
self.advance()
self.add_token(TokenType.STRING)
return
elif not is_multiline and char == quote_char:
if not is_multiline and char == quote_char:
self.add_token(TokenType.STRING)
return

Expand Down

0 comments on commit f392e24

Please sign in to comment.