Skip to content

Commit 4ffa1ef

Browse files
authored
Correct click.edit typing (pallets#2804)
1 parent b5464b7 commit 4ffa1ef

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Unreleased
9494
- ``Option.flag_value`` will no longer have a default value set based on
9595
``Option.default`` if ``Option.is_flag`` is ``False``. This results in
9696
``Option.default`` not needing to implement `__bool__`. :pr:`2829`
97+
- Incorrect ``click.edit`` typing has been corrected. :pr:`2804`
9798

9899
Version 8.1.8
99100
-------------

src/click/_termui_impl.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,18 @@ def edit_files(self, filenames: cabc.Iterable[str]) -> None:
545545
_("{editor}: Editing failed: {e}").format(editor=editor, e=e)
546546
) from e
547547

548-
def edit(self, text: t.AnyStr | None) -> t.AnyStr | None:
548+
@t.overload
549+
def edit(self, text: bytes | bytearray) -> bytes | None: ...
550+
551+
# We cannot know whether or not the type expected is str or bytes when None
552+
# is passed, so str is returned as that was what was done before.
553+
@t.overload
554+
def edit(self, text: str | None) -> str | None: ...
555+
556+
def edit(self, text: str | bytes | bytearray | None) -> str | bytes | None:
549557
import tempfile
550558

551-
if not text:
559+
if text is None:
552560
data = b""
553561
elif isinstance(text, (bytes, bytearray)):
554562
data = text
@@ -588,7 +596,7 @@ def edit(self, text: t.AnyStr | None) -> t.AnyStr | None:
588596
if isinstance(text, (bytes, bytearray)):
589597
return rv
590598

591-
return rv.decode("utf-8-sig").replace("\r\n", "\n") # type: ignore
599+
return rv.decode("utf-8-sig").replace("\r\n", "\n")
592600
finally:
593601
os.unlink(name)
594602

src/click/termui.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,22 @@ def secho(
686686

687687
@t.overload
688688
def edit(
689-
text: t.AnyStr,
689+
text: bytes | bytearray,
690+
editor: str | None = None,
691+
env: cabc.Mapping[str, str] | None = None,
692+
require_save: bool = False,
693+
extension: str = ".txt",
694+
) -> bytes | None: ...
695+
696+
697+
@t.overload
698+
def edit(
699+
text: str,
690700
editor: str | None = None,
691701
env: cabc.Mapping[str, str] | None = None,
692702
require_save: bool = True,
693703
extension: str = ".txt",
694-
) -> t.AnyStr: ...
704+
) -> str | None: ...
695705

696706

697707
@t.overload
@@ -706,13 +716,13 @@ def edit(
706716

707717

708718
def edit(
709-
text: t.AnyStr | None = None,
719+
text: str | bytes | bytearray | None = None,
710720
editor: str | None = None,
711721
env: cabc.Mapping[str, str] | None = None,
712722
require_save: bool = True,
713723
extension: str = ".txt",
714724
filename: str | cabc.Iterable[str] | None = None,
715-
) -> t.AnyStr | None:
725+
) -> str | bytes | bytearray | None:
716726
r"""Edits the given text in the defined editor. If an editor is given
717727
(should be the full path to the executable but the regular operating
718728
system search path is used for finding the executable) it overrides

0 commit comments

Comments
 (0)