Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tommilligan opened this issue May 31, 2022 · 1 comment · Fixed by #7999
Closed

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

tommilligan opened this issue May 31, 2022 · 1 comment · Fixed by #7999

Comments

@tommilligan
Copy link
Contributor

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)
@tommilligan
Copy link
Contributor Author

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

AlexWaygood added a commit that referenced this issue Sep 8, 2022
- Move the logic for running mypy on the test cases from `tests/mypy_test.py` to a separate script, `tests/regr_test.py`.
- Add the necessary logic in order to be able to have test cases for third-party stubs.
- Move logic common to `tests/mypy_test.py` and `tests/regr_test.py` into `tests/colors.py`, and rename `tests/colors.py` to `tests/utils.py`.
- Add a new check to `tests/check_consistent.py`, to enforce the use of `# pyright: reportUnnecessaryTypeIgnoreComment=true` comments in third-party test cases. These are essential if we want to have our tests against false-negatives work with pyright.
- Update the relevant documentation to account for the new test file.
- Add a new job to the `tests.yml` GitHub workflow, to run the new test in CI.
- Add a simple proof-of-concept test case for `requests`, as a regression test for #7998.

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Sebastian Rittau <[email protected]>
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 a pull request may close this issue.

1 participant