Skip to content

AssertionError | assert not url.absolute raisedon a WSS URL #10027

@demoutreiii

Description

@demoutreiii

Describe the bug

Discord's Get Gateway endpoint returns a url field containing "wss://gateway.discord.gg". This WSS URL is used to establish connection with their gateway. Though this raises an exception:

File "...\aiohttp\client.py", line 467, in _build_url
  assert not url.absolute
         ^^^^^^^^^^^^^^^^^
AssertionError

This is also tested with applying yarl.URL(wss_url) ( maybe ), but same issue.

Side notes:

  1. I am testing in both Python 3.12 and 3.13, but would be more favorable to me if there is a fix already for 3.13
  2. I may provide other information should you ask relating to it

To Reproduce

from aiohttp import ClientSession
from asyncio import run
from typing import *

class DiscordWebSocket:
  session = lambda: ClientSession(base_url = "https://discord.com/api/v10/")
  connection = None # would likely be replaced by DiscordWebSocket.connect()

  @classmethod
  async def connect(cls) -> NoReturn:
    wss : Dict = await cls.get("gateway") # {"url": "wss://gateway.discord.gg"}
    async with cls.session() as session:
      response = await session.ws_connect(f"{wss['url']}/") # AssertionError
      return response # debug stuff lol

  @classmethod
  async def get(cls, endpoint : str) -> Dict:
    async with cls.session() as session:
      response = session.get(endpoint)
      return await response.json()


async def main() -> NoReturn:
  print(f"{await DiscordWebSocket.connect() = }")

run(main())
  1. Retrieve the WSS URL from Get Gateway endpoint and pass it to async ClientSession.ws_connect()

Expected behavior

It would ( ? should ? ) print await DiscordWebSocket.connect() = <aiohttp.ClientWebSocketResponse ...> in the console

Logs/tracebacks

> python .\main.py
Traceback (most recent call last):
  File "C:\Users\demo\OneDrive\Documents\python\test\main.py", line 32, in <module>
    run(main())
  File "C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\demo\OneDrive\Documents\python\test\main.py", line 29, in main
    print(f"{await WebSocket.connect() = }")
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\demo\OneDrive\Documents\python\test\main.py", line 16, in connect
    response = await session.ws_connect(str(URL(f"{wss['url']}/")))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\client.py", line 1002, in _ws_connect
    resp = await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\client.py", line 535, in _request
    url = self._build_url(str_or_url)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\client.py", line 467, in _build_url
    assert not url.absolute
           ^^^^^^^^^^^^^^^^
AssertionError

Python Version

$ python --version
Python 3.12.4
Python 3.13.0

aiohttp Version

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.11.7
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author:
Author-email:
License: Apache-2.0
Location: C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires: aiohappyeyeballs, aiosignal, attrs, frozenlist, multidict, propcache, yarl
Required-by: discord.py

multidict Version

$ python -m pip show multidict
Version: 6.0.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires:
Required-by: aiohttp, yarl

propcache Version

$ python -m pip show propcache
Name: propcache
Version: 0.2.0
Summary: Accelerated property cache
Home-page: https://github.com/aio-libs/propcache
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires:
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl
Name: yarl
Version: 1.18.0
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: C:\Users\demo\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires: idna, multidict, propcache
Required-by: aiohttp

OS

Windows 10

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions