Skip to content

The type hint for the filename parameter to open_file is incomplete #2717

@benblank

Description

@benblank

click.open_file(filename, …) happily accepts a pathlib.Path object as the filename; it only passes the value along to either click.utils.LazyFile(filename, …) or click._compat.open_stream(filename, …), both of which accept pathlikes. However, open_file's type hints indicate that filename can only be a string, which causes type-checking errors when feeding it a Path.

from pathlib import Path

import click

filename = Path("/some/path")

# Mypy gives the following error on the line with the call to open_file:
#
# error: Argument 1 to "open_file" has incompatible type "Path"; expected "str"  [arg-type]

with click.open_file(filename) as handle:
    pass

Both LazyFile and open_stream give their filename parameter the type types.Union[str, os.PathLike[str]], so that should probably be the type accepted by open_file, too.

Environment:

  • Python version: 3.12.1
  • Click version: 8.1.7

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