Skip to content

remove requests dependency#401

Merged
glyph merged 14 commits into
trunkfrom
no-more-requests
Jun 2, 2026
Merged

remove requests dependency#401
glyph merged 14 commits into
trunkfrom
no-more-requests

Conversation

@glyph

@glyph glyph commented Sep 19, 2024

Copy link
Copy Markdown
Member

Fixes #325

In order to know whether we are even testing the various requests extensions, we want to have type hints that actually reflect the signatures on all the helpers.

I've definitely bitten off more than I can chew for getting this done today, so if anyone else wants to have a look at this, please be my guest.

Comment thread src/treq/api.py Outdated
return _client(kwargs).head(url, _stacklevel=4, **kwargs)


@_like(HTTPClient.get)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach... I have a branch somewhere where I started factoring out TypedDict for all the arguments but it is so painful I haven't worked up the will to finish it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, huh, will Mypy actually do the right thing with a TypedDict in this context?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I do think I prefer this approach regardless, that one had just never occurred to me.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect it will given Unpack (my experience actually doing this is with Pyright).

I certainly agree that this is less painful!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that there are details that this can't capture (the addition of the agent= argument) so I ended up skipping it.

@glyph glyph marked this pull request as ready for review May 30, 2026 23:05
@glyph glyph requested a review from a team May 30, 2026 23:06
@glyph

glyph commented May 30, 2026

Copy link
Copy Markdown
Member Author

2 things in particular that I'd like feedback on from a reviewer, beyond the usual:

  1. This ended up pulling in a lot of typing-related fixes because I didn't want to make this change without some type hints giving me at least some indication that I was properly passing everything down.
  2. TreqieJar is obviously a terrible name, but I didn't want to drop indexing support on the result type because that would be a hard compatibility break. any thoughts?
  3. what kind of doc updates would be best for this

@adiroiban adiroiban left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the goal of this PR to have a complete removal of requests ?

I guess that we should also update Python package definition and remove the requests dependency.

"requests >= 2.1.0",


Regarding typing. I think that it's a good improvement.
I don't have much feedback about typing as I am still not using Python typing on my day to day work... so I don't have much experience with that.


I don't think this needs more docs.

I think that the current docs page is good enough

https://treq.readthedocs.io/en/latest/#why


while on it, I have update the treq repo to link to the docs since I guess that this is the public website for the project

Image

Comment thread src/treq/cookies.py Outdated
Comment thread src/treq/client.py Outdated
@glyph

glyph commented May 31, 2026

Copy link
Copy Markdown
Member Author

Is the goal of this PR to have a complete removal of requests ?

I guess that we should also update Python package definition and remove the requests dependency.

oh, yeah, this definitely needs to be done.

@glyph

glyph commented May 31, 2026

Copy link
Copy Markdown
Member Author

oh, yeah, this definitely needs to be done.

aaaand done, in edd757f . I manually reviewed some pip freeze output in various tox environments to make sure that it is, indeed, not installed.

@adiroiban adiroiban left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good.

Maybe we can have a CI task to check that 'requests' is not a dependency.

As a start. Something simple.
Maybe add this before installing tox.

${{ runner.os }}-pip-
- run: python -m pip install 'tox<4'

    - name: Check that requests is not installed
      run: |
        python -m pip install .
        if python -m pip freeze | grep -q "requests=="; then
          echo "The 'requests' library should not be a dependency of the project"
          false
        fi

@glyph

glyph commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

Maybe we can have a CI task to check that 'requests' is not a dependency.

In this case a single manual test should work; we don't have ongoing regression tests to make sure we don't depend on NumPy or PyTorch for example :). The right way to do this is with constraints files, which we should eventually swtich to, but probably not as part of this change.

@glyph glyph merged commit 9fa973c into trunk Jun 2, 2026
17 checks passed
@glyph glyph deleted the no-more-requests branch June 2, 2026 07:12

@twm twm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking this on, @glyph! I'm a bit too tired to fully review tonight, but I noted an issue with treq.request() that should really be addressed before merge.

One other thought — doesn't the requests cookie jar also support assignment indexing? IIRC, to create a ☢️ supercookie ☢️. There are some other extensions, like get() and set() take domain and path args, etc.

Comment thread src/treq/api.py
:class:`hyperlink.EncodedURL` objects.
"""
return _client(kwargs).request(method, url, _stacklevel=3, **kwargs)
return _client(**kwargs).request(method, url, _stacklevel=3, **kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As _client() takes a different set of 4 arguments as .request() I don't think this'll work — it'll need the same re-declaration of all the arguments as get(), delete(), etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. How come the tests didn't catch this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there isn't any test that actually passes reactor. There's a similar issue if you pass agent, too.

Comment thread src/treq/api.py

R = TypeVar("R")

SomeURL = Union[DecodedURL, EncodedURL, str, bytes]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be _URLType? (Though SomeURL is a more pleasing name for it.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe _SomeURL? Should it go in _types? I don't have strong feelings.

Comment thread src/treq/cookies.py
Comment on lines +17 to +18
allows for indexing to retrieve cookie values. This is for convenience and
for compatibility with `requests` users expectations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should put a hazard warning sticker on this — it's dangerous to disregard the provenance of cookies in the face of HTTP redirects. IMO indexing by cookie name is dangerous enough that we should consider deprecating it, even as we do need it for compatibility. We provide the search() function as a safer interface.

Comment thread src/treq/cookies.py

In general, you should not need to import or instantiate a
:py:class:`IndexableCookieJar` directly; anywhere that treq requires cookies, a
:py:class:`http.cookiejar.CookieJar` or ``dict`` of ``str`` to ``str``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use the typing syntax to be succinct?

Suggested change
:py:class:`http.cookiejar.CookieJar` or ``dict`` of ``str`` to ``str``
:py:class:`http.cookiejar.CookieJar` or ``dict[str, str]``

Comment thread docs/api.rst
Comment on lines +97 to +98
.. autoclass:: IndexableCookieJar

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since users aren't expected to instantiate this, should it live below scoped_cookie() and search() in the docs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong feelings from me on this one, but rearrange if you think it looks better.

@twm

twm commented Jun 2, 2026

Copy link
Copy Markdown
Member

Oh no.

@glyph

glyph commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

@twm Oops :). Do you think you could do a follow-up to address this stuff? I would rather intentionally leave off the supercookie-setitem; our own tests and examples don't depend on it. Should we deprecate the indexing support for similar reasons?

@twm

twm commented Jun 4, 2026

Copy link
Copy Markdown
Member

@glyph I'm happy to follow up! I'd love to move this toward release.

I think that while removing the remainder of the it is technically an incompatible change, most of the behavior I mentioned is so hazardous an interface that I'd rather not support it at all. I think that we can settle for acknowledging the compatibility implications in the changelog.

@glyph

glyph commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@glyph I'm happy to follow up! I'd love to move this toward release.

I think that while removing the remainder of the it is technically an incompatible change, most of the behavior I mentioned is so hazardous an interface that I'd rather not support it at all. I think that we can settle for acknowledging the compatibility implications in the changelog.

Sounds good. I'll try to review when I can!

@twm twm mentioned this pull request Jun 6, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eliminate dependency on requests

3 participants