from typing import Optional
from typing import Union
import ipaddress


def validate_ipaddress(
    ipaddr: str,
) -> Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]:
    """
    Given the IP address string, either return the enrobed ipaddress
    instance if valid, or None if not.

    Parameters
    ----------
    ipaddr: str

    Returns
    -------
    ipaddress.IPv4Address
        When `ipaddr` is a valid IPv4 address
    ipaddress.IPv6Address
        When `ipaddr` is a valid IPv6 address
    False
        Otherwise
    """
    try:
        return ipaddress.ip_address(ipaddr)

    except ValueError:
        return None