Skip to content

urllib.request.Request data parameter doesn't accept BinaryIO type object #7208

@LeoChen2

Description

@LeoChen2

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions