Skip to content

Commit

Permalink
Correct click.edit typing (#2804)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBackx authored Dec 22, 2024
1 parent b5464b7 commit 4ffa1ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Unreleased
- ``Option.flag_value`` will no longer have a default value set based on
``Option.default`` if ``Option.is_flag`` is ``False``. This results in
``Option.default`` not needing to implement `__bool__`. :pr:`2829`
- Incorrect ``click.edit`` typing has been corrected. :pr:`2804`

Version 8.1.8
-------------
Expand Down
14 changes: 11 additions & 3 deletions src/click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,18 @@ def edit_files(self, filenames: cabc.Iterable[str]) -> None:
_("{editor}: Editing failed: {e}").format(editor=editor, e=e)
) from e

def edit(self, text: t.AnyStr | None) -> t.AnyStr | None:
@t.overload
def edit(self, text: bytes | bytearray) -> bytes | None: ...

# We cannot know whether or not the type expected is str or bytes when None
# is passed, so str is returned as that was what was done before.
@t.overload
def edit(self, text: str | None) -> str | None: ...

def edit(self, text: str | bytes | bytearray | None) -> str | bytes | None:
import tempfile

if not text:
if text is None:
data = b""
elif isinstance(text, (bytes, bytearray)):
data = text
Expand Down Expand Up @@ -588,7 +596,7 @@ def edit(self, text: t.AnyStr | None) -> t.AnyStr | None:
if isinstance(text, (bytes, bytearray)):
return rv

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

Expand Down
18 changes: 14 additions & 4 deletions src/click/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,22 @@ def secho(

@t.overload
def edit(
text: t.AnyStr,
text: bytes | bytearray,
editor: str | None = None,
env: cabc.Mapping[str, str] | None = None,
require_save: bool = False,
extension: str = ".txt",
) -> bytes | None: ...


@t.overload
def edit(
text: str,
editor: str | None = None,
env: cabc.Mapping[str, str] | None = None,
require_save: bool = True,
extension: str = ".txt",
) -> t.AnyStr: ...
) -> str | None: ...


@t.overload
Expand All @@ -706,13 +716,13 @@ def edit(


def edit(
text: t.AnyStr | None = None,
text: str | bytes | bytearray | None = None,
editor: str | None = None,
env: cabc.Mapping[str, str] | None = None,
require_save: bool = True,
extension: str = ".txt",
filename: str | cabc.Iterable[str] | None = None,
) -> t.AnyStr | None:
) -> str | bytes | bytearray | None:
r"""Edits the given text in the defined editor. If an editor is given
(should be the full path to the executable but the regular operating
system search path is used for finding the executable) it overrides
Expand Down

0 comments on commit 4ffa1ef

Please sign in to comment.