Skip to content

Commit b9204a4

Browse files
committed
gh-56593: Document the difflib Match named tuple
The docs and docstrings described the results of SequenceMatcher's find_longest_match() and get_matching_blocks() as plain (i, j, k) triples even though they are Match named tuples, and Match itself was not documented. Add a description of Match to the docs, state the return type explicitly in both methods, and clarify that the diff functions accept sequences of strings rather than lists.
1 parent ee68f5f commit b9204a4

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

Doc/library/difflib.rst

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Diff generation
195195
.. method:: make_file(fromlines, tolines, fromdesc='', todesc='', context=False, \
196196
numlines=5, *, charset='utf-8')
197197
198-
Compares *fromlines* and *tolines* (lists of strings) and returns a string which
198+
Compares *fromlines* and *tolines* (sequences of strings) and returns a string which
199199
is a complete HTML file containing a table showing line by line differences with
200200
inter-line and intra-line changes highlighted.
201201

@@ -222,7 +222,7 @@ Diff generation
222222

223223
.. method:: make_table(fromlines, tolines, fromdesc='', todesc='', context=False, numlines=5)
224224

225-
Compares *fromlines* and *tolines* (lists of strings) and returns a string which
225+
Compares *fromlines* and *tolines* (sequences of strings) and returns a string which
226226
is a complete HTML table showing line by line differences with inter-line and
227227
intra-line changes highlighted.
228228

@@ -233,7 +233,7 @@ Diff generation
233233

234234
.. function:: context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')
235235

236-
Compare *a* and *b* (lists of strings); return a delta (a :term:`generator`
236+
Compare *a* and *b* (sequences of strings); return a delta (a :term:`generator`
237237
generating the delta lines) in context diff format.
238238

239239
Context diffs are a compact way of showing just the lines that have changed plus
@@ -306,7 +306,7 @@ Diff generation
306306

307307
.. function:: ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK)
308308

309-
Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style
309+
Compare *a* and *b* (sequences of strings); return a :class:`Differ`\ -style
310310
delta (a :term:`generator` generating the delta lines).
311311

312312
Optional keyword parameters *linejunk* and *charjunk* are filtering functions
@@ -364,7 +364,7 @@ Diff generation
364364

365365
.. function:: unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', *, color=False)
366366

367-
Compare *a* and *b* (lists of strings); return a delta (a :term:`generator`
367+
Compare *a* and *b* (sequences of strings); return a delta (a :term:`generator`
368368
generating the delta lines) in unified diff format.
369369

370370
Unified diffs are a compact way of showing just the lines that have changed plus
@@ -449,6 +449,32 @@ Junk definition functions
449449

450450
.. _sequence-matcher:
451451

452+
.. class:: Match
453+
454+
The type of the objects returned by :meth:`SequenceMatcher.find_longest_match`
455+
and :meth:`SequenceMatcher.get_matching_blocks`. It is an object with a
456+
:term:`named tuple` interface: values can be accessed by index and by
457+
attribute name. It has the following fields:
458+
459+
.. list-table::
460+
461+
* - Index
462+
- Attribute
463+
- Value
464+
465+
* - 0
466+
- .. attribute:: a
467+
- The index in the first sequence of the matching block.
468+
469+
* - 1
470+
- .. attribute:: b
471+
- The index in the second sequence of the matching block.
472+
473+
* - 2
474+
- .. attribute:: size
475+
- The number of elements in the matching block.
476+
477+
452478
SequenceMatcher objects
453479
-----------------------
454480

@@ -513,7 +539,8 @@ SequenceMatcher objects
513539
Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``.
514540

515541
If *isjunk* was omitted or ``None``, :meth:`find_longest_match` returns
516-
``(i, j, k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo
542+
a :class:`Match` named tuple ``(i, j, k)`` such that ``a[i:i+k]`` is
543+
equal to ``b[j:j+k]``, where ``alo
517544
<= i <= i+k <= ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j',
518545
k')`` meeting those conditions, the additional conditions ``k >= k'``, ``i
519546
<= i'``, and if ``i == i'``, ``j <= j'`` are also met. In other words, of
@@ -541,18 +568,16 @@ SequenceMatcher objects
541568
>>> s.find_longest_match(0, 5, 0, 9)
542569
Match(a=1, b=0, size=4)
543570

544-
If no blocks match, this returns ``(alo, blo, 0)``.
545-
546-
This method returns a :term:`named tuple` ``Match(a, b, size)``.
571+
If no blocks match, this returns ``Match(alo, blo, 0)``.
547572

548573
.. versionchanged:: 3.9
549574
Added default arguments.
550575

551576

552577
.. method:: get_matching_blocks()
553578

554-
Return list of triples describing non-overlapping matching subsequences.
555-
Each triple is of the form ``(i, j, n)``,
579+
Return list of :class:`Match` triples describing non-overlapping matching
580+
subsequences. Each triple is of the form ``(i, j, n)``,
556581
and means that ``a[i:i+n] == b[j:j+n]``. The
557582
triples are monotonically increasing in *i* and *j*.
558583

Lib/difflib.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
Use SequenceMatcher to return list of the best "good enough" matches.
66
77
Function context_diff(a, b):
8-
For two lists of strings, return a delta in context diff format.
8+
For two sequences of strings, return a delta in context diff format.
99
1010
Function ndiff(a, b):
11-
Return a delta: the difference between `a` and `b` (lists of strings).
11+
Return a delta: the difference between `a` and `b` (sequences of strings).
1212
1313
Function restore(delta, which):
1414
Return one of the two sequences that generated an ndiff delta.
1515
1616
Function unified_diff(a, b):
17-
For two lists of strings, return a delta in unified diff format.
17+
For two sequences of strings, return a delta in unified diff format.
1818
1919
Class SequenceMatcher:
2020
A flexible class for comparing pairs of sequences of any type.
@@ -310,7 +310,8 @@ def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None):
310310
311311
If isjunk is not defined:
312312
313-
Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
313+
Return a Match named tuple (i, j, k) such that a[i:i+k] is equal to
314+
b[j:j+k], where
314315
alo <= i <= i+k <= ahi
315316
blo <= j <= j+k <= bhi
316317
and for all (i',j',k') meeting those conditions,
@@ -342,7 +343,7 @@ def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None):
342343
>>> s.find_longest_match(0, 5, 0, 9)
343344
Match(a=1, b=0, size=4)
344345
345-
If no blocks match, return (alo, blo, 0).
346+
If no blocks match, return Match(alo, blo, 0).
346347
347348
>>> s = SequenceMatcher(None, "ab", "c")
348349
>>> s.find_longest_match(0, 2, 0, 1)
@@ -420,7 +421,7 @@ def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None):
420421
return Match(besti, bestj, bestsize)
421422

422423
def get_matching_blocks(self):
423-
"""Return list of triples describing matching subsequences.
424+
"""Return list of Match triples describing matching subsequences.
424425
425426
Each triple is of the form (i, j, n), and means that
426427
a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in
@@ -1323,7 +1324,7 @@ def decode(s):
13231324

13241325
def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK):
13251326
r"""
1326-
Compare `a` and `b` (lists of strings); return a `Differ`-style delta.
1327+
Compare `a` and `b` (sequences of strings); return a `Differ`-style delta.
13271328
13281329
Optional keyword parameters `linejunk` and `charjunk` are for filter
13291330
functions, or can be None:

0 commit comments

Comments
 (0)