-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 files={"example": ("filename", bytes)} #7724
Comments
I see a similar issue for the example from https://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file: import requests
url = "https://httpbin.org/post"
files = {"file": open("report.xls", "rb")}
r = requests.post(url, files=files)
This issue reproduces with |
I'm also seeing this issue when utilizing
|
Thanks all — a new version of types-requests should be uploaded to PyPI shortly. Feel free to leave a comment here or open a new issue if there are still problems with the new release :) |
I have tried 2.27.23 and while the error message has changed, it is still there:
This is with the code example from #7724 (comment) |
I think this issue should be reopened. The problem here I believe is the usage of Edit: sorry, didn't see the new issue opened, which properly describes the problem. |
Okay, #7732 has just been merged, and a new release will be uploaded shortly — please let us know if there are still problems with the new release! |
2.27.24 works fine for me, thanks for the quick fix! |
I am not really sure if this bug should be closed: Using This incorrect(?) variance leads to quite a few false positives on our SW, and I guess we're not alone with this... |
@svenpanne |
It's mainly because we pass As a temporary measure, we just copy the |
This would also make me a bit hesitant to change Another thing worth considering is that to keep things internally consistent, you'd have to make
I can imagine that more people use |
I don't think that this is a balancing act, regarding typing things are always crystal clear: If a parameter gets mutated by a function/method it must be a request's documentation is a bit crappy here IMHO, and the API is more than surprising: Even though it talks about dictionaries, I would have never expected that those input parameters are mutated! Looking at your Regarding;
It is far from clear that the In any case: It's already very helpful to know that |
That is what is happening internally. So making headers/cookies a The problem is these are stubs for a third party. Typeshed can decide to make the parameters as permissive as the current implementation allows, but we don't know what the authors actually intended. Following documentation would be safer, but also leads to false positives. So that's where the balacing lies. However it's also possible that requests' authors didn't intend the input argument to |
When "balancing", we usually consider false negatives (no errors for wrong code) less bad than false positives (errors for correct code).
In general,
|
If people are still getting what they believe to be false-positive errors following #7696, it would be helpful if they could post a minimal repro and the exact error message they're getting 🙂 otherwise this is all purely theoretical |
I am not exactly sure who "we" is, but type checkers for other programming languages actually do the exact opposite thing and issue an error if code can not be proven correct. In other words, they err on the conservative side, because having the guarantee that things can't go wrong at runtime if the type checker is silent is far more important than any "inconvenience" caused by this conservatism. Due to Rice's theorem (all non-trivial, semantic properties of programs are undecidable) there will always be programs which don't go wrong, but can't be proven safe by any type checker. This is not as bad as it actually sounds, there are basically always ways to make your intentions clear to a type checker by rewriting things slightly. If you do not err on the conservative side, type checkers are relatively useless.
Huh? Perhaps I'm misunderstanding things, but this doesn't sound fine at all: If you pass an immutable data structure into some class, which remembers the same data structure internally and makes the very same data structure available as a property, and then claim that the data structure returned is mutable, something has gone seriously wrong. In other words: In such a scenario as above (argument remembered internally and exposed as a property, no copying/cloning involved), then the argument and the property must have the same type. From a variance POV: The class is a consumer and a producer, so we have invariance. Coming back to our This will cause some type errors, e.g. in our own project, but this is a good thing: We weren't aware of the fact that the argument can be mutated, so we have to fix things on our side! This is what type checking is all about... Regarding |
As mentioned above, the situation regarding Regading |
Typeshed maintainers. This is documented in our CONTRIBUTING.md.
This isn't how type checking works in Python. It wouldn't even be possible, because you can always do something very dynamic, e.g.
Indeed. If a mapping is mutated, it should be a We all want errors for code that is most likely wrong, such as passing in an immutable mapping for code that will mutate it. The question is whether we want errors for code that might be correct or wrong. I'm saying that it's sometimes fine to not error in such cases, but I don't mean that we should just apply that blindly. It really has to be considered case by case.
This was my first reaction too. But some classes are used in two kinds of ways:
If you want both of these to "just work", without forcing users to |
For the record,
I opened psf/requests#6118 for requests. I think there is a good chance that they never intended this. |
Looks like it won't be changed any time soon, see psf/requests#6118 . From the response I also gather that they really do expect a mutable argument here. |
Even though the issue is still open, the workaround is apparently not needed anymore. python/typeshed#7724 Change-Id: Ia5ead6c7ac3a667655b8eb70860920d37ecd9423
PR was merged, so this can be closed now, thanks @milanboers :) |
According to https://github.com/psf/requests/blob/2d5517682b3b38547634d153cea43d48fbc8cdb5/requests/models.py#L170 It should be possible to provide
files={"fieldname": ("filename", [bytes-object])}
, but since the last update of mypy this fails withThe issue seems to be in
typeshed/stubs/requests/requests/sessions.pyi
Lines 51 to 56 in 63fb9af
Can this be fixed?
The text was updated successfully, but these errors were encountered: