Skip to content

gh-118150: warn in doc about results asymmetry for difflib junk#153892

Open
Lenormju wants to merge 4 commits into
python:mainfrom
Lenormju:gh-118150-difflib-autojunk-asymmetry-doc
Open

gh-118150: warn in doc about results asymmetry for difflib junk#153892
Lenormju wants to merge 4 commits into
python:mainfrom
Lenormju:gh-118150-difflib-autojunk-asymmetry-doc

Conversation

@Lenormju

@Lenormju Lenormju commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

@python-cla-bot

python-cla-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app Bot added the docs Documentation in the Doc dir label Jul 18, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Docs PRs Jul 18, 2026
@read-the-docs-community

read-the-docs-community Bot commented Jul 18, 2026

Copy link
Copy Markdown

@encukou encukou added the sprint label Jul 18, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Sprint Jul 18, 2026
@encukou encukou self-assigned this Jul 18, 2026
Comment thread Doc/library/difflib.rst Outdated
Comment thread Doc/library/difflib.rst Outdated

This heuristic only ever inspects the second sequence for junk. That has
an unexpected consequence: swapping the inputs can produce a different
diff than simply reversing the output, sometimes very large.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds ungrammatical.
Do we need to note that output can be very large?

Suggested change
diff than simply reversing the output, sometimes very large.
diff than reversing the output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes, because it is the original issue, and the most unexpected behavior. #118150 describes a pathological case where, depending on the inputs' order, the diff can be 1186x bigger. I call that surprising, and very large. In this case, it was because most lines got autojunked.

I will fix the grammar right now, sorry for that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworded the two junk-related paragraphs.
About the size:

But it can also cause pathological cases where too many items considered junk cause an unexpectedly large (but correct) diff result.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, is it the only case that can produce unexpectedly large result?
Your wording tends to imply that avoiding junk will result in “good” and small results.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my understanding, if "junk" did not exist, diffs would be more "minimal", but less pleasing to humans. Junk is an heuristic by Tim to produce better looking diffs for humans (and also being more efficient at doing so).
So I think my implication is true.
But is it the only case ? I ran some experiments, and there is a bias (not a general rule): setting junk tends to produce bigger diffs. But it definitely enables the pathological ones (when the algo has nothing to grasp on, as nearly every item is considered junk).
Setting autojunk=False and isjunk=None, to disable all junk behavior, will cause diffs to be closer to the lower bound possible of the Ratcliff–Obershelp algorithm, but the results would not be as nice to humans (cf the private volatile example here : #80136 (comment)). "good" is subjective. It depends on what you want. Quoting my current wording:

[junk] can produce better results for humans [...] But it can also cause pathological cases [...] unexpectedly large (but correct) diff result

So yes:

avoiding junk will result in “good” and small results

... just not for humans, and less efficiently.

Comment thread Doc/library/difflib.rst Outdated
Comment thread Doc/library/difflib.rst
Comment on lines 22 to 25
.. class:: SequenceMatcher
:noindex:

This is a flexible class for comparing pairs of sequences of any type, so long

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making this an introduction to the difflib module, rather than docs for the SequenceMatcher class (which is documented properly below)?

Something like this, plus dedenting it:

All functionality in this module uses the algorithm implemented in
:class:`SequenceMatcher`, a flexible class for comparing pairs
of sequences of any type, so long […]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, because in the end everything relies on it. But can we do that in another PR, iteratively ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir sprint

Projects

Status: Todo
Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants