# Buggy code
def bind_socket(self):
    family, sockettype, proto, canonname, sockaddr = socket.getaddrinfo(
        self.host, self.port, type=socket.SOCK_STREAM
    )[0]
    sock = socket.socket(family=family, type=sockettype)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        sock.bind((self.host, self.port))
    except OSError as exc:
        logger.error(exc)
        sys.exit(1)
    sock.set_inheritable(True)

    if family == socket.AddressFamily.AF_INET6:
        message, color_message = _get_server_start_message(_IPKind.IPv6)
    else:
        message, color_message = _get_server_start_message(_IPKind.IPv4)
    protocol_name = "https" if self.is_ssl else "http"
    logger.info(
        message,
        protocol_name,
        self.host,
        self.port,
        extra={"color_message": color_message},
    )
    return sock


# Traceback (ValueError)
.py thon/debugpy/launcher 61232 -- /Users/paulafernandez/Sandbox/test/test/start
INFO:     Started server process [43171]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/paulafernandez/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
cli.main()
File "/Users/paulafernandez/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/Users/paulafernandez/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 267, in run_file
runpy.run_path(options.target, run_name=compat.force_str("__main__"))
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/paulafernandez/Sandbox/test/test/start.py", line 18, in <module>
uvicorn.run(
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 391, in run
server.run()
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 419, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 436, in serve
await self.startup(sockets=sockets)
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 526, in startup
if isinstance(ip_address(config.host), IPv6Address):
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ipaddress.py", line 53, in ip_address
raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
ValueError: 'localhost' does not appear to be an IPv4 or IPv6 address

# Fixed code
def bind_socket(self):
    family, sockettype, proto, canonname, sockaddr = socket.getaddrinfo(
        self.host, self.port, type=socket.SOCK_STREAM
    )[0]
    sock = socket.socket(family=family, type=sockettype)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        sock.bind((self.host, self.port))
    except OSError as exc:
        logger.error(exc)
        sys.exit(1)
    sock.set_inheritable(True)

    if family == socket.AddressFamily.AF_INET6:
        message, color_message = _get_server_start_message(is_ipv6_message=True)
    else:
        message, color_message = _get_server_start_message()
    protocol_name = "https" if self.is_ssl else "http"
    logger.info(
        message,
        protocol_name,
        self.host,
        self.port,
        extra={"color_message": color_message},
    )
    return sock