Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@pareshjoshij

Description

@pareshjoshij
Issue body actions

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

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.