Skip to content

requests: allow passing a list of tuples for files #7998

Closed

Description

@tommilligan

Relates to #7724

The files argument in the requests library is incompletely typed. The current types include the Mapping type (over which there has been considerable discourse already), but the official requests documentation states:

You can send multiple files in one request. [...]
To do that, just set files to a list of tuples of (form_field_name, file_info):

The official example code given is:

url = 'https://httpbin.org/post'
multiple_files = [
    ('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
    ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
r = requests.post(url, files=multiple_files)
r.text

This snippet currently fails typechecking with the latest stubs. A minimal reproduction is available here, which also confirms the code works as expected, but fails with.

minimal.py:15: error: Argument "files" to "post" has incompatible type "List[Tuple[str, Tuple[str, BufferedReader, str]]]"; expected "Optional[Union[Mapping[str, Union[SupportsRead[Union[str, bytes]], str, bytes]], Mapping[str, Tuple[Optional[str], Union[SupportsRead[Union[str, bytes]], str, bytes]]], Mapping[str, Tuple[Optional[str], Union[SupportsRead[Union[str, bytes]], str, bytes], str]], Mapping[str, Tuple[Optional[str], Union[SupportsRead[Union[str, bytes]], str, bytes], str, MutableMapping[str, str]]]]]"
Found 1 error in 1 file (checked 1 source file)

Activity

  1. tommilligan commented on May 31, 2022

    @tommilligan
    ContributorAuthor

    A suggested fix for this issue has been opened at #7999

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions