From a891704c0504662a967cdfc1908b92ce9bec6203 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 6 Jun 2021 12:57:57 +0300 Subject: [PATCH 1/3] add types to tkinter.filedialog.ask* --- stdlib/tkinter/filedialog.pyi | 82 +++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/stdlib/tkinter/filedialog.pyi b/stdlib/tkinter/filedialog.pyi index 86238f0be798..c2ed6b06cc60 100644 --- a/stdlib/tkinter/filedialog.pyi +++ b/stdlib/tkinter/filedialog.pyi @@ -1,4 +1,5 @@ -from tkinter import Button, Entry, Frame, Listbox, Scrollbar, Toplevel, commondialog +from _typeshed import StrOrBytesPath +from tkinter import Button, Entry, Frame, Listbox, Misc, Scrollbar, StringVar, Toplevel, commondialog from typing import Any, ClassVar, Dict, Optional, Tuple dialogstates: Dict[Any, Tuple[Any, Any]] @@ -57,11 +58,76 @@ class SaveAs(_Dialog): class Directory(commondialog.Dialog): command: ClassVar[str] = ... -def askopenfilename(**options): ... -def asksaveasfilename(**options): ... -def askopenfilenames(**options): ... -def askopenfile(mode: str = ..., **options): ... -def askopenfiles(mode: str = ..., **options): ... -def asksaveasfile(mode: str = ..., **options): ... -def askdirectory(**options): ... +# TODO: command kwarg available on macos +def asksaveasfilename( + *, + confirmoverwrite: bool | None = ..., + defaultextension: str | None = ..., + filetypes: Iterable[Tuple[str, str] | Tuple[str, _TkinterSequence[str]]] | None = ..., + initialdir: StrOrBytesPath | None = ..., + initialfile: StrOrBytesPath | None = ..., + parent: Misc | None = ..., + title: str | None = ..., + typevariable: StringVar | str | None = ..., +) -> str: ... # can be empty string +def askopenfilename( + *, + defaultextension: str | None = ..., + filetypes: Iterable[Tuple[str, str] | Tuple[str, _TkinterSequence[str]]] | None = ..., + initialdir: StrOrBytesPath | None = ..., + initialfile: StrOrBytesPath | None = ..., + parent: Misc | None = ..., + title: str | None = ..., + typevariable: StringVar | str | None = ..., +) -> str: ... # can be empty string +def askopenfilenames( + *, + defaultextension: str | None = ..., + filetypes: Iterable[Tuple[str, str] | Tuple[str, _TkinterSequence[str]]] | None = ..., + initialdir: StrOrBytesPath | None = ..., + initialfile: StrOrBytesPath | None = ..., + parent: Misc | None = ..., + title: str | None = ..., + typevariable: StringVar | str | None = ..., +) -> Literal[""] | Tuple[str, ...]: ... +def askdirectory( + *, initialdir: StrOrBytesPath | None = ..., mustexist: bool | None = ..., parent: Misc | None = ..., title: str | None = ... +) -> str: ... # can be empty string + +# TODO: If someone actually uses these, overload to have the actual return type of open(..., mode) +def asksaveasfile( + mode: str = ..., + *, + confirmoverwrite: bool | None = ..., + defaultextension: str | None = ..., + filetypes: Iterable[Tuple[str, str] | Tuple[str, _TkinterSequence[str]]] | None = ..., + initialdir: StrOrBytesPath | None = ..., + initialfile: StrOrBytesPath | None = ..., + parent: Misc | None = ..., + title: str | None = ..., + typevariable: StringVar | str | None = ..., +) -> IO[Any] | None: ... +def askopenfile( + mode: str = ..., + *, + defaultextension: str | None = ..., + filetypes: Iterable[Tuple[str, str] | Tuple[str, _TkinterSequence[str]]] | None = ..., + initialdir: StrOrBytesPath | None = ..., + initialfile: StrOrBytesPath | None = ..., + parent: Misc | None = ..., + title: str | None = ..., + typevariable: StringVar | str | None = ..., +) -> IO[Any] | None: ... +def askopenfiles( + mode: str = ..., + *, + defaultextension: str | None = ..., + filetypes: Iterable[Tuple[str, str] | Tuple[str, _TkinterSequence[str]]] | None = ..., + initialdir: StrOrBytesPath | None = ..., + initialfile: StrOrBytesPath | None = ..., + parent: Misc | None = ..., + title: str | None = ..., + typevariable: StringVar | str | None = ..., +) -> Tuple[IO[Any], ...]: ... # can be empty tuple + def test() -> None: ... From 2eb25137b5a68b1565fdd8133ea73d8452c6d09d Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 6 Jun 2021 13:03:43 +0300 Subject: [PATCH 2/3] fix imports --- stdlib/tkinter/filedialog.pyi | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/tkinter/filedialog.pyi b/stdlib/tkinter/filedialog.pyi index c2ed6b06cc60..936ca2a90b52 100644 --- a/stdlib/tkinter/filedialog.pyi +++ b/stdlib/tkinter/filedialog.pyi @@ -1,6 +1,7 @@ from _typeshed import StrOrBytesPath -from tkinter import Button, Entry, Frame, Listbox, Misc, Scrollbar, StringVar, Toplevel, commondialog -from typing import Any, ClassVar, Dict, Optional, Tuple +from tkinter import Button, Entry, Frame, Listbox, Misc, Scrollbar, StringVar, Toplevel, _TkinterSequence, commondialog +from typing import Any, ClassVar, Dict, Iterable, Optional, Tuple +from typing_extensions import Literal dialogstates: Dict[Any, Tuple[Any, Any]] From fc5d25c8507b1d884d15278742c2f9806640e256 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 6 Jun 2021 13:07:03 +0300 Subject: [PATCH 3/3] actually fixening --- stdlib/tkinter/filedialog.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/tkinter/filedialog.pyi b/stdlib/tkinter/filedialog.pyi index 936ca2a90b52..885a1373ea93 100644 --- a/stdlib/tkinter/filedialog.pyi +++ b/stdlib/tkinter/filedialog.pyi @@ -1,6 +1,6 @@ from _typeshed import StrOrBytesPath from tkinter import Button, Entry, Frame, Listbox, Misc, Scrollbar, StringVar, Toplevel, _TkinterSequence, commondialog -from typing import Any, ClassVar, Dict, Iterable, Optional, Tuple +from typing import IO, Any, ClassVar, Dict, Iterable, Optional, Tuple from typing_extensions import Literal dialogstates: Dict[Any, Tuple[Any, Any]] @@ -130,5 +130,4 @@ def askopenfiles( title: str | None = ..., typevariable: StringVar | str | None = ..., ) -> Tuple[IO[Any], ...]: ... # can be empty tuple - def test() -> None: ...