← 输入框|github.com|刷新
Skip to content

[tkinter] filedialog ask* functions can return an empty tuple when cancelled - #16642

Open
hoangchunghien wants to merge 1 commit into
python:mainfrom
hoangchunghien:tkinter-filedialog-cancel
Open

hoangchunghien wants to merge 1 commit into
python:mainfrom
hoangchunghien:tkinter-filedialog-cancel

Conversation

@hoangchunghien

Copy link
Copy Markdown
Contributor

Fixes #16637

askopenfilename, asksaveasfilename and askdirectory were typed as returning str, but when the dialog is cancelled they can return an empty tuple. Which one you get depends on the Tk version. I cancelled each function under Xvfb on Linux:

function Tk 8.6.15 (Python 3.13.9) Tk 9.0.3 (Python 3.14.4)
askopenfilename () ()
asksaveasfilename () ()
askdirectory '' ()
askopenfilenames () ()
askopenfile, asksaveasfile None None
askopenfiles () ()

So the three str functions become str | tuple[()], as suggested in the issue. The others already match: askopenfilenames is Literal[""] | tuple[str, ...], the file-object ones return None or an empty tuple as typed. I did not check Windows or macOS, where Tk uses native dialogs.

Added assert_type cases to check_tkinter.py. regr_test.py stdlib (3.13, 3.10, and win32), mypy_test.py stdlib/tkinter, pyright_test.py, black and flake8 pass locally.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return types in tkinter.filedialog

1 participant