Skip to content

urllib.request.Request.data type erroring when using decode #11470

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

Open
arkinmodi opened this issue Feb 27, 2024 · 2 comments
Open

urllib.request.Request.data type erroring when using decode #11470

arkinmodi opened this issue Feb 27, 2024 · 2 comments
Labels
status: deferred Issue or PR deferred until some precondition is fixed stubs: false positive Type checkers report false errors

Comments

@arkinmodi
Copy link

arkinmodi commented Feb 27, 2024

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.py
from urllib.request import Request

req = 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.py
t.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 --version
Python 3.12.0 (main, Oct  2 2023, 20:56:14) [Clang 16.0.3 ]

$ mypy --version
mypy 1.8.0 (compiled: yes)

I believe PR #10225 had made this an error as it changed bytes to Buffer.

- ReadOnlyBuffer: TypeAlias = bytes
- ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer  # stable
+ ReadOnlyBuffer: TypeAlias = Buffer  # stable
+ ReadableBuffer: TypeAlias = Buffer  # stable

Before: https://github.com/python/typeshed/blame/56aeeb677f27c6771e683314621521aff5c97cd1/stdlib/_typeshed/__init__.pyi#L230-L241
After: https://github.com/python/typeshed/blame/1e7e174b4fc4da6a66622168f8012c7ee0499392/stdlib/_typeshed/__init__.pyi#L263-L267


Deferred until #11422.

@arkinmodi arkinmodi changed the title urllib.request.Request.data type erroring when using decode urllib.request.Request.data type erroring when using decode Feb 27, 2024
@aminalaee
Copy link
Contributor

Isn't it because the data property can also be Iterable[bytes] so .decode() is not available?
I think that's not your case, but it's a possible case.

@srittau srittau added stubs: false positive Type checkers report false errors status: deferred Issue or PR deferred until some precondition is fixed labels Mar 15, 2024
@srittau
Copy link
Collaborator

srittau commented Mar 15, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: deferred Issue or PR deferred until some precondition is fixed stubs: false positive Type checkers report false errors
Projects
None yet
Development

No branches or pull requests

3 participants