typing_path.py
This file
Path
tests/typing/typing_path.py
34 lines · 1.4 KiB · Python
Last changed by
Merge stable into main (#3876)Rowlando13before adoption3cbaa76
History of this fileTake it
1import os2import pathlib34from typing_extensions import assert_type56import click78# Without ``path_type``, ``convert`` returns the value it was given.9assert_type(click.Path().convert("a/b.txt", None, None), str | bytes | os.PathLike[str])10assert_type(click.prompt("File", type=click.Path()), str | bytes | os.PathLike[str])1112# ``path_type`` narrows the converted value's type for ``convert`` and ``prompt``.13assert_type(click.Path(path_type=str).convert("a/b.txt", None, None), str)14assert_type(click.Path(path_type=bytes).convert("a/b.txt", None, None), bytes)15assert_type(16 click.Path(path_type=pathlib.Path).convert("a/b.txt", None, None), pathlib.Path17)18assert_type(click.prompt("File", type=click.Path(path_type=str)), str)19assert_type(click.prompt("File", type=click.Path(path_type=pathlib.Path)), pathlib.Path)2021assert_type(click.Path(path_type=pathlib.Path)("a/b.txt"), pathlib.Path)22assert_type(click.Path(path_type=pathlib.Path)(None), None)2324# The class can be parameterized explicitly.25param_type: click.Path[pathlib.Path] = click.Path(exists=True, path_type=pathlib.Path)26assert_type(param_type.convert("a/b.txt", None, None), pathlib.Path)27assert_type(param_type.coerce_path_result("a/b.txt"), pathlib.Path)2829general: click.ParamType[str | bytes | os.PathLike[str]] = click.Path()30specific: click.ParamType[pathlib.Path] = click.Path(path_type=pathlib.Path)3132assert_type(33 click.Path(path_type=pathlib.Path)(pathlib.PurePosixPath("a/b.txt")), pathlib.Path34)