internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: encode/httpx

Issue:
`allow_redirects` with `httpx.head(...)`
We've currently got a different `allow_redirects` default on HEAD requests to all other request methods.

https://github.com/encode/httpx/blob/a25d924bb910e85ca597c7b4ed819c6e9e17aecd/httpx/_api.py#L217

This is in line with behaviour in `requests`, but I'm not convinced it's something we ought to do. It'd seem to me that we'd do better just to stay consistent rather than have this odd exclusion. I get why it *could* be a reasonable choice, but I think it'd *probably* be better to just stay consistent, and allow the user to switch `allow_redirects=False` if that's their intent.

Flagging this to be addressed pre-1.0.


Discussion:

Resolving patch:
diff --git a/httpx/_api.py b/httpx/_api.py
--- a/httpx/_api.py
+++ b/httpx/_api.py
@@ -214,7 +214,7 @@ def head(
     headers: HeaderTypes = None,
     cookies: CookieTypes = None,
     auth: AuthTypes = None,
-    allow_redirects: bool = False,  # Note: Differs to usual default.
+    allow_redirects: bool = True,
     cert: CertTypes = None,
     verify: VerifyTypes = True,
     timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
diff --git a/httpx/_client.py b/httpx/_client.py
--- a/httpx/_client.py
+++ b/httpx/_client.py
@@ -849,7 +849,7 @@ def head(
         headers: HeaderTypes = None,
         cookies: CookieTypes = None,
         auth: typing.Union[AuthTypes, UnsetType] = UNSET,
-        allow_redirects: bool = False,  # NOTE: Differs to usual default.
+        allow_redirects: bool = True,
         timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
     ) -> Response:
         """
@@ -1453,7 +1453,7 @@ async def head(
         headers: HeaderTypes = None,
         cookies: CookieTypes = None,
         auth: typing.Union[AuthTypes, UnsetType] = UNSET,
-        allow_redirects: bool = False,  # NOTE: Differs to usual default.
+        allow_redirects: bool = True,
         timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
     ) -> Response:
         """


Test patch:
Repository: encode/httpx

Issue:
Add API docs for streaming
- [x] There are no similar issues or pull requests for this yet.
- [x] I discussed this idea in #1152 and feedback is positive.

There is no documentation in either the top level `stream` or `Client.stream`, and they are not mentioned in generated documentation. At least a short description of functionality and parameters (pointing to `httpx.request` if applicable) should be added.


Discussion:
So the streaming docs are here... https://www.python-httpx.org/quickstart/#streaming-responses

But yes, they're missing from the API documentation at the moment.
Yes, that's what I meant by generated documentation! And thanks for the reference link 👍 

Resolving patch:
diff --git a/httpx/_api.py b/httpx/_api.py
--- a/httpx/_api.py
+++ b/httpx/_api.py
@@ -118,6 +118,16 @@ def stream(
     cert: CertTypes = None,
     trust_env: bool = True,
 ) -> StreamContextManager:
+    """
+    Alternative to `httpx.request()` that streams the response body
+    instead of loading it into memory at once.
+
+    **Parameters**: See `httpx.request`.
+
+    See also: [Streaming Responses][0]
+
+    [0]: /quickstart#streaming-responses
+    """
     client = Client(proxies=proxies, cert=cert, verify=verify, trust_env=trust_env)
     request = Request(
         method=method,
diff --git a/httpx/_client.py b/httpx/_client.py
--- a/httpx/_client.py
+++ b/httpx/_client.py
@@ -191,6 +191,16 @@ def stream(
         allow_redirects: bool = True,
         timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
     ) -> "StreamContextManager":
+        """
+        Alternative to `httpx.request()` that streams the response body
+        instead of loading it into memory at once.
+
+        **Parameters**: See `httpx.request`.
+
+        See also: [Streaming Responses][0]
+
+        [0]: /quickstart#streaming-responses
+        """
         request = self.build_request(
             method=method,
             url=url,


Test patch:
Repository: encode/httpx

Issue:
Auto headers inserted into `headers._list` while not into `headers._dict`
### Checklist

- [x] The bug is reproducible against the latest release and/or `master`.
- [x] There are no similar issues or pull requests to fix it yet.

### Describe the bug
`Request.prepare()` insert auto headers into `headers.raw` (ie `headers._list`), but not into the `headers._dict`.
https://github.com/encode/httpx/blob/924fa8c9dc19f3a601db54a384158fef56f07c96/httpx/_models.py#L650-L651

### To reproduce
```python
>>> import httpx
>>> client = httpx.Client()
>>> request = client.build_request("GET", "https://google.com")
>>> requests.headers
Headers({'host': 'google.com', 'user-agent': 'python-httpx/0.14.1', 'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'connection': 'keep-alive'})
>>> "host" in request.headers
False
>>> request.headers.pop("host")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\programs\anaconda3\lib\_collections_abc.py", line 801, in pop
    del self[key]
  File "E:\projects\pycharm\httpx\httpx\_models.py", line 536, in __delitem__
    del self._dict[del_key]
KeyError: b'host'
```

### Environment

- OS: Windows/macOS
- Python version: 3.7.6
- HTTPX version: 0.14.1



Discussion:

Resolving patch:
diff --git a/httpx/_models.py b/httpx/_models.py
--- a/httpx/_models.py
+++ b/httpx/_models.py
@@ -411,9 +411,8 @@ def encoding(self, value: str) -> None:
     def raw(self) -> typing.List[typing.Tuple[bytes, bytes]]:
         """
         Returns a list of the raw header items, as byte pairs.
-        May be mutated in-place.
         """
-        return self._list
+        return list(self._list)
 
     def keys(self) -> typing.KeysView[str]:
         return {key.decode(self.encoding): None for key in self._dict.keys()}.keys()
@@ -647,8 +646,7 @@ def prepare(self) -> None:
         if not has_connection:
             auto_headers.append((b"connection", b"keep-alive"))
 
-        for item in reversed(auto_headers):
-            self.headers.raw.insert(0, item)
+        self.headers = Headers(auto_headers + self.headers.raw)
 
     @property
     def content(self) -> bytes:


Test patch:
diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py
--- a/tests/client/test_headers.py
+++ b/tests/client/test_headers.py
@@ -5,7 +5,7 @@
 import httpcore
 import pytest
 
-from httpx import AsyncClient, Headers, __version__
+from httpx import AsyncClient, Headers, Request, __version__
 from httpx._content_streams import ContentStream, JSONStream
 
 
@@ -181,3 +181,8 @@ async def test_host_with_non_default_port_in_url():
             "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
         }
     }
+
+
+def test_request_auto_headers():
+    request = Request("GET", "https://www.example.org/")
+    assert "host" in request.headers
Repository: encode/httpx

Issue:
Packaging dependancy tweaks
* Include `rfc3986` as `rfc3986[idna2008]`, and *don't* include the `idna` dependancy ourselves.
* Add an `extra_requires = {"brotli": "brotlipy==0.7.*"'}`
* Update our dependancy info in the docs/index.md and README.md

---

Our 1.0 release deps ought to look like this...

* `httpcore` - The underlying transport implementation for `httpx`.
  * `h11` - HTTP/1.1 support.
  * `h2` - HTTP/2 support. *(Optional, `pip install httpx[http2]`)*
* `rfc3986` - URL parsing & normalization.
    * `idna` - Internationalized domain name support.
* `certifi` - SSL certificates.
* `sniffio` - Async library autodetection.
* `brotlipy` - Decoding for "brotli" compressed responses. *(Optional, `pip install httpx[brotli]`)*

**Note that I've dropped the following...**

* `chardet` - Fallback auto-detection for response encoding.  **- See #1018**
* `urllib3` - Support for the `httpx.URLLib3Transport` class. *(Optional)* **- See https://github.com/encode/httpx/issues/1143**



Discussion:

Resolving patch:
diff --git a/httpx/_decoders.py b/httpx/_decoders.py
--- a/httpx/_decoders.py
+++ b/httpx/_decoders.py
@@ -108,9 +108,13 @@ class BrotliDecoder(Decoder):
     """
 
     def __init__(self, request: "Request") -> None:
-        assert (
-            brotli is not None
-        ), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'"
+        if brotli is None:  # pragma: nocover
+            raise ImportError(
+                "Using 'BrotliDecoder', but the 'brotlipy' or 'brotli' library "
+                "is not installed."
+                "Make sure to install httpx using `pip install httpx[brotli]`."
+            ) from None
+
         self.request = request
         self.decompressor = brotli.Decompressor()
         self.seen_data = False
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -58,12 +58,12 @@ def get_packages(package):
         "certifi",
         "sniffio",
         "chardet==3.*",
-        "idna==2.*",
-        "rfc3986>=1.3,<2",
+        "rfc3986[idna2008]>=1.3,<2",
         "httpcore==0.10.*",
     ],
     extras_require={
         "http2": "h2==3.*",
+        "brotli": "brotlipy==0.7.*",
     },
     classifiers=[
         "Development Status :: 4 - Beta",


Test patch:
Repository: encode/httpx

Issue:
Docs incorrectly reference `HTTPError.message` attribute
### Checklist

<!-- Please make sure you check all these items before submitting your bug report. -->

- [x] The bug is reproducible against the latest release and/or `master`.
- [x] There are no similar issues or pull requests to fix it yet.

### Describe the bug

<!-- A clear and concise description of what the bug is. -->

The documentation indicates that message field can be used in the HTTPError:
https://github.com/encode/httpx/blob/master/httpx/_exceptions.py#L54
```
    try:
        response = httpx.get("https://www.example.com")
        response.raise_for_status()
    except httpx.HTTPError as exc:
        print(f"HTTP Exception for {exc.request.url} - {exc.message}")
```
But there is not such field:
```
AttributeError: 'HTTPStatusError' object has no attribute 'message'
```


### To reproduce

Execute the example from the doc

### Expected behavior

Print the string without raising any exceptions

### Actual behavior

AttributeError is raised

### Possible fixes

1. Update the documentation to use `str(exc)` instead of `exc.message`
or
2. Set `self.message` field in HTTPError



Discussion:
Good catch, yup. Of those two I *think* we should probably set a `.message` attribute on the exception instances, right here... https://github.com/encode/httpx/blob/19b863af408123783d5f9916e71688b32ba558c8/httpx/_exceptions.py#L63-L64

Changing it to...

```python
super().__init__(message) 
self.message = message
self.request = request 
```

Though you're right, the alternative would be a documentation fix, to read eg...

```python
print(f"HTTP Exception for {exc.request.url} - {exc}")
```

Resolving patch:
diff --git a/httpx/_exceptions.py b/httpx/_exceptions.py
--- a/httpx/_exceptions.py
+++ b/httpx/_exceptions.py
@@ -55,7 +55,7 @@ class HTTPError(Exception):
         response = httpx.get("https://www.example.com")
         response.raise_for_status()
     except httpx.HTTPError as exc:
-        print(f"HTTP Exception for {exc.request.url} - {exc.message}")
+        print(f"HTTP Exception for {exc.request.url} - {exc}")
     ```
     """
 


Test patch:
Repository: encode/httpx

Issue:
Confusing error messages when passing in the wrong URL type 
https://github.com/encode/httpx/blob/642aabdac093cf9798f4881cbdd8b39bd3398bb5/httpx/_models.py#L74

Say, for example, we're using a Starlette API which features a URL type: https://github.com/encode/starlette/blob/c566fc6c819f0d565f8cff432351fe009e83d866/starlette/datastructures.py#L14

And you might mistakenly think you could pass that URL type in and use it, but of course you'd be wrong because:
```
class URL:
    def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:
        if isinstance(url, str):
            ...
        else:
            self._uri_reference = url._uri_reference
```

It is not a string, but also does not have a `._uri_reference` field.

The resulting error we got, which was hard for us to debug was:
```
'URL' object has no attribute '_uri_reference'
Traceback (most recent call last):
  ...
  File <redacted>, line 27, in <redacted>
    async with AsyncClient(base_url=settings.service_baselink) as client:
  File "<redacted>/lib/python3.7/site-packages/httpx/_client.py", line 1125, in __init__
    trust_env=trust_env,
  File "<redacted>/lib/python3.7/site-packages/httpx/_client.py", line 72, in __init__
    self._base_url = self._enforce_trailing_slash(URL(base_url))
  File "<redacted>/lib/python3.7/site-packages/httpx/_models.py", line 74, in __init__
    self._uri_reference = url._uri_reference
AttributeError: 'URL' object has no attribute '_uri_reference'
```

I would suggest that this block be changed to have a further check on the appropriate expected types and then raise a type error rather than an attribute error here.


Discussion:

Resolving patch:
diff --git a/httpx/_models.py b/httpx/_models.py
--- a/httpx/_models.py
+++ b/httpx/_models.py
@@ -71,8 +71,12 @@ def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:
                 # We don't want to normalize relative URLs, since doing so
                 # removes any leading `../` portion.
                 self._uri_reference = self._uri_reference.normalize()
-        else:
+        elif isinstance(url, URL):
             self._uri_reference = url._uri_reference
+        else:
+            raise TypeError(
+                f"Invalid type for url.  Expected str or httpx.URL, got {type(url)}"
+            )
 
         # Add any query parameters, merging with any in the URL if needed.
         if params:


Test patch:
diff --git a/tests/models/test_url.py b/tests/models/test_url.py
--- a/tests/models/test_url.py
+++ b/tests/models/test_url.py
@@ -204,3 +204,11 @@ def test_url_copywith_for_userinfo():
 def test_url_invalid():
     with pytest.raises(httpx.InvalidURL):
         httpx.URL("https://😇/")
+
+
+def test_url_invalid_type():
+    class ExternalURLClass:  # representing external URL class
+        pass
+
+    with pytest.raises(TypeError):
+        httpx.URL(ExternalURLClass())  # type: ignore
Repository: encode/httpx

Issue:
Wrong elapsed timedelta
### Checklist

- [ X] The bug is reproducible against the latest release and/or `master`.
- [ X] There are no similar issues or pull requests to fix it yet.

### Describe the bug

The `elapsed` argument obtained following a stream request seems completely incoherent.

### To reproduce

Code example with httpx 0.15.1:

```
import httpx

client = httpx.Client()

with client.stream("GET", "https://httpbin.org/basic-auth/user/pass", auth=("user", "pass")) as r:
   print(r)

print(r.elapsed)
```

### Expected behavior

Result got with httpx 0.14.3: `0:00:02.275297`

### Actual behavior

Result got with httpx 0.15.1: `1 day, 17:07:15.687523`

### Debugging material

![image](https://user-images.githubusercontent.com/12841394/93995268-15707c00-fd91-11ea-913e-a6741a251d1d.png)


### Environment

- OS: Linux
- Python version: 3.7.5
- HTTPX version: 0.15.1
- Async environment: None
- HTTP proxy: No
- Custom certificates: No

Thanks,



Discussion:
👀 Yikes.

Resolving patch:
diff --git a/httpx/_client.py b/httpx/_client.py
--- a/httpx/_client.py
+++ b/httpx/_client.py
@@ -859,7 +859,7 @@ def _send_single_request(self, request: Request, timeout: Timeout) -> Response:
             )
 
         def on_close(response: Response) -> None:
-            response.elapsed = datetime.timedelta(timer.sync_elapsed())
+            response.elapsed = datetime.timedelta(seconds=timer.sync_elapsed())
             if hasattr(stream, "close"):
                 stream.close()
 
@@ -1504,7 +1504,7 @@ async def _send_single_request(
             )
 
         async def on_close(response: Response) -> None:
-            response.elapsed = datetime.timedelta(await timer.async_elapsed())
+            response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
             if hasattr(stream, "close"):
                 await stream.aclose()
 


Test patch:
Repository: encode/httpx

Issue:
Async client does not close underlying stream thus leaving connection active which leads to hitting pool limit
### Checklist

<!-- Please make sure you check all these items before submitting your bug report. -->

- [x] The bug is reproducible against the latest release and/or `master`.
- [x] There are no similar issues or pull requests to fix it yet.

### Describe the bug

``AsyncClient`` doesn't close ``httpcore``'s stream after performing request, the bug shows itself in locking on connection pool's semaphore.

### To reproduce

```
$ python3.8 -m asyncio
>>> import asyncio
>>> import httpx
>>> c = httpx.AsyncClient()
>>> await c.get('http://httpbin.org/')
<Response [200 OK]>
>>> c._transport._connections
{(b'http', b'httpbin.org', 80): {<AsyncHTTPConnection http_version=HTTP/1.1 state=2>}}
>>> await c.get('http://httpbin.org/')
<Response [200 OK]>
>>> c._transport._connections
{(b'http', b'httpbin.org', 80): {<AsyncHTTPConnection http_version=HTTP/1.1 state=2>, <AsyncHTTPConnection http_version=HTTP/1.1 state=2>}}
```

### Expected behavior

There should be one connection in pool in ``IDLE`` state.

### Actual behavior

There are 2 connection in ``ACTIVE`` state in pool after performing and finishing request.

### Debugging material

### Environment

- OS: Linux
- Python version: 3.8.5
- HTTPX version: 0.15.0
- Async environment: asyncio/trio
- HTTP proxy: no
- Custom certificates: no

### Additional context

Bug is most obvious in long-running process making regular request to one endpoint.


Discussion:

Resolving patch:
diff --git a/httpx/_client.py b/httpx/_client.py
--- a/httpx/_client.py
+++ b/httpx/_client.py
@@ -1505,7 +1505,7 @@ async def _send_single_request(
 
         async def on_close(response: Response) -> None:
             response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
-            if hasattr(stream, "close"):
+            if hasattr(stream, "aclose"):
                 await stream.aclose()
 
         response = Response(


Test patch:
Repository: encode/httpx

Issue:
ResponseNotRead raises since v.0.15.0
### Checklist

- [x] The bug is reproducible against the latest release and/or `master`.

### Describe the bug
Response behaviour has been changed since 0.15.0, so that calling json() on response instance raises ResponseNotRead exception.

### To reproduce
import httpx
r = httpx.Response(200, content='content')
result = r.json()

### Expected behavior
result = 'content'

### Actual behavior
httpx.ResponseNotRead: Attempted to access response content, without having called `read()` after a streaming response.


### Environment

- OS: Linux
- Python version:  3.8.2
- HTTPX version: 0.15.3
- Async environment: asyncio
- HTTP proxy: no
- Custom certificates: no




Discussion:

Resolving patch:
diff --git a/httpx/_models.py b/httpx/_models.py
--- a/httpx/_models.py
+++ b/httpx/_models.py
@@ -900,7 +900,7 @@ def __init__(
             headers, stream = encode_response(content, text, html, json)
             self._prepare(headers)
             self.stream = stream
-            if content is None or isinstance(content, bytes):
+            if content is None or isinstance(content, (bytes, str)):
                 # Load the response body, except for streaming content.
                 self.read()
 


Test patch:
diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py
--- a/tests/models/test_responses.py
+++ b/tests/models/test_responses.py
@@ -38,6 +38,19 @@ def test_response():
     assert not response.is_error
 
 
+def test_response_content():
+    response = httpx.Response(200, content="Hello, world!")
+
+    assert response.status_code == 200
+    assert response.reason_phrase == "OK"
+    assert response.text == "Hello, world!"
+    assert response.headers == httpx.Headers(
+        {
+            "Content-Length": "13",
+        }
+    )
+
+
 def test_response_text():
     response = httpx.Response(200, text="Hello, world!")
Repository: encode/httpx

Issue:
Nicer repr for `httpx.Cookie`, displaying stored cookies.
When i'm trying to save cookies with these code:
with open('Cookies/cookies.txt', 'w') as f:
        json.dump(dict(client.cookies), f)

i got error:
raise CookieConflict(message)
httpx.CookieConflict: Multiple cookies exist with name=client_type

i'm also curios, how to print all session cookies like in requests module (using print(session.cookies)
when im using print(client.cookies) with httpx module, the result was <httpx.Cookies object at 0x03B50E20>

can you help me?




Discussion:
@Mavic111 use `client.cookies.jar` to print, there will be more than 1 cookie with the same key name in there, so when your converting to a dict, that is your issue.
Requests will give you the same behaviour wrt. multiple cookies with the same name...

```python
>>> dict(s.cookies)
...
requests.cookies.CookieConflictError: There are multiple cookies with name, 'name'
```

Tho I. would agree that our `Cookies` repr would be nicer if it displayed the stored cookies. In the same way that `RequestsCookieJar` does...

```python
>>> print(s.cookies)
<RequestsCookieJar[<Cookie name=value for httpbin.org/>, <Cookie name=value2 for www.httpbin.org/>]>
```
I've retitled the issue to reflect the one aspect of this that I think we could improve on.

Resolving patch:
diff --git a/httpx/_models.py b/httpx/_models.py
--- a/httpx/_models.py
+++ b/httpx/_models.py
@@ -1491,6 +1491,16 @@ def __bool__(self) -> bool:
             return True
         return False
 
+    def __repr__(self) -> str:
+        cookies_repr = ", ".join(
+            [
+                f"<Cookie {cookie.name}={cookie.value} for {cookie.domain} />"
+                for cookie in self.jar
+            ]
+        )
+
+        return f"<Cookies[{cookies_repr}]>"
+
     class _CookieCompatRequest(urllib.request.Request):
         """
         Wraps a `Request` instance up in a compatibility interface suitable


Test patch:
diff --git a/tests/models/test_cookies.py b/tests/models/test_cookies.py
--- a/tests/models/test_cookies.py
+++ b/tests/models/test_cookies.py
@@ -85,3 +85,14 @@ def test_cookies_can_be_a_list_of_tuples():
     assert len(cookies.items()) == 2
     for k, v in cookies_val:
         assert cookies[k] == v
+
+
+def test_cookies_repr():
+    cookies = httpx.Cookies()
+    cookies.set(name="foo", value="bar", domain="http://blah.com")
+    cookies.set(name="fizz", value="buzz", domain="http://hello.com")
+
+    assert (
+        repr(cookies)
+        == "<Cookies[<Cookie foo=bar for http://blah.com />, <Cookie fizz=buzz for http://hello.com />]>"
+    )