Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-133722: Add Difflib theme to _colorize and 'color' option to difflib.unified_diff#133725
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
dougthor42 commented May 9, 2025 • edited by github-actions bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by github-actions bot
Uh oh!
There was an error while loading. Please reload this page.
tomasr8 commented May 9, 2025
Thanks for the PR! With the basic theming support in #133347 merged, perhaps we could define a separate difflib theme and use that here? |
Uh oh!
There was an error while loading. Please reload this page.
hugovk commented May 9, 2025
Thanks for the PR! I had a play with something similar but didn't get very far before the 3.14 freeze.
Yes, let's add theming support. See this docstring for instructions. For a couple of standalone examples of adding theme support to a module, see 230d658 and 5c5c3e1 from that PR. |
dougthor42 commented May 9, 2025
Ah, TIL! Thanks, I'll take a look at those docs and update accordingly. It'll be a few days a least before I can get to it, so for now I'll revert this back to draft. |
hugovk commented May 9, 2025
Absolutely no rush for this, we have 12 months until the 3.15 feature freeze. Don't hesitate if you have questions :) |
dougthor42 left a comment
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.
Wow that was really easy! The examples helped a lot too, thanks! PTAL at your leisure.
Uh oh!
There was an error while loading. Please reload this page.
_colorize and 'color' option to difflib.unified_diffLib/_colorize.py Outdated
| equal: str = ANSIColors.RESET # context lines | ||
| insert: str = ANSIColors.GREEN | ||
| delete: str = ANSIColors.RED |
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.
I opted to use the difflib.context_diff internal terminology here. Should I use the more git-like terms context, added, and removed instead?
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.
Hmm, maybe the Git-like terms if they're going to be more familiar with people and if the difflib ones are all internal.
Where does Git mention them?
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.
Overall they are defined in the GNU unified diff detailed description.
The git diff man page mentions them in a variety of places (emphasis mine):
-U
--unified=Generate diffs with lines of context instead of the usual three. Implies --patch.
Generating patch text with -p
- Hunk headers mention the name of the function to which the hunk applies.
plain
Any line that is added in one location and was removed in another location will be colored with
color.diff.newMoved.
Though sometimes it uses "deleted" instead of "removed":
--numstat
Similar to --stat, but shows number of added and deleted lines
I've gone ahead and switched to the GNU unified diff terms prior to the requested sorting. Also, to keep consistent with the other dataclass attributes (which are not currently sorted), the reset value was left at the end.
ZeroIntensity left a comment
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.
Hm, I would expect to see this in the difflib CLI rather than a library function. But, as it turns out, difflib doesn't have a working CLI? That's different than what's documented.
I personally think it's worth adding one if we're going through the effort to add colorization.
hugovk left a comment
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.
Let's also add a What's New entry, perhaps something similar to https://docs.python.org/3.14/whatsnew/3.14.html#whatsnew314-color-argparse
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Lib/_colorize.py Outdated
| equal: str = ANSIColors.RESET # context lines | ||
| insert: str = ANSIColors.GREEN | ||
| delete: str = ANSIColors.RED |
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.
Hmm, maybe the Git-like terms if they're going to be more familiar with people and if the difflib ones are all internal.
Where does Git mention them?
Uh oh!
There was an error while loading. Please reload this page.
Lib/_colorize.py Outdated
| syntax: Syntax = field(default_factory=Syntax) | ||
| traceback: Traceback = field(default_factory=Traceback) | ||
| unittest: Unittest = field(default_factory=Unittest) | ||
| difflib: Difflib = field(default_factory=Difflib) |
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.
And here:
| syntax: Syntax=field(default_factory=Syntax) | |
| traceback: Traceback=field(default_factory=Traceback) | |
| unittest: Unittest=field(default_factory=Unittest) | |
| difflib: Difflib=field(default_factory=Difflib) | |
| difflib: Difflib=field(default_factory=Difflib) | |
| syntax: Syntax=field(default_factory=Syntax) | |
| traceback: Traceback=field(default_factory=Traceback) | |
| unittest: Unittest=field(default_factory=Unittest) |
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.
I'm very slightly concerned about sorting when the dataclasses aren't kw_only.
Should I update the dataclass decorator to include kw_only=True and then sort? Or is that out of scope of this PR?
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.
Let's ask @ambv about this.
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.
I like this suggestion.
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.
👍 Done
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| @@ -0,0 +1,2 @@ | |||
| Added a ``color`` option to :func:`difflib.unified_diff` that injects ANSI color | |||
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.
| Added a ``color`` option to :func:`difflib.unified_diff` that injects ANSI color | |
| Added a *color* option to :func:`difflib.unified_diff` that injects ANSI color |
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.
Done
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.
Still todo :)
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.
Oops!
dougthor42 left a comment • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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.
What's New entry
Added, assuming a 3.15 release.
I would expect to see this in the difflib CLI rather than a library function.
My underlying use case and request is specific to the library function. That said, I agree that any CLI should also include coloring.
difflib doesn't have a working CLI? That's different than what's documented.
Hmm... I'm not able to find the difflib CLI docs? The closest thing I found was https://docs.python.org/3/library/difflib.html#difflib-interface but that just looks like an example of how to make a CLI for difflib. Was that what you were thinking of?
If I actually read the comment I would have seen that the docs were indeed linked... And yes, the linked item is just an example of how one might make a CLI for difflib.
I personally think it's worth adding one if we're going through the effort to add colorization.
I'd be happy to modify any existing CLI code to include colors. I can't say I'd have the time to add a CLI though.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Lib/_colorize.py Outdated
| equal: str = ANSIColors.RESET # context lines | ||
| insert: str = ANSIColors.GREEN | ||
| delete: str = ANSIColors.RED |
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.
Overall they are defined in the GNU unified diff detailed description.
The git diff man page mentions them in a variety of places (emphasis mine):
-U
--unified=Generate diffs with lines of context instead of the usual three. Implies --patch.
Generating patch text with -p
- Hunk headers mention the name of the function to which the hunk applies.
plain
Any line that is added in one location and was removed in another location will be colored with
color.diff.newMoved.
Though sometimes it uses "deleted" instead of "removed":
--numstat
Similar to --stat, but shows number of added and deleted lines
I've gone ahead and switched to the GNU unified diff terms prior to the requested sorting. Also, to keep consistent with the other dataclass attributes (which are not currently sorted), the reset value was left at the end.
Uh oh!
There was an error while loading. Please reload this page.
Lib/_colorize.py Outdated
| syntax: Syntax = field(default_factory=Syntax) | ||
| traceback: Traceback = field(default_factory=Traceback) | ||
| unittest: Unittest = field(default_factory=Unittest) | ||
| difflib: Difflib = field(default_factory=Difflib) |
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.
I'm very slightly concerned about sorting when the dataclasses aren't kw_only.
Should I update the dataclass decorator to include kw_only=True and then sort? Or is that out of scope of this PR?
| @@ -0,0 +1,2 @@ | |||
| Added a ``color`` option to :func:`difflib.unified_diff` that injects ANSI color | |||
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.
Done
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
dougthor42 commented May 14, 2025
@ZeroIntensity Doh! I just re-read your comment and realized you linked to it haha. Anyway yeah that's just example code. To my knowledge there is no official CLI for |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Lib/_colorize.py Outdated
| syntax: Syntax = field(default_factory=Syntax) | ||
| traceback: Traceback = field(default_factory=Traceback) | ||
| unittest: Unittest = field(default_factory=Unittest) | ||
| difflib: Difflib = field(default_factory=Difflib) |
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.
Let's ask @ambv about this.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| @@ -0,0 +1,2 @@ | |||
| Added a ``color`` option to :func:`difflib.unified_diff` that injects ANSI color | |||
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.
Still todo :)
Wulian233 commented May 28, 2025
This PR needs to be updated to resolve the conflicts → #134581 |
dougthor42 commented May 28, 2025
ACK @Wulian233. I was planning on waiting to resolve conflicts until after @ambv comments on #133725 (comment). |
ambv commented May 29, 2025
@dougthor42 I responded on that comment. It's a good suggestion to make the dataclasses kwonly. |
hugovk left a comment
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.
Thank you!
Lib/difflib.py Outdated
| Set *color* to ``True`` to enable output in color, similar to | ||
| :program:`git diff --color`. Even if enabled, it can be | ||
| :ref:`controlled using environment variables <using-on-controlling-color>`. |
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.
The CPython docstrings aren't rendered into docs, so let's use plain formatting like elsewhere. Perhaps:
| Set*color*to``True``toenableoutputincolor, similarto | |
| :program:`git diff --color`. Evenifenabled, itcanbe | |
| :ref:`controlled using environment variables <using-on-controlling-color>`. | |
| Set'color'toTruetoenableoutputincolor, similarto | |
| 'git diff --color'. Evenifenabled, itcanbe | |
| controlledusingenvironmentvariablessuchas'NO_COLOR'. |
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.
Done. Also fixed the 3 spaces thing 🤦
Uh oh!
There was an error while loading. Please reload this page.
python-cla-botbot commented Jul 18, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
34d7351 into python:mainUh oh!
There was an error while loading. Please reload this page.
hugovk commented Aug 8, 2025
Thank you! |
…to `difflib.unified_diff` (python#133725)
Add a
colorarg (defaulting toFalse) todifflib.unified_diff. WhenTrue, ANSI color codes are injected to the diff lines so that the printed result looks likegit diff --color:Fixes#133722.
It's a bummer, I just missed the feature freeze window for 3.14 (it was yesterday! 😭), but c'est la vie!
color: boolarg todifflib.unified_diff#133722📚 Documentation preview 📚: https://cpython-previews--133725.org.readthedocs.build/