Skip to content

Type Inspection

IsInstance

IsInstance(expected_type: ExpectedType, *, only_direct_instance: bool = False)

Bases: DirtyEquals[ExpectedType]

A type which checks that the value is an instance of the expected type.

Parameters:

Name Type Description Default
expected_type ExpectedType

The type to check against.

required
only_direct_instance bool

whether instances of subclasses of expected_type should be considered equal.

False

Note

IsInstance can be parameterized or initialised with a type - IsInstance[Foo] is exactly equivalent to IsInstance(Foo).

This allows usage to be analogous to type hints.

Example:

IsInstance
from dirty_equals import IsInstance

class Foo:
    pass

class Bar(Foo):
    pass

assert Foo() == IsInstance[Foo]
assert Foo() == IsInstance(Foo)
assert Foo != IsInstance[Bar]

assert Bar() == IsInstance[Foo]
assert Foo() == IsInstance(Foo, only_direct_instance=True)
assert Bar() != IsInstance(Foo, only_direct_instance=True)

HasName

HasName(expected_name: Union[IsStr, str], *, allow_instances: bool = True)

Bases: DirtyEquals[T]

A type which checks that the value has the given __name__ attribute.

Parameters:

Name Type Description Default
expected_name Union[IsStr, str]

The name to check against.

required
allow_instances bool

whether instances of classes with the given name should be considered equal, (e.g. whether other.__class__.__name__ == expected_name should be checked).

True

Example:

HasName
from dirty_equals import HasName, IsStr

class Foo:
    pass

assert Foo == HasName('Foo')
assert Foo == HasName['Foo']
assert Foo() == HasName('Foo')
assert Foo() != HasName('Foo', allow_instances=False)
assert Foo == HasName(IsStr(regex='F..'))
assert Foo != HasName('Bar')
assert int == HasName('int')
assert int == HasName('int')

HasRepr

HasRepr(expected_repr: Union[IsStr, str])

Bases: DirtyEquals[T]

A type which checks that the value has the given repr() value.

Parameters:

Name Type Description Default
expected_repr Union[IsStr, str]

The expected repr value.

required

Example:

HasRepr
from dirty_equals import HasRepr, IsStr

class Foo:
    def __repr__(self):
        return 'This is a Foo'

assert Foo() == HasRepr('This is a Foo')
assert Foo() == HasRepr['This is a Foo']
assert Foo == HasRepr(IsStr(regex='<class.+'))
assert 42 == HasRepr('42')
assert 43 != HasRepr('42')

HasAttributes

HasAttributes(*expected_args: Dict[Any, Any], **expected_kwargs: Any)

Bases: DirtyEquals[Any]

A type which checks that the value has the given attributes.

This is a partial check - e.g. the attributes provided to check do not need to be exhaustive.

Can be created from either keyword arguments or an existing dictionary (same as dict()).

Example:

HasAttributes
from dirty_equals import AnyThing, HasAttributes, IsInt, IsStr

class Foo:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def spam(self):
        pass

assert Foo(1, 2) == HasAttributes(a=1, b=2)
assert Foo(1, 2) == HasAttributes(a=1)
assert Foo(1, 's') == HasAttributes(a=IsInt, b=IsStr)
assert Foo(1, 2) != HasAttributes(a=IsInt, b=IsStr)
assert Foo(1, 2) != HasAttributes(a=1, b=2, c=3)
assert Foo(1, 2) == HasAttributes(a=1, b=2, spam=AnyThing)