-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-118150: difflib: expose autojunk flag from SequenceMatcher to public methods and functions #153959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-118150: difflib: expose autojunk flag from SequenceMatcher to public methods and functions #153959
Changes from all commits
367dc55
9a822c4
5e2f622
7d86473
b5213c9
7d7a35b
bd98298
af14a4f
e3d69d8
9dc95bd
e4cd72a
3586616
1197f62
1843999
3da543a
56204ad
d117379
61e64a2
5e091f1
c38edf3
35b45f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,14 @@ Comparisons are done using a matching algorithm implemented in | |||||||
| of any type, not just text, so long as the sequence elements are | ||||||||
| :term:`hashable`. | ||||||||
|
|
||||||||
| .. versionchanged:: 3.16 | ||||||||
| Exposed *autojunk* parameter of :class:`SequenceMatcher` in public functions | ||||||||
| and classes of this module (:class:`Differ`, :class:`HtmlDiff`, :func:`ndiff`, | ||||||||
| :func:`unified_diff`, :func:`context_diff`). For backward compatibility | ||||||||
| this parameter is set everywhere to be ``True`` by default. | ||||||||
|
|
||||||||
| See :gh:`118150` for motivation and reasons. | ||||||||
|
|
||||||||
|
|
||||||||
| .. _difflib-junk: | ||||||||
|
|
||||||||
|
|
@@ -161,6 +169,8 @@ Diff generation | |||||||
| printed as-is via the :meth:`~io.IOBase.writelines` method of a | ||||||||
| file-like object. | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| .. class:: HtmlDiff | ||||||||
|
|
||||||||
| This class can be used to create an HTML table (or a complete HTML file | ||||||||
|
|
@@ -176,7 +186,7 @@ Diff generation | |||||||
| The constructor for this class is: | ||||||||
|
|
||||||||
|
|
||||||||
| .. method:: __init__(tabsize=8, wrapcolumn=None, linejunk=None, charjunk=IS_CHARACTER_JUNK) | ||||||||
| .. method:: __init__(tabsize=8, wrapcolumn=None, linejunk=None, charjunk=IS_CHARACTER_JUNK, *, autojunk=True): | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| Initializes instance of :class:`HtmlDiff`. | ||||||||
|
|
||||||||
|
|
@@ -187,8 +197,13 @@ Diff generation | |||||||
| broken and wrapped, defaults to ``None`` where lines are not wrapped. | ||||||||
|
|
||||||||
| *linejunk* and *charjunk* are optional keyword arguments passed into :func:`ndiff` | ||||||||
| (used by :class:`HtmlDiff` to generate the side by side HTML differences). See | ||||||||
| :func:`ndiff` documentation for argument default values and descriptions. | ||||||||
| (used by :class:`HtmlDiff` to generate the side by side HTML differences). | ||||||||
| See :func:`ndiff` documentation for argument default values and descriptions. | ||||||||
|
|
||||||||
| .. versionchanged:: 3.16 | ||||||||
| Added keyword-only *autojunk* parameter. | ||||||||
|
|
||||||||
| *autojunk* flag is for setting on/off automatic junk heuristic of :class:`SequenceMatcher`. | ||||||||
|
|
||||||||
| The following methods are public: | ||||||||
|
|
||||||||
|
|
@@ -231,7 +246,7 @@ Diff generation | |||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| .. function:: context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n') | ||||||||
| .. function:: context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', *, autojunk=True) | ||||||||
|
|
||||||||
| Compare *a* and *b* (lists of strings); return a delta (a :term:`generator` | ||||||||
| generating the delta lines) in context diff format. | ||||||||
|
|
@@ -249,6 +264,11 @@ Diff generation | |||||||
| For inputs that do not have trailing newlines, set the *lineterm* argument to | ||||||||
| ``""`` so that the output will be uniformly newline free. | ||||||||
|
|
||||||||
| .. versionchanged:: 3.16 | ||||||||
| Added keyword-only *autojunk* parameter. | ||||||||
|
Comment on lines
+267
to
+268
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move |
||||||||
|
|
||||||||
| Optional *autojunk* flag sets on/off automatic junk heuristic of :class:`SequenceMatcher`. | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's now a section for the heuristic in the description; it would be good to link there:
Suggested change
Please use the same wording for all of these (unless there's a difference that needs to be pointed out). |
||||||||
|
|
||||||||
| The context diff format normally has a header for filenames and modification | ||||||||
| times. Any or all of these may be specified using strings for *fromfile*, | ||||||||
| *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally | ||||||||
|
|
@@ -278,7 +298,7 @@ Diff generation | |||||||
| See :ref:`difflib-interface` for a more detailed example. | ||||||||
|
|
||||||||
|
|
||||||||
| .. function:: get_close_matches(word, possibilities, n=3, cutoff=0.6) | ||||||||
| .. function:: get_close_matches(word, possibilities, n=3, cutoff=0.6, *, autojunk=True) | ||||||||
|
|
||||||||
| Return a list of the best "good enough" matches. *word* is a sequence for which | ||||||||
| close matches are desired (typically a string), and *possibilities* is a list of | ||||||||
|
|
@@ -290,6 +310,12 @@ Diff generation | |||||||
| Optional argument *cutoff* (default ``0.6``) is a float in the range [0, 1]. | ||||||||
| Possibilities that don't score at least that similar to *word* are ignored. | ||||||||
|
|
||||||||
| .. versionchanged:: 3.16 | ||||||||
| Added keyword-only *autojunk* parameter. | ||||||||
|
|
||||||||
| Optional *autojunk* param is a flag for turning on/off | ||||||||
| an automatic junk heuristic of :class:`SequenceMatcher`. | ||||||||
|
|
||||||||
| The best (no more than *n*) matches among the possibilities are returned in a | ||||||||
| list, sorted by similarity score, most similar first. | ||||||||
|
|
||||||||
|
|
@@ -304,7 +330,7 @@ Diff generation | |||||||
| ['except'] | ||||||||
|
|
||||||||
|
|
||||||||
| .. function:: ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK) | ||||||||
| .. function:: ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK, *, autojunk=True) | ||||||||
|
|
||||||||
| Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style | ||||||||
| delta (a :term:`generator` generating the delta lines). | ||||||||
|
|
@@ -325,6 +351,14 @@ Diff generation | |||||||
| function :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a | ||||||||
| blank or tab; it's a bad idea to include newline in this!). | ||||||||
|
|
||||||||
| .. versionchanged:: 3.16 | ||||||||
| Added keyword-only *autojunk* parameter. | ||||||||
|
|
||||||||
| *autojunk*: An optional parameter for setting on/off automatic junk heuristic | ||||||||
| of :class:`SequenceMatcher`. | ||||||||
|
|
||||||||
| Example: | ||||||||
|
|
||||||||
| >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True), | ||||||||
| ... 'ore\ntree\nemu\n'.splitlines(keepends=True)) | ||||||||
| >>> print(''.join(diff), end="") | ||||||||
|
|
@@ -362,7 +396,7 @@ Diff generation | |||||||
| emu | ||||||||
|
|
||||||||
|
|
||||||||
| .. function:: unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', *, color=False) | ||||||||
| .. function:: unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', *, autojunk=True, color=False) | ||||||||
|
|
||||||||
| Compare *a* and *b* (lists of strings); return a delta (a :term:`generator` | ||||||||
| generating the delta lines) in unified diff format. | ||||||||
|
|
@@ -410,6 +444,12 @@ Diff generation | |||||||
| .. versionchanged:: 3.15 | ||||||||
| Added the *color* parameter. | ||||||||
|
|
||||||||
| .. versionchanged:: 3.16 | ||||||||
| Added keyword-only *autojunk* parameter. | ||||||||
|
|
||||||||
| Set *autojunk* to ``False`` in order to disable automatic junk heuristic | ||||||||
| of underlying :class:`SequenceMatcher`. | ||||||||
|
|
||||||||
|
|
||||||||
| .. function:: diff_bytes(dfunc, a, b, fromfile=b'', tofile=b'', fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n') | ||||||||
|
|
||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is covered in the individual function/class docs; we don't need to repeat it.
We usually don't link to the issue from versionchanged; that's for NEWS & whatsnew.