You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
urllib.request.Request.data type erroring when using decode #11470
When getting the data out of a urllib.request.Request object, mypy is erroring saying that decode is not supported even tho it is a bytes object (and does not error when at runtime).
$ cat t.pyfrom urllib.request import Requestreq = Request("http://localhost:8080", data=b"req body")if req.data: print(dir(req.data)) print(type(req.data)) print(req.data.decode())
$ mypy t.pyt.py:8: error: Item "Buffer" of "Buffer | SupportsRead[bytes] | Iterable[bytes]" has no attribute "decode" [union-attr]t.py:8: error: Item "SupportsRead[bytes]" of "Buffer | SupportsRead[bytes] | Iterable[bytes]" has no attribute "decode" [union-attr]t.py:8: error: Item "Iterable[bytes]" of "Buffer | SupportsRead[bytes] | Iterable[bytes]" has no attribute "decode" [union-attr]Found 3 errors in 1 file (checked 1 source file)
$ python t.py[... 'decode', ...]<class 'bytes'>req body
$ python --version --versionPython 3.12.0 (main, Oct 2 2023, 20:56:14) [Clang 16.0.3 ]
$ mypy --versionmypy 1.8.0 (compiled: yes)
I believe PR #10225 had made this an error as it changed bytes to Buffer.
changed the title [-]urllib.request.Request.data type erroring when using decode[/-][+]`urllib.request.Request.data` type erroring when using `decode`[/+]on Feb 27, 2024
The problem here is that the type of the data attribute depends on the data passed in, which might not have a decode() method. The solution is to make Request generic over the type of data, but we'll have to wait until #11422 is implemented. I've marked this issue as "deferred" for now.
Activity
[-]urllib.request.Request.data type erroring when using decode[/-][+]`urllib.request.Request.data` type erroring when using `decode`[/+]aminalaee commentedon Feb 28, 2024
Isn't it because the
dataproperty can also beIterable[bytes]so.decode()is not available?I think that's not your case, but it's a possible case.
srittau commentedon Mar 15, 2024
The problem here is that the type of the
dataattribute depends on thedatapassed in, which might not have adecode()method. The solution is to makeRequestgeneric over the type of data, but we'll have to wait until #11422 is implemented. I've marked this issue as "deferred" for now.[urllib.request] Make `Request` generic over the type of `data`