Title: http.client: Usage of mutable default headers={} in HTTPConnection.request · Issue #142279 · python/cpython · GitHub
Open Graph Title: http.client: Usage of mutable default headers={} in HTTPConnection.request · Issue #142279 · python/cpython
X Title: http.client: Usage of mutable default headers={} in HTTPConnection.request · Issue #142279 · python/cpython
Description: 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 ...
Open Graph Description: 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, heade...
X Description: 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, heade...
Opengraph URL: https://github.com/python/cpython/issues/142279
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"http.client: Usage of mutable default headers={} in HTTPConnection.request","articleBody":"# Bug report\n\n### Bug description:\n\nHi everyone,\n\n\nI noticed that the request method defines headers with a mutable default argument ```({}):```\n\n```Python\n\n# Lib/http/client.py\ndef request(self, method, url, body=None, headers={}, *, encode_chunked=False):\n```\nI 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.\n\n\nIt 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.\n\nBut 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?\n\nIf 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!\n\nThanks!\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_","author":{"url":"https://github.com/pareshjoshij","@type":"Person","name":"pareshjoshij"},"datePublished":"2025-12-05T06:28:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/142279/cpython/issues/142279"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:dc9359a3-6939-1f4b-2f8b-69d6cd6188a9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A710:381ACD:478F041:63D3063:6A50D907 |
| html-safe-nonce | f5a249572c5cb0c4115e60322fcb42fddfb5a74436771d7b8e5d7fe7bdf531fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzEwOjM4MUFDRDo0NzhGMDQxOjYzRDMwNjM6NkE1MEQ5MDciLCJ2aXNpdG9yX2lkIjoiNDIzNDg3NDI2Mjg2MTY5OTMzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 295fb0dd4ebf6e41cadfcf55dccfd8ab34544502d8ed191ce8a8e0a8283d0ab9 |
| hovercard-subject-tag | issue:3697677999 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/142279/issue_layout |
| twitter:image | https://opengraph.githubassets.com/668262a6b6b37fb638bdf3f9c792f7e488e4b3a242d57781d8d5a180cfd5d31e/python/cpython/issues/142279 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/668262a6b6b37fb638bdf3f9c792f7e488e4b3a242d57781d8d5a180cfd5d31e/python/cpython/issues/142279 |
| og:image:alt | 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, heade... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pareshjoshij |
| hostname | github.com |
| expected-hostname | github.com |
| None | 72b13e0e8d0319acdd27a145cfe73f4f06c791713d0ac4690e41fb68ad28cac0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 73ad5c4eaffed7bb3637024653eabdbca84a855e |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width