Correct click.edit typing (#2804)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andreas Backx · committed by GitHub · parent b5464b7 · Browse files · 3 files·+26 added−7 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+26 added−7 removed
- other:+26 added−7 removed
Provenance
- Commit
4ffa1ef- Tree
aade6f3- Author
Andreas Backx <AndreasBackx@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b5464b7
3 files changed+26 added−7 removed
modified:src/click/_termui_impl.py+11 added-3 removed
543 unmodified lines544 raise ClickException(545 _("{editor}: Editing failed: {e}").format(editor=editor, e=e)546 ) from e547 548- def edit(self, text: t.AnyStr | None) -> t.AnyStr | None:548+ @t.overload549+ def edit(self, text: bytes | bytearray) -> bytes | None: ...550+551+ # We cannot know whether or not the type expected is str or bytes when None552+ # is passed, so str is returned as that was what was done before.553+ @t.overload554+ def edit(self, text: str | None) -> str | None: ...555+556+ def edit(self, text: str | bytes | bytearray | None) -> str | bytes | None:557 import tempfile558 551- if not text:559+ if text is None:560 data = b""561 elif isinstance(text, (bytes, bytearray)):562 data = text563 else:31 unmodified lines595 596 if isinstance(text, (bytes, bytearray)):597 return rv598 591- return rv.decode("utf-8-sig").replace("\r\n", "\n") # type: ignore599+ return rv.decode("utf-8-sig").replace("\r\n", "\n")600 finally:601 os.unlink(name)602 603 167 unmodified linesmodified:src/click/termui.py+14 added-4 removed
684 unmodified lines685 686 687 @t.overload688 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.overload698+def edit(699+ text: str,700+ editor: str | None = None,701+ env: cabc.Mapping[str, str] | None = None,702 require_save: bool = True,703 extension: str = ".txt",694-) -> t.AnyStr: ...704+) -> str | None: ...705 706 707 @t.overload708 def edit(6 unmodified lines715 ) -> None: ...716 717 718 def edit(709- text: t.AnyStr | None = None,719+ text: str | bytes | bytearray | None = None,720 editor: str | None = None,721 env: cabc.Mapping[str, str] | None = None,722 require_save: bool = True,723 extension: str = ".txt",724 filename: str | cabc.Iterable[str] | None = None,715-) -> t.AnyStr | None:725+) -> str | bytes | bytearray | None:726 r"""Edits the given text in the defined editor. If an editor is given727 (should be the full path to the executable but the regular operating728 system search path is used for finding the executable) it overrides729 the detected editor. Optionally, some environment variables can be148 unmodified linesmodified:CHANGES.rst+1 added
92 unmodified lines93 the value from :class:`CliRunner`. :issue:`2817` :pr:`2818`94 - ``Option.flag_value`` will no longer have a default value set based on95 ``Option.default`` if ``Option.is_flag`` is ``False``. This results in96 ``Option.default`` not needing to implement `__bool__`. :pr:`2829`97+- Incorrect ``click.edit`` typing has been corrected. :pr:`2804`98 99 Version 8.1.8100 -------------101 1117 unmodified lines