Registry / testing / dirty-equals

dirty-equals

JSON →
library0.11pypypi✓ verified 25d ago

dirty-equals is a Python library for doing 'dirty' (but useful) things with equality comparisons. It allows for flexible assertions in tests and other contexts, such as comparing values against types, regular expressions, or fuzzy time ranges. The current version is 0.11.0, and the library maintains an active release cadence, with major updates typically every few months.

pip install dirty-equals
INSTALL
IMPORT
SIG · DIRTY-EQUALS
D
dirty-equals
testingpythonv0.11
Install
1.5s avg
Import
90ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.11 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.094s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.086s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

IsInt
from dirty_equals import IsInt
IsStr
from dirty_equals import IsStr
IsNow
from dirty_equals import IsNow
IsListOrTuple
from dirty_equals import IsListOrTuple
IsUrl
from dirty_equals import IsUrl

Demonstrates basic usage of dirty-equals for type checking, fuzzy time comparisons, and asserting elements within collections. The `IsNow()` helper provides a flexible way to compare datetimes without needing an exact match, useful in tests.

from datetime import datetime from dirty_equals import IsInt, IsStr, IsNow, IsListOrTuple # Basic type checking assert 123 == IsInt() assert "hello" == IsStr() # Fuzzy time comparison (matches if within a reasonable delta) now_time = datetime.now() # The library internally compares against datetime.now() at the moment of assertion. # The assertion will pass if now_time is approximately equal to the current time. assert now_time == IsNow() # Asserting content and types within collections assert [1, "two", 3] == IsListOrTuple(IsInt(), IsStr(), IsInt()) print("Dirty equals assertions successful!")
Debug
Known issues
breakingPython 3.8 support was dropped in version 0.10.0. dirty-equals now requires Python 3.9 or newer.
fix
Upgrade your Python environment to 3.9 or higher if you need to use dirty-equals 0.10.0+.
affects: >=0.10.0
gotchaThe v0.7.0 release was announced but never published to PyPI. Users attempting to install or reference v0.7.0 will fail. The intended changes were included in v0.7.1.
fix
Use version 0.7.1 or later instead of 0.7.0.
affects: 0.7.0
gotcha`IsNow` compares against the *current* moment of time when the assertion runs, not against a specific pre-defined timestamp. This is suitable for fuzzy time comparisons but not for exact matches against a fixed reference.
fix
Understand that `IsNow()` is dynamic. If you need to compare against a specific historical timestamp, you might need a different approach or to capture `datetime.now()` precisely when creating your expected value.
affects: <0.5.0 had different behavior, >=0.5.0 has current behavior
gotchaComparison of nested dataclasses was fixed in version 0.11.0. Older versions might exhibit unexpected behavior or failures when comparing complex nested dataclass structures.
fix
Ensure you are using dirty-equals v0.11.0 or newer when comparing nested dataclass objects to avoid subtle comparison bugs.
affects: <0.11.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dirty_equals'
The `dirty-equals` library has not been installed in your Python environment or the environment is not correctly activated.
fix
Install the library using pip: `pip install dirty-equals`
TypeError: Can't instantiate abstract class DirtyEquals with abstract method equals
When creating a custom equality type, you must inherit from `dirty_equals.DirtyEquals` and implement the abstract `equals` method within your subclass.
fix
Ensure your custom class overrides the `equals` method:
```python
from typing import Any
from dirty_equals import DirtyEquals

class MyCustomType(DirtyEquals):
    def equals(self, other: Any) -> bool:
        # Implement your custom comparison logic here
        return other == 'expected_value'
```
AttributeError: 'IsPositive' object has no attribute 'value'
The `.value` property on `dirty-equals` objects (like `IsPositive`) is only set and accessible *after* a successful equality comparison (`==`) has occurred, as it stores the value that successfully matched.
fix
Access the `.value` property only after the `dirty-equals` object has been successfully compared to a value. If no comparison has happened, or the comparison was unsuccessful, the property will not exist.
```python
from dirty_equals import IsPositive

p = IsPositive()
assert 42 == p
print(p.value) # Accessing .value after a successful comparison
```
AssertionError (when using dirty-equals types that require arguments without initialization, e.g., IsApprox)
Some `dirty-equals` types, like `IsApprox` or `IsStr(regex=...)`, require specific arguments during initialization. If these are omitted, the comparison will often silently return `False` instead of raising an error, leading to an `AssertionError` if used in an `assert` statement.
fix
Always initialize `dirty-equals` types that expect arguments with those arguments, even if using default values. For `IsApprox`, this means providing the approximate value and optionally a delta.
```python
from dirty_equals import IsApprox

# Incorrect usage (returns False if not initialized with an expected value)
# assert 10.1 == IsApprox

# Correct usage
assert 10.1 == IsApprox(10, delta=0.2)
```
Upgrade
Version history
0.11latest on PyPI · released Nov 17, 2025
Audit
Dependencies
pydanticoptionalRequired for specific dirty-equals validators like IsUrl, IsEmail, IsUUID, etc.
Agent activity
7 hits · last 30 days
node
6
Resources
dirty-equals — pip install dirty-equals · libregistry