Skip to content

http.client: Usage of mutable default headers={} in HTTPConnection.request #142279

@pareshjoshij

Description

@pareshjoshij

Bug report

Bug description:

Hi everyone,

I noticed that the request method defines headers with a mutable default argument ({}):

# Lib/http/client.py
def request(self, method, url, body=None, headers={}, *, encode_chunked=False):

I know the Python docs have a specific warning about this (in Section 4.9.1) because mutable defaults can sometimes accumulate values across calls if they get modified.

It looks like the current code is actually safe because _send_request just reads from the dictionary and doesn't write to it, so I doubt this is causing any bugs right now.

But I was just wondering—is this kept as {} for backward compatibility (maybe for tools inspecting the signature)? Or would it be better to update it to None to follow the standard best practice?

If you think it's worth fixing, I’d be happy to put together a PR for it. If it's better to just leave it alone to be safe, I totally understand!

Thanks!

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions