# Buggy code
def copy_with(self, **kwargs: typing.Any) -> "URL":
    """
    Copy this URL, returning a new URL with some components altered.
    Accepts the same set of parameters as the components that are made
    available via properties on the `URL` class.

    For example:

    url = httpx.URL("https://www.example.com").copy_with(username="jo@gmail.com", password="a secret")
    assert url == "https://jo%40email.com:a%20secret@www.example.com"
    """
    allowed = {
        "scheme": str,
        "username": str,
        "password": str,
        "userinfo": bytes,
        "host": str,
        "port": int,
        "netloc": str,
        "path": str,
        "query": bytes,
        "raw_path": bytes,
        "fragment": str,
    }
    for key, value in kwargs.items():
        if key not in allowed:
            message = f"{key!r} is an invalid keyword argument for copy_with()"
            raise TypeError(message)
        if value is not None and not isinstance(value, allowed[key]):
            expected = allowed[key].__name__
            seen = type(value).__name__
            message = f"Argument {key!r} must be {expected} but got {seen}"
            raise TypeError(message)

    # Replace username, password, userinfo, host, port, netloc with "authority" for rfc3986
    if "username" in kwargs or "password" in kwargs:
        # Consolidate username and password into userinfo.
        username = quote(kwargs.pop("username", self.username) or "")
        password = quote(kwargs.pop("password", self.password) or "")
        userinfo = f"{username}:{password}" if password else username
        kwargs["userinfo"] = userinfo.encode("ascii")

    if "host" in kwargs or "port" in kwargs:
        # Consolidate host and port into  netloc.
        host = kwargs.pop("host", self.host) or ""
        port = kwargs.pop("port", self.port)
        kwargs["netloc"] = f"{host}:{port}" if port is not None else host

    if "userinfo" in kwargs or "netloc" in kwargs:
        # Consolidate userinfo and netloc into authority.
        userinfo = (kwargs.pop("userinfo", self.userinfo) or b"").decode("ascii")
        netloc = kwargs.pop("netloc", self.netloc) or ""
        authority = f"{userinfo}@{netloc}" if userinfo else netloc
        kwargs["authority"] = authority

    if "raw_path" in kwargs:
        raw_path = kwargs.pop("raw_path") or b""
        path, has_query, query = raw_path.decode("ascii").partition("?")
        kwargs["path"] = path
        kwargs["query"] = query if has_query else None

    else:
        # Ensure path=<url quoted str> for rfc3986
        if kwargs.get("path") is not None:
            kwargs["path"] = quote(kwargs["path"])

        # Ensure query=<str> for rfc3986
        if kwargs.get("query") is not None:
            kwargs["query"] = kwargs["query"].decode("ascii")

    return URL(self._uri_reference.copy_with(**kwargs).unsplit())


# Traceback (httpcore.ConnectError)
<Response [200]>
<Response [200]>
<Response [200 OK]>
Traceback (most recent call last):
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_exceptions.py", line 339, in map_exceptions
yield
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_client.py", line 858, in _send_single_request
ext={"timeout": timeout.as_dict()},
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpcore/_sync/connection_pool.py", line 195, in request
method, url, headers=headers, stream=stream, ext=ext
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpcore/_sync/connection.py", line 87, in request
self.socket = self._open_socket(timeout)
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpcore/_sync/connection.py", line 113, in _open_socket
local_address=self.local_address,
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpcore/_backends/sync.py", line 144, in open_tcp_stream
return SyncSocketStream(sock=sock)
File "/home/user/.local/share/pyenv/versions/3.7.6/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
raise to_exc(exc) from None
httpcore.ConnectError: [Errno -2] Name or service not known

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "kek.py", line 11, in <module>
print(httpx.get(url6))
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_api.py", line 193, in get
trust_env=trust_env,
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_api.py", line 104, in request
allow_redirects=allow_redirects,
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_client.py", line 722, in request
request, auth=auth, allow_redirects=allow_redirects, timeout=timeout
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_client.py", line 758, in send
history=[],
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_client.py", line 795, in _send_handling_auth
history=history,
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_client.py", line 823, in _send_handling_redirects
response = self._send_single_request(request, timeout)
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_client.py", line 858, in _send_single_request
ext={"timeout": timeout.as_dict()},
File "/home/user/.local/share/pyenv/versions/3.7.6/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/home/user/repos/lb-test/.venv/lib/python3.7/site-packages/httpx/_exceptions.py", line 356, in map_exceptions
raise mapped_exc(message, **kwargs) from exc  # type: ignore
httpx.ConnectError: [Errno -2] Name or service not known

# Fixed code
def copy_with(self, **kwargs: typing.Any) -> "URL":
    """
    Copy this URL, returning a new URL with some components altered.
    Accepts the same set of parameters as the components that are made
    available via properties on the `URL` class.

    For example:

    url = httpx.URL("https://www.example.com").copy_with(username="jo@gmail.com", password="a secret")
    assert url == "https://jo%40email.com:a%20secret@www.example.com"
    """
    allowed = {
        "scheme": str,
        "username": str,
        "password": str,
        "userinfo": bytes,
        "host": str,
        "port": int,
        "netloc": str,
        "path": str,
        "query": bytes,
        "raw_path": bytes,
        "fragment": str,
    }
    for key, value in kwargs.items():
        if key not in allowed:
            message = f"{key!r} is an invalid keyword argument for copy_with()"
            raise TypeError(message)
        if value is not None and not isinstance(value, allowed[key]):
            expected = allowed[key].__name__
            seen = type(value).__name__
            message = f"Argument {key!r} must be {expected} but got {seen}"
            raise TypeError(message)

    # Replace username, password, userinfo, host, port, netloc with "authority" for rfc3986
    if "username" in kwargs or "password" in kwargs:
        # Consolidate username and password into userinfo.
        username = quote(kwargs.pop("username", self.username) or "")
        password = quote(kwargs.pop("password", self.password) or "")
        userinfo = f"{username}:{password}" if password else username
        kwargs["userinfo"] = userinfo.encode("ascii")

    if "host" in kwargs or "port" in kwargs:
        # Consolidate host and port into  netloc.
        host = kwargs.pop("host", self.host) or ""
        port = kwargs.pop("port", self.port)

        if host and ":" in host and host[0] != "[":
            # it's an IPv6 address, so it should be hidden under bracket
            host = f"[{host}]"

        kwargs["netloc"] = f"{host}:{port}" if port is not None else host

    if "userinfo" in kwargs or "netloc" in kwargs:
        # Consolidate userinfo and netloc into authority.
        userinfo = (kwargs.pop("userinfo", self.userinfo) or b"").decode("ascii")
        netloc = kwargs.pop("netloc", self.netloc) or ""
        authority = f"{userinfo}@{netloc}" if userinfo else netloc
        kwargs["authority"] = authority

    if "raw_path" in kwargs:
        raw_path = kwargs.pop("raw_path") or b""
        path, has_query, query = raw_path.decode("ascii").partition("?")
        kwargs["path"] = path
        kwargs["query"] = query if has_query else None

    else:
        # Ensure path=<url quoted str> for rfc3986
        if kwargs.get("path") is not None:
            kwargs["path"] = quote(kwargs["path"])

        # Ensure query=<str> for rfc3986
        if kwargs.get("query") is not None:
            kwargs["query"] = kwargs["query"].decode("ascii")

    return URL(self._uri_reference.copy_with(**kwargs).unsplit())