Skip to content

Commit

Permalink
Remove unnecessary else after return statement (Fix: RET505)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenstad committed Jun 28, 2024
1 parent 7a93f23 commit 63185e2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
27 changes: 13 additions & 14 deletions nornir/core/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,27 @@ def _verify_rules(data: Any, rule: List[str], value: Any) -> bool:
if hasattr(data, operator):
return getattr(data, operator)(value) is True

elif hasattr(data, rule[0]):
if hasattr(data, rule[0]):
if callable(getattr(data, rule[0])):
return bool(getattr(data, rule[0])(value))
return bool(getattr(data, rule[0]) == value)

else:
return bool(getattr(data, rule[0]) == value)

elif rule == ["in"]:
if rule == ["in"]:
return bool(data in value)
elif rule == ["any"]:

if rule == ["any"]:
if isinstance(data, list):
return any([x in data for x in value])
else:
return any([x == data for x in value])
elif rule == ["all"]:
return any([x == data for x in value])

if rule == ["all"]:
if isinstance(data, list):
return all([x in data for x in value])
else:
# it doesn't make sense to check a single value meets more than one case
return False
else:
return bool(data.get(rule[0]) == value)

# it doesn't make sense to check a single value meets more than one case
return False

return bool(data.get(rule[0]) == value)

else:
raise Exception("I don't know how I got here:\n{}\n{}\n{}".format(data, rule, value))
Expand Down
11 changes: 5 additions & 6 deletions nornir/core/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class ParentGroups(List["Group"]):
def __contains__(self, value: object) -> bool:
if isinstance(value, str):
return any([value == g.name for g in self])
else:
return any([value == g for g in self])

return any([value == g for g in self])

def add(self, group: "Group") -> None:
"""
Expand Down Expand Up @@ -318,8 +318,7 @@ def has_parent_group(self, group: Union[str, "Group"]) -> bool:
if isinstance(group, str):
return self._has_parent_group_by_name(group)

else:
return self._has_parent_group_by_object(group)
return self._has_parent_group_by_object(group)

def _has_parent_group_by_name(self, group: str) -> bool:
for g in self.groups:
Expand Down Expand Up @@ -362,8 +361,8 @@ def __getattribute__(self, name: str) -> Any:
return r

return object.__getattribute__(self.defaults, name)
else:
return v

return v

def __bool__(self) -> bool:
return bool(self.name)
Expand Down
3 changes: 1 addition & 2 deletions nornir/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ def __str__(self) -> str:
if self.exception:
return str(self.exception)

else:
return str(self.result)
return str(self.result)


class MultiResult(List[Result]):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ ignore = [
"PTH120", # `os.path.dirname()` should be replaced by `Path.parent`
"PTH123", # `open()` should be replaced by `Path.open()`
"PYI036", # The first argument in `__exit__` should be annotated with `object` or `type[BaseException] | None`
"RET505", # Unnecessary `else` after `return` statement
"RET504", # Unnecessary assignment before `return` statement
"RSE102", # Unnecessary parentheses on raised exception
"RUF001", # String contains ambiguous `–` (EN DASH). Did you mean `-` (HYPHEN-MINUS)?
Expand Down

0 comments on commit 63185e2

Please sign in to comment.