8000 gh-133722: Add Difflib theme to `_colorize` and 'color' option to `difflib.unified_diff` by dougthor42 · Pull Request #133725 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5c5b248
Add test case
dougthor42 May 9, 2025
fdc0fa0
Add 'color' arg to difflib.unified_diff.
dougthor42 May 9, 2025
fcdd7ab
Update docs and ACKs
dougthor42 May 9, 2025
0e9b070
blurb
dougthor42 May 9, 2025
7c31749
fixup to follow convention
dougthor42 May 9, 2025
66475a2
Add 'Difflib' theme
dougthor42 May 11, 2025
dbf0547
fixup tests
dougthor42 May 11, 2025
a72012e
Switch to using themes. So easy!
dougthor42 May 11, 2025
2a3d818
use 'next' in versionchanged docs
dougthor42 May 11, 2025
252982e
turns out 'git diff' adds reset to the start and end of context lines
dougthor42 May 11, 2025
3422fa7
Use GNU unified diff terms
dougthor42 May 14, 2025
bffdd71
move class
dougthor42 May 14, 2025
3255866
kw-only the 'color' arg
dougthor42 May 14, 2025
c48a6ac
Doc formatting updates
dougthor42 May 14, 2025
8ca50fa
Sort the things that are safe to sort without kw_only=True
dougthor42 May 14, 2025
eb0e81e
Update what's new
dougthor42 May 14, 2025
f7b34c3
Merge remote-tracking branch 'upstream/main' into difflib-color-gh133722
dougthor42 May 14, 2025
fb092b0
fixup docs
dougthor42 May 14, 2025
734b0bc
fixup docs
dougthor42 May 20, 2025
8a10e40
Code review: docs, whatsnew, f-strings, news
dougthor42 May 20, 2025
57b80d1
force_colorized
dougthor42 May 20, 2025
c235425
kw_only
dougthor42 May 29, 2025
387cfe6
Merge branch 'main' into difflib-color-gh133722
dougthor42 May 29, 2025
833d86a
documentation updates per code review
dougthor42 Jun 11, 2025
25f9d80
Merge branch 'main' into difflib-color-gh133722
dougthor42 Jul 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add 'Difflib' theme
  • Loading branch information
dougthor42 committed May 11, 2025
commit 66475a2c9fd65aa5ad3f24fd72053f93eb861425
14 changes: 14 additions & 0 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ class Unittest(ThemeSection):
reset: str = ANSIColors.RESET


@dataclass(frozen=True)
class Difflib(ThemeSection):
"""A 'git diff'-like theme for `difflib.unified_diff`."""
header: str = ANSIColors.BOLD # eg "---" and "+++" lines
hunk: str = ANSIColors.CYAN # the "@@" lines
insert: str = ANSIColors.GREEN
delete: str = ANSIColors.RED
reset: str = ANSIColors.RESET


@dataclass(frozen=True)
class Theme:
"""A suite of themes for all sections of Python.
Expand All @@ -218,6 +228,7 @@ class Theme:
syntax: Syntax = field(default_factory=Syntax)
traceback: Traceback = field(default_factory=Traceback)
unittest: Unittest = field(default_factory=Unittest)
difflib: Difflib = field(default_factory=Difflib)
Copy link
Member

Choose a reason for hiding this comment

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

And here:

Suggested change
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)

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like this suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Done


def copy_with(
self,
Expand All @@ -226,6 +237,7 @@ def copy_with(
syntax: Syntax | None = None,
traceback: Traceback | None = None,
unittest: Unittest | None = None,
difflib: Difflib | None = None,
) -> Self:
"""Return a new Theme based on this instance with some sections replaced.

Expand All @@ -237,6 +249,7 @@ def copy_with(
syntax=syntax or self.syntax,
traceback=traceback or self.traceback,
unittest=unittest or self.unittest,
difflib=difflib or self.difflib,
)

@classmethod
Expand All @@ -252,6 +265,7 @@ def no_colors(cls) -> Self:
syntax=Syntax.no_colors(),
traceback=Traceback.no_colors(),
unittest=Unittest.no_colors(),
difflib=Difflib.no_colors(),
)


Expand Down
0