responses icon indicating copy to clipboard operation
responses copied to clipboard

Support fuller files kwarg for requests.post

Open semperos opened this issue 1 year ago • 7 comments

Thanks for taking the time to review this! I ran into this type mismatch with some code that leverages the requests library and was hoping to update responses to match.


The files keyword argument for the requests.post function has this type:

files: _Files | None = ...

This commit adds this type alias as found in requests version 2.32.2 and updates this library's multipart_matcher function's files keyword argument to be of type _Files.

semperos avatar Dec 12 '24 14:12 semperos

should we just import it from requests then ?

beliaev-maksim avatar Dec 13 '24 08:12 beliaev-maksim

should we just import it from requests then ?

:+1: to importing the types if we can.

markstory avatar Dec 13 '24 14:12 markstory

I neglected to run tox before pushing that last commit, there are errors; I'll fix them before pushing again, no need to approve the workflows.

semperos avatar Dec 13 '24 14:12 semperos

I believe Python's import prevents importing names with a leading underscore; I get an ImportError: cannot import name '_Files' from 'requests.sessions' when I attempt to from requests.sessions import _Files.

Two paths forward that I see:

  1. Ask the requests maintainers to expose this type.
  2. Recreate the types here.

The original PR workflow run showed that the syntax I ported from requests of tuple[_FileName, _FileContent] breaks in Python 3.8. If we go with option 2, I can adjust the type syntax to make the code compatible with Python 3.8.

semperos avatar Dec 13 '24 15:12 semperos

can you please send a permalink to the piece of code at requests where they define the type?

beliaev-maksim avatar Dec 13 '24 15:12 beliaev-maksim

Apologies for not double-checking the source of the types, I had jumped to them in my editor and didn't realize they weren't provided by the requests implementation itself. The types I included in my first commit were from typeshed.

Here is the permalink to where _Files is used: https://github.com/python/typeshed/blob/7cae61f61a042a772010274dd1d8f1ee4454e5a4/stubs/requests/requests/sessions.pyi#L148

Here is the permalink to the definition of _Files itself: https://github.com/python/typeshed/blob/7cae61f61a042a772010274dd1d8f1ee4454e5a4/stubs/requests/requests/sessions.pyi#L92

semperos avatar Dec 16 '24 15:12 semperos

so, if we add a dependency and import the type, will it work?

beliaev-maksim avatar Dec 16 '24 15:12 beliaev-maksim