You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
shutil.copytree - 3.8 changed argument types of the ignore callback #83571
In Python 3.8, the types of the parameters to the ignore callable appear to have changed.
Previously the src parameter was a string and the names parameter was a list of strings. Now the src parameter appears to be either a pathlib.Path or an os.DirEntry, while the names parameter is a set of strings.
.. versionchanged:: 3.8
The types of arguments to *ignore* have changed. The first argument
(the directory being visited) is a func:`os.DirEntry` or a
func:`pathlib.Path`; Previously it was a string. The second argument is
a set of strings; previously it was a list of strings.
changed the title [-]shutil.copytree - ignore callback behaviour change[/-][+]shutil.copytree - 3.8 changed argument types of the ignore callback[/+]on Jan 19, 2020
changed the title [-]shutil.copytree - ignore callback behaviour change[/-][+]shutil.copytree - 3.8 changed argument types of the ignore callback[/+]on Jan 19, 2020
Yes, thanks. Whoever got bit by this is either getting an exception or not the intended behavior (due to failed string comparison). I doubt anybody is relying on the new type checking since it's not documented. If they are, they are probably just doing:
For completeness, a similar problem is present also on python < 3.8 if passing a pathlib.Path type as *src*: the callback function will receive a pathlib.Path type once, and then string types.
mbarkhau commented on Jan 24, 2020
mbarkhaumannequin
MannequinAuthor
More actions
For completeness, a similar problem is present also on python < 3.8
For completeness, a similar problem is present also on python < 3.8 if passing a pathlib.Path type as *src*
I do not think this is a problem. If you pass a string, you will get a string, so existing code will continue to work as before. The only problem if you pass a string, but get an unexpected type.
I don't think we need to change anything on < 3.8, but 3.8 and 3.9 will always convert *src* to str via os.fspath(), which IMO is more consistent (e.g. os.path.* functions and others do the same).
mbarkhau commented on Jan 24, 2020
mbarkhaumannequin
MannequinAuthor
More actions
If you pass a string, you will get a string, so existing code will continue to work as before.
Somebody might have code that is running against a flat directory and have written their ignore function expecting to get a pathlib.Path, because that's the only case they encountered. This change would break their code and so would an upgrade to 3.9 with the patch that was just merged.
Activity
mbarkhau commentedon Jan 19, 2020
In Python 3.8, the types of the parameters to the ignore callable appear to have changed.
Previously the
srcparameter was a string and thenamesparameter was a list of strings. Now thesrcparameter appears to be either apathlib.Pathor anos.DirEntry, while thenamesparameter is a set of strings.I would suggest adding the following to the documentation https://github.com/python/cpython/blob/master/Doc/library/shutil.rst
.. versionchanged:: 3.8
The types of arguments to *ignore* have changed. The first argument
(the directory being visited) is a func:`os.DirEntry` or a
func:`pathlib.Path`; Previously it was a string. The second argument is
a set of strings; previously it was a list of strings.
[-]shutil.copytree - ignore callback behaviour change[/-][+]shutil.copytree - 3.8 changed argument types of the ignore callback[/+][-]shutil.copytree - ignore callback behaviour change[/-][+]shutil.copytree - 3.8 changed argument types of the ignore callback[/+]5 remaining items
giampaolo commentedon Jan 21, 2020
Yes, thanks. Whoever got bit by this is either getting an exception or not the intended behavior (due to failed string comparison). I doubt anybody is relying on the new type checking since it's not documented. If they are, they are probably just doing:
giampaolo commentedon Jan 24, 2020
New changeset 8870433 by Giampaolo Rodola (mbarkhau) in branch 'master':
bpo-39390 shutil: fix argument types for ignore callback (GH-18122)
8870433
giampaolo commentedon Jan 24, 2020
For completeness, a similar problem is present also on python < 3.8 if passing a pathlib.Path type as *src*: the callback function will receive a pathlib.Path type once, and then string types.
mbarkhau commentedon Jan 24, 2020
Fair point. I'll have a look.
serhiy-storchaka commentedon Jan 24, 2020
I do not think this is a problem. If you pass a string, you will get a string, so existing code will continue to work as before. The only problem if you pass a string, but get an unexpected type.
giampaolo commentedon Jan 24, 2020
I don't think we need to change anything on < 3.8, but 3.8 and 3.9 will always convert *src* to str via os.fspath(), which IMO is more consistent (e.g. os.path.* functions and others do the same).
mbarkhau commentedon Jan 24, 2020
Somebody might have code that is running against a flat directory and have written their ignore function expecting to get a pathlib.Path, because that's the only case they encountered. This change would break their code and so would an upgrade to 3.9 with the patch that was just merged.
giampaolo commentedon Jan 27, 2020
New changeset cf9d005 by Giampaolo Rodola (mbarkhau) in branch '3.8':
[3.8] bpo-39390 shutil: fix argument types for ignore callback (GH-18122)
cf9d005