Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
benblank opened this issue May 3, 2024 · 1 comment
Closed
Milestone

Comments

@benblank
Copy link

benblank commented May 3, 2024

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
@danwuSBU
Copy link

working on this at PyCon sprint

@davidism davidism added this to the 8.1.8 milestone May 22, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants