Repository: encode/httpx
Pull request: Support header comparisons with dict or list.
Allows comparison of headers with dict or list:

```
response = httpx.get(...)
assert response.status_code == 200
assert response.text == "Hello, world!"
assert response.headers == {"Content-Length": "13"} # True
assert response.headers == [("Content-Length", "13")] # True
```

Closes #1325 

Problem statement:
Support header comparisons with dict / list.
We should support direct header comparisons with dicts, or with lists of two-tuples.
This would be nice because it would help folks clean up test cases, eg...

```python
response = httpx.get(...)
assert response.status_code == 200
assert response.text == "Hello, world!"
assert response.headers == {"Content-Length": "13"}
```

The fix will be something like changing `Headers.__eq__` from this...

```python
def __eq__(self, other: typing.Any) -> bool:
    if not isinstance(other, Headers):
        return False
    return sorted(self._list) == sorted(other._list)
```

To this...

```python
def __eq__(self, other: typing.Any) -> bool:
    try:
        other_headers = Headers(other)
    except ValueError:
        return False
    return sorted(self._list) == sorted(other_headers._list)
```

A pull request resolving this should:

* Make the fix in `Headers.__eq__`.
* Update all our tests to use the simpler comparison style wherever possible.

This would be a nice contributor friendly issue, so if anyone's keen then feel free to jump in.

Hints from issue discussion:

Commit message: Add check for no headers item

Patch to review:
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000000..8c85075de4
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+    "python.pythonPath": "/Users/mus/Projects/httpx/venv/bin/python3"
+}
\ No newline at end of file
diff --git a/httpx/_models.py b/httpx/_models.py
index 03635f3e38..63f2f72279 100644
--- a/httpx/_models.py
+++ b/httpx/_models.py
@@ -724,9 +724,11 @@ def __len__(self) -> int:
         return len(self._list)
 
     def __eq__(self, other: typing.Any) -> bool:
-        if not isinstance(other, Headers):
+        try:
+            other_headers = Headers(other)
+        except ValueError:
             return False
-        return sorted(self._list) == sorted(other._list)
+        return sorted(self._list) == sorted(other_headers._list)
 
     def __repr__(self) -> str:
         class_name = self.__class__.__name__
diff --git a/tests/models/test_headers.py b/tests/models/test_headers.py
index 263db11920..f5c283cee7 100644
--- a/tests/models/test_headers.py
+++ b/tests/models/test_headers.py
@@ -23,7 +23,9 @@ def test_headers():
     assert dict(h) == {"a": "123, 456", "b": "789"}
     assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"
     assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])
-    assert h != [("a", "123"), ("A", "456"), ("b", "789")]
+    assert h == [("a", "123"), ("A", "456"), ("b", "789")]
+    assert h == {"a": "123", "A": "456", "b": "789"}
+    assert h != "a: 123\nA: 456\nb: 789"
 
     h = httpx.Headers({"a": "123", "b": "789"})
     assert h["A"] == "123"


Code review comments:
diff_hunk: @@ -0,0 +1,3 @@
+{
+    "python.pythonPath": "/Users/mus/Projects/httpx/venv/bin/python3"
+}
path: .vscode/settings.json
text: @user1:
We don't really want this file. You might want to setup a global gitignore to avoid including config that's specific to your local dev environment.

@author:
Slipped me. Fixing.