CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-dirty-equals

Python library that leverages the __eq__ method to make unit tests more declarative and readable through flexible comparison classes.

Pending
Overview
Eval results
Files

boolean-types.mddocs/

Boolean Types

Comparison classes for validating truthiness and falsiness with configurable behavior for different data types. These types provide flexible boolean validation including special handling for strings and customizable falsy value detection.

Capabilities

IsTrueLike

Checks if a value is truthy using Python's built-in bool() function. This follows standard Python truthiness rules where values like True, non-zero numbers, non-empty strings, and non-empty collections are considered truthy.

class IsTrueLike(DirtyEquals):
    """
    Checks if value is truthy using bool(other).
    
    Uses Python's standard truthiness evaluation:
    - True, non-zero numbers, non-empty strings/collections are truthy
    - False, 0, None, empty strings/collections are falsy
    """
    
    def equals(self, other: Any) -> bool:
        """
        Check if the value is truthy.
        
        Args:
            other: Value to check for truthiness
            
        Returns:
            bool: True if bool(other) is True
        """

Usage Examples

from dirty_equals import IsTrueLike

# Basic truthy values
assert True == IsTrueLike
assert 1 == IsTrueLike
assert -1 == IsTrueLike
assert 42 == IsTrueLike
assert "hello" == IsTrueLike
assert [1, 2, 3] == IsTrueLike
assert {"key": "value"} == IsTrueLike

# These would fail (falsy values)
# assert False == IsTrueLike  # False
# assert 0 == IsTrueLike      # False  
# assert "" == IsTrueLike     # False
# assert [] == IsTrueLike     # False
# assert None == IsTrueLike   # False

# In data validation
api_response = {
    'success': True,
    'data': [1, 2, 3],
    'message': 'Operation completed'
}

assert api_response == {
    'success': IsTrueLike,    # Must be truthy
    'data': IsTrueLike,       # Must have data
    'message': IsTrueLike     # Must have message
}

# With boolean logic
from dirty_equals import IsStr

# Must be both truthy and a string
assert "hello" == (IsTrueLike & IsStr)

IsFalseLike

Checks if a value is falsy, with optional special handling for strings. By default uses Python's standard falsy evaluation, but can be configured to treat certain strings as falsy beyond just empty strings.

class IsFalseLike(DirtyEquals):
    """
    Checks if value is falsy with optional string support.
    
    By default uses Python's falsy evaluation (False, 0, None, empty collections).
    With allow_strings=True, also treats certain strings as falsy.
    """
    
    def __init__(self, *, allow_strings: bool = False):
        """
        Initialize falsy checker.
        
        Args:
            allow_strings: If True, enables special string falsy checking
        """
    
    @staticmethod
    def make_string_check(other: str) -> bool:
        """
        Determine if a string should be considered falsy.
        
        Args:
            other: String to evaluate
            
        Returns:
            bool: True if string should be considered falsy
        """
    
    def equals(self, other: Any) -> bool:
        """
        Check if the value is falsy.
        
        Args:
            other: Value to check for falsiness
            
        Returns:
            bool: True if value is considered falsy
        """

Usage Examples

from dirty_equals import IsFalseLike

# Basic falsy values
assert False == IsFalseLike
assert 0 == IsFalseLike  
assert 0.0 == IsFalseLike
assert "" == IsFalseLike
assert [] == IsFalseLike
assert {} == IsFalseLike
assert None == IsFalseLike

# These would fail (truthy values)  
# assert True == IsFalseLike   # False
# assert 1 == IsFalseLike      # False
# assert "hello" == IsFalseLike # False
# assert [1] == IsFalseLike     # False

# With string support enabled
falsy_with_strings = IsFalseLike(allow_strings=True)

# Standard falsy values still work
assert False == falsy_with_strings
assert 0 == falsy_with_strings
assert "" == falsy_with_strings

# Additional string values treated as falsy (implementation specific)
assert "false" == falsy_with_strings  # Example - depends on implementation
assert "0" == falsy_with_strings      # Example - depends on implementation

# In data validation  
form_data = {
    'required_field': "",
    'optional_field': None,
    'enabled': False
}

# Check for missing/empty required fields
assert form_data == {
    'required_field': IsFalseLike,   # Empty string is falsy
    'optional_field': IsFalseLike,   # None is falsy
    'enabled': IsFalseLike           # False is falsy
}

# Validating disabled states
settings = {
    'notifications': False,
    'auto_save': 0,
    'theme': ""
}

# All should be disabled/empty
assert all(value == IsFalseLike for value in settings.values())

# With boolean combinations
from dirty_equals import IsStr

# Must be falsy but if it's a string, allow string falsy values
string_falsy = IsFalseLike(allow_strings=True)
assert "" == (IsFalseLike & IsStr)  # Empty string is both falsy and string

Type Definitions

from typing import Any

# No additional type definitions needed for boolean types
# Both classes inherit from DirtyEquals and use standard Python types

Install with Tessl CLI

npx tessl i tessl/pypi-dirty-equals

docs

base-types.md

boolean-types.md

datetime-types.md

dict-types.md

index.md

inspection-types.md

numeric-types.md

other-types.md

sequence-types.md

string-types.md

tile.json