Skip to content

Commit 50eec50

Browse files
gh-57141: Make shallow argument to filecmp.dircmp keyword-only (#121767)
It is our general practice to make new optional parameters keyword-only, even if the existing parameters are all positional-or-keyword. Passing this parameter as positional would look confusing and could be error-prone if additional parameters are added in the future.
1 parent 7982363 commit 50eec50

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

Doc/library/filecmp.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The :mod:`filecmp` module defines the following functions:
7070
The :class:`dircmp` class
7171
-------------------------
7272

73-
.. class:: dircmp(a, b, ignore=None, hide=None, shallow=True)
73+
.. class:: dircmp(a, b, ignore=None, hide=None, *, shallow=True)
7474

7575
Construct a new directory comparison object, to compare the directories *a*
7676
and *b*. *ignore* is a list of names to ignore, and defaults to

Lib/filecmp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _do_cmp(f1, f2):
8888
class dircmp:
8989
"""A class that manages the comparison of 2 directories.
9090
91-
dircmp(a, b, ignore=None, hide=None, shallow=True)
91+
dircmp(a, b, ignore=None, hide=None, *, shallow=True)
9292
A and B are directories.
9393
IGNORE is a list of names to ignore,
9494
defaults to DEFAULT_IGNORES.
@@ -124,7 +124,7 @@ class dircmp:
124124
in common_dirs.
125125
"""
126126

127-
def __init__(self, a, b, ignore=None, hide=None, shallow=True): # Initialize
127+
def __init__(self, a, b, ignore=None, hide=None, *, shallow=True): # Initialize
128128
self.left = a
129129
self.right = b
130130
if hide is None:
@@ -201,7 +201,7 @@ def phase4(self): # Find out differences between common subdirectories
201201
a_x = os.path.join(self.left, x)
202202
b_x = os.path.join(self.right, x)
203203
self.subdirs[x] = self.__class__(a_x, b_x, self.ignore, self.hide,
204-
self.shallow)
204+
shallow=self.shallow)
205205

206206
def phase4_closure(self): # Recursively call phase4() on subdirectories
207207
self.phase4()

Lib/test/test_filecmp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import filecmp
22
import os
3+
import re
34
import shutil
45
import tempfile
56
import unittest
@@ -277,6 +278,17 @@ def test_dircmp_shallow_same_file(self):
277278
]
278279
self._assert_report(d.report, expected_report)
279280

281+
def test_dircmp_shallow_is_keyword_only(self):
282+
with self.assertRaisesRegex(
283+
TypeError,
284+
re.escape("dircmp.__init__() takes from 3 to 5 positional arguments but 6 were given"),
285+
):
286+
filecmp.dircmp(self.dir, self.dir_same, None, None, True)
287+
self.assertIsInstance(
288+
filecmp.dircmp(self.dir, self.dir_same, None, None, shallow=True),
289+
filecmp.dircmp,
290+
)
291+
280292
def test_dircmp_subdirs_type(self):
281293
"""Check that dircmp.subdirs respects subclassing."""
282294
class MyDirCmp(filecmp.dircmp):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The *shallow* argument to :class:`filecmp.dircmp` (new in Python 3.13) is
2+
now keyword-only.

0 commit comments

Comments
 (0)