Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using underscore to name a field in a named tuple #8228

Closed
1 task done
aleksa opened this issue Mar 13, 2024 · 2 comments
Closed
1 task done

Using underscore to name a field in a named tuple #8228

aleksa opened this issue Mar 13, 2024 · 2 comments
Labels

Comments

@aleksa
Copy link

aleksa commented Mar 13, 2024

Describe the bug

An internal BasicAuth issue: aiohttp connection can fail because a field name _password in a named tuple in /aiohttp/helpers.py starts with an underscore.

https://docs.python.org/3/library/collections.html#collections.namedtuple

Any valid Python identifier may be used for a fieldname except for names starting with an underscore.

To Reproduce

Try to run this code:

#!/usr/bin/env python

import asyncio
import aiohttp


async def main() -> None:
    url: str = "ws://localhost:8080"
    async with aiohttp.ClientSession() as session:
        async with session.ws_connect(url) as ws:
            await ws.send_str("Test")


if __name__ == "__main__":
    asyncio.run(main())

Expected behavior

It should fail, or succeed, but not because of an issue with an underscore in a BasicAuth named tuple.

Logs/tracebacks

Traceback (most recent call last):
  File "./test_ws.py", line 4, in <module>
    import aiohttp
  File "vlpy312/lib/python3.12/site-packages/aiohttp/__init__.py", line 6, in <module>
    from .client import (
  File "vlpy312/lib/python3.12/site-packages/aiohttp/client.py", line 38, in <module>
    from . import hdrs, http, payload
  File "vlpy312/lib/python3.12/site-packages/aiohttp/http.py", line 7, in <module>
    from .http_parser import (
  File "vlpy312/lib/python3.12/site-packages/aiohttp/http_parser.py", line 30, in <module>
    from .helpers import (
  File "vlpy312/lib/python3.12/site-packages/aiohttp/helpers.py", line 117, in <module>
    class BasicAuth(namedtuple("BasicAuth", ["login", "_password", "encoding"])):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/.pyenv/versions/3.12.2/lib/python3.12/collections/__init__.py", line 409, in namedtuple
    raise ValueError('Field names cannot start with an underscore: '
ValueError: Field names cannot start with an underscore: '_password'

Python Version

$ python --version

Python 3.12.2

aiohttp Version

$ python -m pip show aiohttp

Name: aiohttp
Version: 3.9.3
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author:
Author-email:
License: Apache 2
Location: ./vlpy312/lib/python3.12/site-packages
Requires: aiosignal, attrs, frozenlist, multidict, yarl
Required-by:

multidict Version

$ python -m pip show multidict

Name: multidict
Version: 6.0.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: ./vlpy312/lib/python3.12/site-packages
Requires:
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl

Name: yarl
Version: 1.9.4
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Location: ./vlpy312/lib/python3.12/site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

Darwin LUSM7JR49DP75 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000 arm64

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@aleksa aleksa added the bug label Mar 13, 2024
@aleksa
Copy link
Author

aleksa commented Mar 13, 2024

It looks like it is already fixed:

class BasicAuth(namedtuple("BasicAuth", ["login", "password", "encoding"])):
I assume that the new version hasn't been pushed out yet.

@aleksa aleksa closed this as completed Mar 13, 2024
@Dreamsorcerer
Copy link
Member

That line was created 10 years ago:
https://github.com/aio-libs/aiohttp/blame/58a9190ed092e5748e80e16082592b886d105975/aiohttp/helpers.py#L120

I have no idea how you've got a modified version...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants