path type supports pathlib
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 1472e3d · Browse files · 3 files·+43 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+43 added−18 removed
- tests:+22 added−0 removed
- other:+21 added−18 removed
Provenance
- Commit
6d64ca5- Tree
5365974- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1472e3d
3 files changed+43 added−18 removed
modified:src/click/types.py+19 added-18 removed
647 unmodified lines648 different checks. First of all, instead of returning an open file649 handle it returns just the filename. Secondly, it can perform various650 basic checks about what the file or directory should be.651 652- .. versionchanged:: 6.0653- `allow_dash` was added.654-652 :param exists: if set to true, the file or directory needs to exist for653 this value to be valid. If this is not required and a654 file does indeed not exist, then all further checks are655 silently skipped.7 unmodified lines663 will not expand a tilde-prefix, as this is664 supposed to be done by the shell only.665 :param allow_dash: If this is set to `True`, a single dash to indicate666 standard streams is permitted.670- :param path_type: optionally a string type that should be used to671- represent the path. The default is `None` which672- means the return value will be either bytes or673- unicode depending on what makes most sense given the674- input data Click deals with.667+ :param path_type: Convert the incoming path value to this type. If668+ ``None``, keep Python's default, which is ``str``. Useful to669+ convert to :class:`pathlib.Path`.670+671+ .. versionchanged:: 8.0672+ Allow passing ``type=pathlib.Path``.673+674+ .. versionchanged:: 6.0675+ Added the ``allow_dash`` parameter.676 """677 678 envvar_list_splitter = os.path.pathsep679 18 unmodified lines698 self.type = path_type699 700 if self.file_okay and not self.dir_okay:701 self.name = "file"701- self.path_type = "File"702 elif self.dir_okay and not self.file_okay:703 self.name = "directory"704- self.path_type = "Directory"704 else:705 self.name = "path"707- self.path_type = "Path"706 707 def to_info_dict(self):708 info_dict = super().to_info_dict()709 info_dict.update(8 unmodified lines718 719 def coerce_path_result(self, rv):720 if self.type is not None and not isinstance(rv, self.type):721 if self.type is str:724- rv = rv.decode(get_filesystem_encoding())722+ rv = os.fsdecode(rv)723+ elif self.type is bytes:724+ rv = os.fsencode(rv)725 else:726- rv = rv.encode(get_filesystem_encoding())726+ rv = self.type(rv)727+728 return rv729 730 def convert(self, value, param, ctx):731 rv = value9 unmodified lines741 except OSError:742 if not self.exists:743 return self.coerce_path_result(rv)744 self.fail(744- f"{self.path_type} {filename_to_ui(value)!r} does not exist.",745+ f"{self.name.title()} {filename_to_ui(value)!r} does not exist.",746 param,747 ctx,748 )749 750 if not self.file_okay and stat.S_ISREG(st.st_mode):751 self.fail(751- f"{self.path_type} {filename_to_ui(value)!r} is a file.",752+ f"{self.name.title()} {filename_to_ui(value)!r} is a file.",753 param,754 ctx,755 )756 if not self.dir_okay and stat.S_ISDIR(st.st_mode):757 self.fail(757- f"{self.path_type} {filename_to_ui(value)!r} is a directory.",758+ f"{self.name.title()} {filename_to_ui(value)!r} is a directory.",759 param,760 ctx,761 )762 if self.writable and not os.access(value, os.W_OK):763 self.fail(763- f"{self.path_type} {filename_to_ui(value)!r} is not writable.",764+ f"{self.name.title()} {filename_to_ui(value)!r} is not writable.",765 param,766 ctx,767 )768 if self.readable and not os.access(value, os.R_OK):769 self.fail(769- f"{self.path_type} {filename_to_ui(value)!r} is not readable.",770+ f"{self.name.title()} {filename_to_ui(value)!r} is not readable.",771 param,772 ctx,773 )774 152 unmodified linesmodified:tests/test_types.py+22 added
1+import pathlib2+3 import pytest4 5 import click6 72 unmodified lines79 cli = click.Command("cli", params=[param], callback=lambda a: a)80 result = runner.invoke(cli, standalone_mode=False)81 assert result.exception is None82 assert result.return_value == expect83+84+85+@pytest.mark.parametrize(86+ ("cls", "expect"),87+ [88+ (None, "a/b/c.txt"),89+ (str, "a/b/c.txt"),90+ (bytes, b"a/b/c.txt"),91+ (pathlib.Path, pathlib.Path("a", "b", "c.txt")),92+ ],93+)94+def test_path_type(runner, cls, expect):95+ cli = click.Command(96+ "cli",97+ params=[click.Argument(["p"], type=click.Path(path_type=cls))],98+ callback=lambda p: p,99+ )100+ result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False)101+ assert result.exception is None102+ assert result.return_value == expectmodified:CHANGES.rst+2 added
178 unmodified lines179 - Progress bar output is shown even if execution time is less than 0.5180 seconds. :issue:`1648`181 - Progress bar ``item_show_func`` shows the current item, not the182 previous item. :issue:`1353`183+- The ``Path`` param type can be passed ``path_type=pathlib.Path`` to184+ return a path object instead of a string. :issue:`405`185 186 187 Version 7.1.2188 -------------635 unmodified lines