Skip to main content

classdiff - Python Class Difffer

This is a small library to diff (data) classes or any other objects. It is intended two work one obejcts of the same time or NoneType. Diffing different classes is possible but might result in unexpected result. Different from most existing diff tools that produce data structures that show adds, removes or changes, this tool is intended to use for printing a dataclass and higlight the diff with different color coding, similar to the output of Terraform or Pulumi.

Usage

Just pass your classes to the diff function to get back a representation of the diff.

Given the following two classes:

new = SomeResource(
    name="my-data",
    price=2.3,
    quantity=4,
    dangerous=True,
    other=OtherClass(name="OA"),
    not_changed=OtherClass(name="Same"),
)

old = SomeResource(
    name="my-data",
    price=3.3,
    quantity=4,
    dangerous=False,
    other=OtherClass(name="OB"),
    not_changed=OtherClass(name="Same"),
)

Passing them to classdiff.diff in combinations of (None, new), (old, new) and (old, None) and printing the lines in the returned value, the following will be printed with colored output (green for added, red for removed and yellow for changed). Note that each element in the returned list is of type DiffInfo which implements __repr__ to print with proper prefix and color.

> diff(None, new)
----------------------------------------
+ SomeResource(
+   dangerous = True
+   name = my-data
+   not_changed = OtherClass(
+     name = Same
+   )
+   other = OtherClass(
+     name = OA
+   )
+   price = 2.3
+   quantity = 4
+ )

> diff(old, new)
----------------------------------------
~ SomeResource(
~   dangerous = False => True
    name = my-data
    not_changed = OtherClass(
      name = Same
    )
~   other = OtherClass(
~     name = OB => OA
~   )
~   price = 3.3 => 2.3
    quantity = 4
~ )

> diff(old, None)
----------------------------------------
- SomeResource(
-   dangerous = False
-   name = my-data
-   not_changed = OtherClass(
-     name = Same
-   )
-   other = OtherClass(
-     name = OB
-   )
-   price = 3.3
-   quantity = 4
- )

Development

All code is formatted and analyzed with ruff. Tests are run with pytest.

 poetry run ruff check .
› poetry run ruff format .
› poetry run mypy .
› poetry run pytest tests/

Release files for classdiff 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for classdiff 0.5.2
File Size Uploaded
classdiff-0.5.2.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for classdiff 0.5.2
File Interpreter ABI Platform
classdiff-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / classdiff-0.5.2.tar.gz

Download URL classdiff-0.5.2.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fc950879a4434aae06b010fe281d28e35c01b92e4ef6c4f23912b5e339830517
BLAKE2b-256 checksum
How to use checksums
7fa8e6ce0755261f849ddadca3f158b2902a2d4d10adb60b1697605f0b211791
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.6 Darwin/25.0.0

Release files / classdiff-0.5.2-py3-none-any.whl

Download URL classdiff-0.5.2-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffb115f2e9b25a2794fbff0e327514cf437f0ce0fd31cf551746ee0a2d39444c
BLAKE2b-256 checksum
How to use checksums
38fca3496c33b955250a11c8f21415942cf4102b82e1c929d19c373803faf9e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.6 Darwin/25.0.0

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page