Dirty Equals

0.11.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

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!")

view raw JSON →