Skip to content

Commit e1fd594

Browse files
authored
Add support of pathlib.Path to edit (#3781)
2 parents 2103e15 + a1d8785 commit e1fd594

5 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎CHANGES.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Unreleased
8989
bare `less`. {pr}`3777`
9090
- Improve raw mode detection by parsing the option tokens. {issue}`3416`
9191
{pr}`3777`
92+
- {func}`edit` accepts `os.PathLike` values for `filename`, in addition to
93+
strings. {issue}`2869` {pr}`3781`
9294

9395
## Version 8.4.2
9496

‎src/click/_termui_impl.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ def get_editor(self) -> str:
710710
return editor
711711
return "vi"
712712

713-
def edit_files(self, filenames: cabc.Iterable[str]) -> None:
713+
def edit_files(self, filenames: cabc.Iterable[str | os.PathLike[str]]) -> None:
714714
"""Open files in the user's editor."""
715715
import shlex
716716
import subprocess

‎src/click/termui.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import inspect
55
import io
66
import itertools
7+
import os
78
import re
89
import sys
910
import typing as t
@@ -837,7 +838,10 @@ def edit(
837838
env: cabc.Mapping[str, str] | None = None,
838839
require_save: bool = True,
839840
extension: str = ".txt",
840-
filename: str | cabc.Iterable[str] | None = None,
841+
filename: str
842+
| os.PathLike[str]
843+
| cabc.Iterable[str | os.PathLike[str]]
844+
| None = None,
841845
) -> None: ...
842846

843847

@@ -847,7 +851,10 @@ def edit(
847851
env: cabc.Mapping[str, str] | None = None,
848852
require_save: bool = True,
849853
extension: str = ".txt",
850-
filename: str | cabc.Iterable[str] | None = None,
854+
filename: str
855+
| os.PathLike[str]
856+
| cabc.Iterable[str | os.PathLike[str]]
857+
| None = None,
851858
) -> str | bytes | bytearray | None:
852859
r"""Edits the given text in the defined editor. If an editor is given
853860
(should be the full path to the executable but the regular operating
@@ -874,16 +881,20 @@ def edit(
874881
highlighting.
875882
:param filename: if provided it will edit this file instead of the
876883
provided text contents. It will not use a temporary
877-
file as an indirection in that case. If the editor supports
878-
editing multiple files at once, a sequence of files may be
879-
passed as well. Invoke `click.file` once per file instead
880-
if multiple files cannot be managed at once or editing the
881-
files serially is desired.
884+
file as an indirection in that case. It accepts a path
885+
or any iterable of paths. If the editor supports
886+
editing multiple files at once, a sequence of files may
887+
be passed as well. Invoke `click.file` once per file
888+
instead if multiple files cannot be managed at once or
889+
editing the files serially is desired.
882890
883891
.. versionchanged:: 8.2.0
884892
``filename`` now accepts any ``Iterable[str]`` in addition to a ``str``
885893
if the ``editor`` supports editing multiple files at once.
886894
895+
.. versionchanged:: 8.5.0
896+
``filename`` accepts ``os.PathLike`` values in addition to strings.
897+
887898
"""
888899
from ._termui_impl import Editor
889900

@@ -892,7 +903,7 @@ def edit(
892903
if filename is None:
893904
return ed.edit(text)
894905

895-
if isinstance(filename, str):
906+
if isinstance(filename, (str, os.PathLike)):
896907
filename = (filename,)
897908

898909
ed.edit_files(filenames=filename)

‎tests/test_termui.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,13 @@ def test_getchar_windows_exceptions(runner, monkeypatch, key_char, exc):
421421
click.getchar()
422422

423423

424-
@pytest.mark.skipif(platform.system() == "Windows", reason="No sed on Windows.")
424+
@pytest.mark.skipif(WIN, reason="No sed on Windows.")
425425
def test_fast_edit(runner):
426426
result = click.edit("a\nb", editor="sed -i~ 's/$/Test/'")
427427
assert result == "aTest\nbTest\n"
428428

429429

430-
@pytest.mark.skipif(platform.system() == "Windows", reason="No sed on Windows.")
430+
@pytest.mark.skipif(WIN, reason="No sed on Windows.")
431431
def test_edit(runner):
432432
with tempfile.NamedTemporaryFile(mode="w") as named_tempfile:
433433
named_tempfile.write("a\nb\n")
@@ -448,6 +448,19 @@ def test_edit(runner):
448448
assert reopened_file.read() == "aTest\nbTest\n"
449449

450450

451+
@pytest.mark.skipif(WIN, reason="No sed on Windows.")
452+
@pytest.mark.parametrize("use_iterable", [False, True], ids=["single", "iterable"])
453+
def test_edit_pathlib(runner, tmp_path, use_iterable):
454+
"""Issue #2869: ``filename`` accepts ``os.PathLike`` values."""
455+
file_path = tmp_path / "file.txt"
456+
file_path.write_text("a\nb\n", encoding="UTF-8")
457+
filename = [file_path] if use_iterable else file_path
458+
459+
result = click.edit(filename=filename, editor="sed -i~ 's/$/Test/'")
460+
assert result is None
461+
assert file_path.read_text(encoding="UTF-8") == "aTest\nbTest\n"
462+
463+
451464
@pytest.mark.parametrize(
452465
("editor_cmd", "filenames", "expected_args"),
453466
[

‎tests/typing/typing_edit.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pathlib import Path
2+
3+
from typing_extensions import assert_type
4+
5+
import click
6+
7+
# ``filename`` accepts a path or an iterable of paths.
8+
assert_type(click.edit(filename="f.txt"), None)
9+
assert_type(click.edit(filename=Path("f.txt")), None)
10+
assert_type(click.edit(filename=["f.txt", Path("g.txt")]), None)

0 commit comments

Comments
 (0)