Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/deprecations/pending-removal-in-3.18.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ Pending removal in Python 3.18
C implementation, has been deprecated since Python 3.13.
(Contributed by Serhiy Storchaka in :gh:`89902`.)

* :mod:`tkinter`:

* :func:`tkinter.filedialog.askopenfiles` has been deprecated since Python
3.16. Iterate over the names returned by
:func:`~tkinter.filedialog.askopenfilenames` and open them one by one
instead.
(Contributed by Serhiy Storchaka in :gh:`152638`.)

* Deprecations defined by :pep:`829`:

* ``import`` lines in :file:`{name}.pth` files are silently ignored.
Expand Down
20 changes: 14 additions & 6 deletions Doc/library/dialog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,23 @@ versions, so test the result for truth rather than comparing it with a
specific value.

.. function:: askopenfile(mode="r", **options)
askopenfiles(mode="r", **options)

Create an :class:`Open` dialog.
:func:`askopenfile` returns the opened file object, or ``None`` if the
dialog is cancelled.
:func:`askopenfiles` returns a list of the opened file objects, or an empty
tuple if cancelled.
Create an :class:`Open` dialog and return the opened file object, or
``None`` if the dialog is cancelled.
The file is opened in mode *mode* (read-only ``'r'`` by default).

.. function:: askopenfiles(mode="r", **options)

Create an :class:`Open` dialog and return a list of the opened file
objects, or an empty tuple if cancelled.
The files are opened in mode *mode* (read-only ``'r'`` by default).

.. deprecated-removed:: next 3.18
Opening several files at once is error-prone, and the returned list
cannot be used in a :keyword:`with` statement.
Iterate over the names returned by :func:`askopenfilenames` and open
them one by one instead.

.. function:: asksaveasfile(mode="w", **options)

Create a :class:`SaveAs` dialog and return the opened file object, or
Expand Down
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,15 @@ New deprecations
3.9, now issues a deprecation warning on use. This property is slated for
removal in 3.21. Use ``ast.Tuple.elts`` instead.

* :mod:`tkinter`:

* :func:`tkinter.filedialog.askopenfiles` is deprecated and slated for
removal in Python 3.18. Opening several files at once is error-prone, and
the returned list cannot be used in a :keyword:`with` statement. Iterate
over the names returned by :func:`~tkinter.filedialog.askopenfilenames` and
open them one by one instead.
(Contributed by Serhiy Storchaka in :gh:`152638`.)

.. Add deprecations above alphabetically, not here at the end.
.. include:: ../deprecations/pending-removal-in-3.17.rst
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_tkinter/test_filedialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,13 @@ def test_subclasses(self):
self.assertEqual(d.top.title(), cls.title)


class DeprecationTest(unittest.TestCase):

def test_askopenfiles_deprecated(self):
with swap_attr(filedialog, 'askopenfilenames', lambda **kw: ()):
with self.assertWarns(DeprecationWarning):
filedialog.askopenfiles()


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions Lib/tkinter/filedialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import fnmatch
import os
import warnings
from tkinter import (
Frame, LEFT, YES, BOTTOM, Entry, TOP, Button, Tk, X,
Toplevel, RIGHT, Y, END, Listbox, BOTH, Scrollbar,
Expand Down Expand Up @@ -418,6 +419,11 @@ def askopenfiles(mode = "r", **options):
returns a list of open file objects or an empty list if
cancel selected
"""
warnings._deprecated(
"tkinter.filedialog.askopenfiles",
message=f"{warnings._DEPRECATED_MSG}; iterate over the names returned "
"by askopenfilenames() and open them instead",
remove=(3, 18))

files = askopenfilenames(**options)
if files:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deprecate :func:`tkinter.filedialog.askopenfiles`. Opening several files at
once is error-prone and the returned list cannot be used in a :keyword:`with`
statement; iterate over the names returned by
:func:`tkinter.filedialog.askopenfilenames` and open them one by one instead.