Skip to content

Commit

Permalink
make mypy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostofGoes committed Jul 18, 2024
1 parent c530b25 commit d97db6b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions getmac/getmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,9 @@ def get_by_method(

def get_mac_address(
interface: Union[str, bytes, None] = None,
ip: Union[str, bytes, IPv4Address, IPv4Interface, IPv6Address, IPv6Interface, None] = None,
ip: Union[
str, bytes, IPv4Address, IPv4Interface, IPv6Address, IPv6Interface, None
] = None,
ip6: Union[str, bytes, IPv6Address, IPv6Interface, None] = None,
hostname: Union[str, bytes, None] = None,
network_request: bool = True,
Expand Down Expand Up @@ -1468,17 +1470,18 @@ def get_mac_address(
start_time = timeit.default_timer()

# Convert bytes to str
if interface and isinstance(interface, bytes):
if isinstance(interface, bytes):
interface = interface.decode("utf-8")
if ip and isinstance(ip, bytes):
if isinstance(ip, bytes):
ip = ip.decode("utf-8")
if ip6 and isinstance(ip6, bytes):
if isinstance(ip6, bytes):
ip6 = ip6.decode("utf-8")
if hostname and isinstance(hostname, bytes):
if isinstance(hostname, bytes):
hostname = hostname.decode("utf-8")

# Handle ipaddress objects
if ip and not isinstance(ip, str):
# "is not None" check makes mypy happier
if ip is not None and not isinstance(ip, str):
if isinstance(ip, IPv4Address):
ip = str(ip)
elif isinstance(ip, IPv4Interface):
Expand All @@ -1503,7 +1506,9 @@ def get_mac_address(
"not a network. Try IPv6Address or IPv6Interface instead."
)
else:
raise ValueError(f"Unknown type for 'ip' argument: '{ip.__class__.__name__}'")
raise ValueError(
f"Unknown type for 'ip' argument: '{ip.__class__.__name__}'"
)

if (hostname and hostname == "localhost") or (ip and ip == "127.0.0.1"):
return "00:00:00:00:00:00"
Expand All @@ -1520,15 +1525,16 @@ def get_mac_address(
gvars.log.debug(traceback.format_exc())
return None

if ip6:
if ip6 is not None: # "is not None" check makes mypy happier
if not socket.has_ipv6:
# TODO: raise exception instead of returning None?
gvars.log.error(
"Cannot get the MAC address of a IPv6 host: "
"IPv6 is not supported on this system"
)
return None
elif isinstance(ip6, IPv6Address):

if isinstance(ip6, IPv6Address):
ip6 = str(ip6)
elif isinstance(ip6, IPv6Interface):
ip6 = str(ip6.ip)
Expand All @@ -1537,11 +1543,14 @@ def get_mac_address(
"IPv6Network objects are not supported. getmac needs a host address, "
"not a network. Try IPv6Address or IPv6Interface instead."
)
elif ":" not in ip6:
elif not isinstance(ip6, str):
raise ValueError(
f"Unknown type for 'ip6' argument: '{ip6.__class__.__name__}'"
)

if ":" not in ip6:
gvars.log.error(f"Invalid IPv6 address (no ':'): {ip6}")
return None
elif not isinstance(ip6, str):
raise ValueError(f"Unknown type for 'ip6' argument: '{ip6.__class__.__name__}'")

mac = None

Expand Down

0 comments on commit d97db6b

Please sign in to comment.