Skip to content

gh-118150: difflib: expose autojunk flag from SequenceMatcher to public methods and functions - #153959

Open
faithlesstomas wants to merge 27 commits into
python:mainfrom
faithlesstomas:gh-118150-expose-autojunk
Open

gh-118150: difflib: expose autojunk flag from SequenceMatcher to public methods and functions#153959
faithlesstomas wants to merge 27 commits into
python:mainfrom
faithlesstomas:gh-118150-expose-autojunk

Conversation

@faithlesstomas

@faithlesstomas faithlesstomas commented Jul 18, 2026

Copy link
Copy Markdown

Until now difflib methods and functions took SequenceMatcher class kwarg autojunk by default which is set up to be True in this class.. This PR aims to expose this flag to the public methods and functions of this module, in order that user can choose behavior of this option in SequenceMatcher.

Issue: #118150
Related PR: #153892

@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 commented Jul 18, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@faithlesstomas

faithlesstomas commented Jul 18, 2026

Copy link
Copy Markdown
Author

Quick check if this works for the original issue test:

 #!/usr/bin/python3                                                                                                                                                          
                                                                                                                                                                              
  import difflib                                                                                                                                                              
                                                                                                                                                                              
  def get_lines(filename):                                                                                                                                                    
      with open(filename, 'r', encoding='utf8') as fd:                                                                                                                        
          return fd.readlines()                                                                                                                                               
                                                                                                                                                                              
  for autojunk in (True, False):                                                                                                                                              
                                                                                                                                                                              
      old_new = list(difflib.unified_diff(                                                                                                                                    
          get_lines('small.external.old.json'),                                                                                                                               
          get_lines('small.external.new.json'),                                                                                                                               
          autojunk=autojunk                                                                                                                                                   
      ))                                                                                                                                                                      
      new_old = list(difflib.unified_diff(                                                                                                                                    
          get_lines('small.external.new.json'),                                                                                                                               
          get_lines('small.external.old.json'),                                                                                                                               
          autojunk=autojunk                                                                                                                                                   
      ))                                                                                                                                                                      
                                                                                                                                                                              
      print('Autojunk flag:', autojunk)                                                                                                                                       
      print('diff external.old external.new.json | wc -l')                                                                                                                    
      print(len(old_new))                                                                                                                                                     
      print('diff external.new external.old.json | wc -l')                                                                                                                    
      print(len(new_old))                                                                                                                                                     
Autojunk flag: True
diff external.old external.new.json | wc -l
90
diff -u external.new external.old.json | wc -l
25
Autojunk flag: False
diff external.old external.new.json | wc -l
25
diff -u external.new external.old.json | wc -l
25

#118150 (comment)

@faithlesstomas faithlesstomas changed the title difflib: expose autojunk flag from SequenceMatcher to public methods and functions gh-118150: difflib: expose autojunk flag from SequenceMatcher to public methods and functions Jul 18, 2026
@Lenormju

Copy link
Copy Markdown
Contributor

I think the Documentation (Doc/library/difflib.rst) should get updated too to include this new parameter.

@hugovk hugovk left a comment

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.

Please could you add tests to Lib/test/test_difflib.py?

Comment thread .github/workflows/build.yml Outdated
Comment thread Lib/difflib.py Outdated
Comment thread Lib/difflib.py Outdated
Comment thread Lib/difflib.py Outdated
Comment thread Lib/difflib.py Outdated
Comment thread Lib/difflib.py Outdated
Comment thread Lib/difflib.py Outdated
Comment thread Lib/difflib.py Outdated
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Comment thread Lib/difflib.py Outdated
faithlesstomas and others added 10 commits August 5, 2026 01:02
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
…python#153959)

* Add tests in `TestAutojunk` verifying `autojunk` propagation
  and behavior across `get_close_matches`, `Differ`, `ndiff`,
  `unified_diff`, `context_diff`, and `HtmlDiff`.

* Add signature inspection tests using `inspect.signature`
  to verify that `autojunk=True` is present and defined
  as a keyword-only parameter.

* Fix passing of `autojunk` as a keyword argument
  in `difflib._mdiff()` call to `ndiff()`.
…junk

* fixed conflicts in Doc/library/difflib.rst
@read-the-docs-community

read-the-docs-community Bot commented Aug 5, 2026

Copy link
Copy Markdown

@encukou encukou left a comment

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.

The change looks good; let's make the docs great :)

Comment thread Doc/library/difflib.rst Outdated
Comment thread Doc/library/difflib.rst Outdated
.. versionchanged:: 3.16
Added keyword-only *autojunk* parameter.

Optional *autojunk* flag sets on/off automatic junk heuristic of :class:`SequenceMatcher`.

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.

There's now a section for the heuristic in the description; it would be good to link there:

Suggested change
Optional *autojunk* flag sets on/off automatic junk heuristic of :class:`SequenceMatcher`.
Setting the optional *autojunk* argument to ``False`` will turn
:ref:`automatic junk heuristic <difflib-junk>` off.

Please use the same wording for all of these (unless there's a difference that needs to be pointed out).

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 is still relevant:

Please use the same wording for all of these (unless there's a difference that needs to be pointed out).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done!

Comment thread Doc/library/difflib.rst Outdated
Comment thread Doc/library/difflib.rst Outdated
Comment thread Doc/library/difflib.rst Outdated
@webknjaz
webknjaz removed their request for review August 10, 2026 21:15
faithlesstomas and others added 6 commits August 12, 2026 09:41
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Add references to junk heuristic description section
for *autojunk* kwarg.
Updating description of *autojunk* kwarg
to have exactly the same wording everywhere.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

5 participants