import ipaddress


def format_addr(addr):
    """Validate and format IP address.

    :param addr: IPv6 or IPv4 address
    :type addr: str
    :returns: Address string, optionally encapsulated in brackets([])
    :rtype: str
    :raises: ValueError
    """
    ipaddr = ipaddress.ip_address(addr)
    if isinstance(ipaddr, ipaddress.IPv6Address):
        fmt = '[{}]'
    else:
        fmt = '{}'
    return fmt.format(ipaddr)