Dirty Equals
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.
Warnings
- breaking Python 3.8 support was dropped in version 0.10.0. dirty-equals now requires Python 3.9 or newer.
- gotcha The 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.
- 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.
- gotcha Comparison of nested dataclasses was fixed in version 0.11.0. Older versions might exhibit unexpected behavior or failures when comparing complex nested dataclass structures.
Install
-
pip install dirty-equals
Imports
- 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
Quickstart
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!")