Skip to content

Commit f650143

Browse files
committed
relax matcher argument type to callable
1 parent 0a78769 commit f650143

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

Lib/difflib.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,9 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6, matcher=None):
806806
Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
807807
that don't score at least that similar to word are ignored.
808808
809-
Optional arg matcher is a subclass of SequenceMatcherBase.
810-
Default (if None) is SequenceMatcher.
809+
Optional arg matcher is a callable that takes 3 positional arguments.
810+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
811+
Default (if None) is SequenceMatcher class.
811812
812813
The best (no more than n) matches among the possibilities are returned
813814
in a list, sorted by similarity score, most similar first.
@@ -829,9 +830,8 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6, matcher=None):
829830
raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
830831
if matcher is None:
831832
matcher = SequenceMatcher
832-
elif not issubclass(matcher, SequenceMatcherBase):
833-
msg = "matcher must be a subclass of SequenceMatcherBase: %r"
834-
raise TypeError(msg % (matcher,))
833+
elif not callable(matcher):
834+
raise TypeError("matcher must be callable: %r" % (matcher,))
835835
result = []
836836
s = matcher()
837837
s.set_seq2(word)
@@ -968,22 +968,22 @@ def __init__(self, linejunk=None, charjunk=None,
968968
whitespace characters (a blank or tab; **note**: bad idea to include
969969
newline in this!). Use of IS_CHARACTER_JUNK is recommended.
970970
971-
- `linematcher`: Subclass of SequenceMatcherBase. If None, defaults
972-
to SequenceMatcher.
971+
- `linematcher`: callable that takes 3 positional arguments.
972+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
973+
Default (if None) is SequenceMatcher class.
973974
974-
- `charmatcher`: Subclass of SequenceMatcherBase. If None, defaults
975-
to SequenceMatcher.
975+
- `charmatcher`: callable that takes 3 positional arguments.
976+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
977+
Default (if None) is SequenceMatcher class.
976978
"""
977979
if linematcher is None:
978980
linematcher = SequenceMatcher
979-
elif not issubclass(linematcher, SequenceMatcherBase):
980-
msg = "linematcher must be a subclass of SequenceMatcherBase: %r"
981-
raise TypeError(msg % (linematcher,))
981+
elif not callable(linematcher):
982+
raise TypeError("linematcher must be callable: %r" % (linematcher,))
982983
if charmatcher is None:
983984
charmatcher = SequenceMatcher
984-
elif not issubclass(charmatcher, SequenceMatcherBase):
985-
msg = "charmatcher must be a subclass of SequenceMatcherBase: %r"
986-
raise TypeError(msg % (charmatcher,))
985+
elif not callable(charmatcher):
986+
raise TypeError("charmatcher must be callable: %r" % (charmatcher,))
987987
self.linejunk = linejunk
988988
self.charjunk = charjunk
989989
self.linematcher = linematcher
@@ -1273,8 +1273,9 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
12731273
'git diff --color'. Even if enabled, it can be
12741274
controlled using environment variables such as 'NO_COLOR'.
12751275
1276-
Optional arg matcher is a subclass of SequenceMatcherBase.
1277-
Default (if None) is SequenceMatcher.
1276+
Optional arg matcher is a callable that takes 3 positional arguments.
1277+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
1278+
Default (if None) is SequenceMatcher class.
12781279
12791280
The unidiff format normally has a header for filenames and modification
12801281
times. Any or all of these may be specified using strings for
@@ -1300,9 +1301,8 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
13001301
"""
13011302
if matcher is None:
13021303
matcher = SequenceMatcher
1303-
elif not issubclass(matcher, SequenceMatcherBase):
1304-
msg = "matcher must be a subclass of SequenceMatcherBase: %r"
1305-
raise TypeError(msg % (matcher,))
1304+
elif not callable(matcher):
1305+
raise TypeError("matcher must be callable: %r" % (matcher,))
13061306

13071307
if color and can_colorize():
13081308
t = get_theme(force_color=True).difflib
@@ -1371,8 +1371,9 @@ def context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='',
13711371
For inputs that do not have trailing newlines, set the lineterm
13721372
argument to "" so that the output will be uniformly newline free.
13731373
1374-
Optional arg matcher is a subclass of SequenceMatcherBase.
1375-
Default (if None) is SequenceMatcher.
1374+
Optional arg matcher is a callable that takes 3 positional arguments.
1375+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
1376+
Default (if None) is SequenceMatcher class.
13761377
13771378
The context diff format normally has a header for filenames and
13781379
modification times. Any or all of these may be specified using
@@ -1401,9 +1402,8 @@ def context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='',
14011402
"""
14021403
if matcher is None:
14031404
matcher = SequenceMatcher
1404-
elif not issubclass(matcher, SequenceMatcherBase):
1405-
msg = "matcher must be a subclass of SequenceMatcherBase: %r"
1406-
raise TypeError(msg % (matcher,))
1405+
elif not callable(matcher):
1406+
raise TypeError("matcher must be callable: %r" % (matcher,))
14071407

14081408
_check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
14091409
prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ')
@@ -1509,11 +1509,13 @@ def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK,
15091509
whitespace characters (a blank or tab; note: it's a bad idea to
15101510
include newline in this!).
15111511
1512-
- `linematcher`: Subclass of SequenceMatcherBase. If None, defaults
1513-
to SequenceMatcher.
1512+
- `linematcher`: callable that takes 3 positional arguments.
1513+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
1514+
Default (if None) is SequenceMatcher class.
15141515
1515-
- `charmatcher`: Subclass of SequenceMatcherBase. If None, defaults
1516-
to SequenceMatcher.
1516+
- `charmatcher`: callable that takes 3 positional arguments.
1517+
i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
1518+
Default (if None) is SequenceMatcher class.
15171519
15181520
Tools/scripts/ndiff.py is a command-line front-end to this function.
15191521

0 commit comments

Comments
 (0)