Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork16.7k
Description
path_or_file argument offlask.helpers.send_file is typed asos.PathLike[t.AnyStr] | str | t.BinaryIO. This prevents passing some objects that aret.IO[bytes], but nott.BinaryIO. The underlyingwerkzeug.utils.send_file already allowst.IO[bytes] due topallets/werkzeug#2209 since version 2.0.2.
Reproduction:
fromtempfileimportNamedTemporaryFilefromtypingimportIOfromflask.helpersimportsend_file# The return type cannot be "BinaryIO" because "NamedTemporaryFile" is incompatible with it according to Mypy.defsome_function()->IO[bytes]:file=NamedTemporaryFile() ...returnfilefile=some_function()send_file(file)
Raises the following exception with Mypy 1.16.1.
error: Argument 1 to "send_file" has incompatible type "IO[bytes]"; expected "PathLike[str] | str | BinaryIO" [arg-type]I could simply change the return value ofsome_function to_TemporaryFileWrapper[bytes] or cast it toBinaryIO.
However, I would like to allowt.IO[bytes].
Side note:_TemporaryFileWrapper[bytes] conforms toPathLike[str] due topython/typeshed#7840. That is why it is accepted byflask.helpers.send_file.
Allowingt.IO[bytes] would be backwards compatible change as allt.BinaryIO are alsot.IO[bytes].
Mypy Playground
Environment:
- Python version: 3.12.11
- Flask version: 3.1.1