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-equalsVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Python environment to 3.9 or higher if you need to use dirty-equals 0.10.0+.
Use version 0.7.1 or later instead of 0.7.0.
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.
Ensure you are using dirty-equals v0.11.0 or newer when comparing nested dataclass objects to avoid subtle comparison bugs.
Install the library using pip: `pip install dirty-equals`
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'
```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 ```
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) ```