-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
I am trying to pass a file-like object to urllib.request.Request data parameter. See code snippet:
def upload_binary(contents: BinaryIO):
request = urllib.request.Request(url, data=contents, method="PUT", headers=headers)
The file like object is annotated as BinaryIO in my code. Both Pycharm and mypy type checking expects the data parameter type to be Optional[bytes]
and rejects BinaryIO type. Mypy error: Argument "data" to "Request" has incompatible type "BinaryIO"; expected "Optional[bytes]"
and Pycharm error: Expected type 'Optional[bytes]', got 'BinaryIO' instead
.
Passing the object annotated as BinaryIO to data parameter actually works when I run the code. This makes me think if urllib.request.Request data parameter should accept BinaryIO? Or should I use other type annotation for file like object so that mypy and Pycharm won't complain about this issue? Any advice?