Fix PreparedRequest.copy() sharing hooks reference with original
PreparedRequest.copy() currently assigns p.hooks = self.hooks which means the copy shares the same hooks dictionary and callback lists with the original request. Modifying hooks on the copy (e.g., registering a new response hook) will unintentionally affect the original as well.
This is inconsistent with how the method handles other mutable attributes — headers and _cookies are both properly copied via .copy() and _copy_cookie_jar(), respectively.
import requests
req = requests.Request("GET", "https://example.com").prepare()
copy = req.copy()
copy.hooks["response"].append(lambda r, **kw: print("hook fired"))
print(req.hooks["response"]) # ['<lambda>'] — original was modified
The fix creates a new dict with shallow-copied callback lists so that adding or removing hooks on a copy does not affect the original.
Fair point about file-like objects — copy() can never provide a full deep copy because of those. But I think fixing the hooks case is still worthwhile since hooks is just a plain dict of lists, and the mutation there is unexpected and easy to avoid (unlike file handles). The current inconsistency where headers and cookies are copied but hooks aren't seems like an oversight rather than a deliberate decision.
What approach would you prefer? Happy to adjust.