`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`

Open Beginner friendly
#7,645 0 comments 0 reactions 0 assignees View on GitHub

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
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
api

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from psf/requests

All issues in psf/requests

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.