Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Looser type annotations for send_file() path_or_file argument #5776

Closed
Milestone
@tvuotila

Description

@tvuotila

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp