requests icon indicating copy to clipboard operation
requests copied to clipboard

Incorrect type annotation for iter_lines

Open grantisu opened this issue 2 months ago • 6 comments

The type annotation for Response.iter_lines misses that chunk_size can be None, causing valid code to fail type checks.

This seems pretty trivial to fix (e.g. https://github.com/psf/requests/compare/main...grantisu:requests:gmathews/tiny-type-fix), but I'm not able to open a PR since I'm not a collaborator on the repo.

grantisu avatar Sep 24 '26 17:09 grantisu

Hi, I'd like to work on this. I'll submit a focused typing fix with regression coverage shortly.

nightcityblade avatar Sep 24 '26 18:09 nightcityblade

I see the reporter already has the fix prepared on a comparison branch, so I’m stepping back to avoid duplicating that work.

nightcityblade avatar Sep 24 '26 18:09 nightcityblade

I see the reporter already has the fix prepared on a comparison branch, so I’m stepping back to avoid duplicating that work.

The work was pretty minor; feel free to duplicate/copy/steal/expand/etc if it leads to a better/quicker outcome.

grantisu avatar Sep 24 '26 19:09 grantisu

Hi @grantisu @nateprewitt,

Following up on this, I've prepared and verified the fix along with parameterized tests and static typing validation.

Since PR creation is restricted to collaborators on this repository, the tested changes are available on my fork here: Branch: agustin18:requests:fix/iter-lines-chunk-size-type-7630 Compare Diff: psf/requests/compare/main...agustin18:requests:fix/iter-lines-chunk-size-type-7630

Changes

  1. Updated chunk_size: int | None = ITER_CHUNK_SIZE in both @overload signatures and the iter_lines method definition in src/requests/models.py.
  2. Added parameterized regression tests in tests/test_requests.py covering chunk_size=None and chunk_size=512 with both decode_unicode=False and True.
  3. Verified locally:
    • pyright src/requests/: 0 errors.
    • All 6 iter_lines overload variants pass Pyright strict type check.
    • All 343 pytest tests pass.
    • All pre-commit hooks (ruff check, ruff format, whitespace) pass cleanly.

Maintainers can pull directly from this branch or grant access to open the PR.

agustin18 avatar Sep 25 '26 15:09 agustin18

Put together the typing fix along with unit test coverage on a branch here if a collaborator wants to pull or cherry-pick it: https://github.com/DevMubarak1/requests/tree/fix/iter-lines-chunk-size-type (diff: https://github.com/psf/requests/compare/main...DevMubarak1:requests:fix/iter-lines-chunk-size-type)

DevMubarak1 avatar Oct 07 '26 09:10 DevMubarak1

I’m working on a focused fix for the overloads so is accepted by type checkers, with runtime behavior unchanged.

masyarakatai avatar Oct 22 '26 01:10 masyarakatai