`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`
Nobody has claimed this yet.
- #1 by @draarivpatel-ui — closed without merging
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
Research direction
The failure is in iter_lines in src/requests/models.py, where chunk.split(delimiter) receives a str delimiter while the chunk is bytes because Response.encoding is None. Run the reproduction snippet from the issue to confirm the TypeError, then check how iter_lines decodes chunks when decode_unicode=True. Done means the call either yields str lines or raises a clear error, with a test added alongside the existing iter_lines tests. The closed, unmerged linked pull request may show an earlier attempt worth reading first.
Written by the indexing model from the issue text.
Description
Expected Result
Either return lines, or raise a clear error message
I spotted this while investigating whether Pyrefly could run on the Requests source code. (happy to make a PR to add Pyrefly if you'd be interested in it by the way, it may avert future issues like this one? looks like only collaboratos can make PRs, so maybe not)
Actual Result
Unexpected error message:
[b'line one', b'line two', b'line three']
Traceback (most recent call last):
File "/home/mgorelli/scratch/t.py", line 15, in <module>
print(list(make_response().iter_lines(decode_unicode=True, delimiter="\n")))
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mgorelli/scratch/.venv/lib/python3.14/site-packages/requests/models.py", line 1016, in iter_lines
lines = chunk.split(delimiter) # type: ignore[arg-type]
TypeError: a bytes-like object is required, not 'str'
Reproduction Steps
import io
import requests
def make_response():
r = requests.models.Response()
r.status_code = 200
r.raw = io.BytesIO(b"line one\nline two\nline three")
r.encoding = None # e.g. a Content-Type with no charset
return r
print(list(make_response().iter_lines(decode_unicode=True)))
print(list(make_response().iter_lines(decode_unicode=True, delimiter="\n")))
System Information
$ python -m requests.help
{
"chardet": {
"version": null
},
"charset_normalizer": {
"version": "3.5.2"
},
"cryptography": {
"version": ""
},
"idna": {
"version": "3.20"
},
"implementation": {
"name": "CPython",
"version": "3.14.4"
},
"platform": {
"release": "6.6.87.2-microsoft-standard-WSL2",
"system": "Linux"
},
"pyOpenSSL": {
"openssl_version": "",
"version": null
},
"requests": {
"version": "2.34.2"
},
"system_ssl": {
"version": "30500060"
},
"urllib3": {
"version": "2.8.0"
},
"using_charset_normalizer": true,
"using_pyopenssl": false
}
- Dominant language
- Python
- Stars
- 54.7k
- Forks
- 10.8k
- Avg merge
- 18m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from psf/requests
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)Possibly taken @suyashgitt claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
OCA/account-invoicing#2529 ·
Maintainers usually reply within 1 day
-
area/ci
Difficulty 1/5 Under an hour Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
ImageGen generate-batch aborts valid JSONL jobs containing a Unicode line separator in poster textOpenbug imagen skills
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Email validation accepts a trailing newlinePossibly taken @marcello-pichini claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day