Skip to content

Commit

Permalink
Fix outstanding Mypy errors
Browse files Browse the repository at this point in the history
- Fix wrong import path for napalm.base.exceptions.CommitError
- Re-write convert method to properly utilize typing
  • Loading branch information
Kircheneer committed Feb 12, 2022
1 parent 6c3a0a9 commit 31434ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
26 changes: 23 additions & 3 deletions napalm/base/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,36 @@ def find_txt(
return str(value)


def convert(to: Callable[[T], R], who: Optional[T], default: R = "") -> R:
def convert(to: Callable[[T], R], who: Optional[T], default: Optional[R] = None) -> R:
"""
Converts data to a specific datatype.
In case of error, will return a default value.
:param to: datatype to be casted to.
:param who: value to cast.
:param default: value to return in case of error.
:return: a str value.
:param default: default value to return in case of an error with the conversion function.
:return: the result of the cast or a default value.
"""
if default is None:
# Mypy is currently unable to resolve the Optional[R] correctly, therefore the following
# assignments to 'default' need a 'type: ignore' statement.
# Ref: https://github.com/python/mypy/issues/8708
if to in [str, ip, mac]:
default = "" # type: ignore
elif to in [float, int]:
default = 0 # type: ignore
elif to == bool:
default = False # type: ignore
elif to == list:
default = [] # type: ignore
else:
raise ValueError(
f"Can't convert with callable {to} - no default is defined for this type."
)

# This is safe because the None-case if handled above
assert default is not None

if who is None:
return default
try:
Expand Down
4 changes: 3 additions & 1 deletion napalm/base/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def commit_config(self, message: str = "", revert_in: Optional[int] = None) -> N
self._raise_if_closed()
if revert_in is not None:
if self.has_pending_commit():
raise napalm.CommitError("Pending commit confirm already in process!")
raise napalm.base.exceptions.CommitError(
"Pending commit confirm already in process!"
)
else:
self._pending_commits = True
self.merge = None
Expand Down
6 changes: 3 additions & 3 deletions napalm/nxos/nxos.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ def _compute_timestamp(stupid_cisco_output: str) -> float:
for key in things_keys:
if key in part:
things[key]["count"] = napalm.base.helpers.convert(
int, part.replace(key, ""), 0
int, part.replace(key, "")
)

delta = sum([det.get("count", 0) * det["weight"] for det in things.values()])
Expand Down Expand Up @@ -1025,7 +1025,7 @@ def get_interfaces(self) -> Dict[str, models.InterfaceDict]:
"speed": interface_speed,
"mtu": interface_mtu,
"mac_address": napalm.base.helpers.convert(
napalm.base.helpers.mac, mac_address, ""
napalm.base.helpers.mac, mac_address
),
}
return interfaces
Expand Down Expand Up @@ -1161,7 +1161,7 @@ def get_arp_table(self, vrf: str = "") -> List[models.ARPTableDict]:
{
"interface": interface,
"mac": napalm.base.helpers.convert(
napalm.base.helpers.mac, raw_mac, raw_mac
napalm.base.helpers.mac, raw_mac
),
"ip": napalm.base.helpers.ip(raw_ip),
"age": age_sec,
Expand Down

0 comments on commit 31434ed

Please sign in to comment.