Other Types¶
FunctionCheck ¶
Bases: DirtyEquals[Any]
Use a function to check if a value "equals" whatever you want to check
Parameters:
from dirty_equals import FunctionCheck
def is_even(x):
return x % 2 == 0
assert 2 == FunctionCheck(is_even)
assert 3 != FunctionCheck(is_even)
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 |
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:
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)
IsJson ¶
IsJson(expected_value: JsonType = AnyJson, **expected_kwargs: Any)
Bases: DirtyEquals[JsonType]
A class that checks if a value is a JSON object, and check the contents of the JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected_value |
JsonType
|
Value to compare the JSON to, if omitted, any JSON is accepted. |
AnyJson
|
**expected_kwargs |
Any
|
Keyword arguments forming a dict to compare the JSON to,
|
{}
|
As with any dirty_equals
type, types can be nested to provide more complex checks.
Note
Like IsInstance
, IsJson
can be parameterized or initialised with a value -
IsJson[xyz]
is exactly equivalent to IsJson(xyz)
.
This allows usage to be analogous to type hints.
from dirty_equals import IsJson, IsPositiveInt, IsStrictDict
assert '{"a": 1, "b": 2}' == IsJson
assert '{"a": 1, "b": 2}' == IsJson(a=1, b=2)
assert '{"a": 1}' != IsJson(a=2)
assert 'invalid json' != IsJson
assert '{"a": 1}' == IsJson(a=IsPositiveInt)
assert '"just a quoted string"' == IsJson('just a quoted string')
assert '{"a": 1, "b": 2}' == IsJson[IsStrictDict(a=1, b=2)]
assert '{"b": 2, "a": 1}' != IsJson[IsStrictDict(a=1, b=2)]
IsUUID ¶
IsUUID(version: Literal[None, 1, 2, 3, 4, 5] = None)
Bases: DirtyEquals[UUID]
A class that checks if a value is a valid UUID, optionally checking UUID version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version |
Literal[None, 1, 2, 3, 4, 5]
|
The version of the UUID to check, if omitted, all versions are accepted. |
None
|
import uuid
from dirty_equals import IsUUID
assert 'edf9f29e-45c7-431c-99db-28ea44df9785' == IsUUID
assert 'edf9f29e-45c7-431c-99db-28ea44df9785' == IsUUID(4)
assert 'edf9f29e45c7431c99db28ea44df9785' == IsUUID(4)
assert 'edf9f29e-45c7-431c-99db-28ea44df9785' != IsUUID(5)
assert uuid.uuid4() == IsUUID(4)
AnyThing ¶
Bases: DirtyEquals[Any]
A type which matches any value. AnyThing
isn't generally very useful on its own, but can be used within
other comparisons.
from dirty_equals import AnyThing, IsList, IsStrictDict
assert 1 == AnyThing
assert 'foobar' == AnyThing
assert [1, 2, 3] == AnyThing
assert [1, 2, 3] == IsList(AnyThing, 2, 3)
assert {'a': 1, 'b': 2, 'c': 3} == IsStrictDict(a=1, b=AnyThing, c=3)
Parameters:
IsOneOf ¶
Bases: DirtyEquals[Any]
A type which checks that the value is equal to one of the given values.
Can be useful with boolean operators.
Parameters:
from dirty_equals import Contains, IsOneOf
assert 1 == IsOneOf(1, 2, 3)
assert 4 != IsOneOf(1, 2, 3)
# check that a list either contain 1 or is empty
assert [1, 2, 3] == Contains(1) | IsOneOf([])
assert [] == Contains(1) | IsOneOf([])
IsUrl ¶
IsUrl(any_url: bool = False, any_http_url: bool = False, http_url: bool = False, file_url: bool = False, postgres_dsn: bool = False, ampqp_dsn: bool = False, redis_dsn: bool = False, **expected_attributes: Any)
Bases: DirtyEquals[Any]
A class that checks if a value is a valid URL, optionally checking different URL types and attributes with Pydantic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
any_url |
bool
|
any scheme allowed, host required |
False
|
any_http_url |
bool
|
scheme http or https, host required |
False
|
http_url |
bool
|
scheme http or https, host required, max length 2083 |
False
|
file_url |
bool
|
scheme file, host not required |
False
|
postgres_dsn |
bool
|
user info required |
False
|
ampqp_dsn |
bool
|
schema amqp or amqps, user info not required, host not required |
False
|
redis_dsn |
bool
|
scheme redis or rediss, user info not required, host not required |
False
|
**expected_attributes |
Any
|
Expected values for url attributes |
{}
|
from dirty_equals import IsUrl
assert 'https://example.com' == IsUrl
assert 'https://example.com' == IsUrl(host='example.com')
assert 'https://example.com' == IsUrl(scheme='https')
assert 'https://example.com' != IsUrl(scheme='http')
assert 'postgres://user:pass@localhost:5432/app' == IsUrl(postgres_dsn=True)
assert 'postgres://user:pass@localhost:5432/app' != IsUrl(http_url=True)
IsHash ¶
Bases: DirtyEquals[str]
A class that checks if a value is a valid common hash type, using a simple length and allowed characters regex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hash_type |
HashTypes
|
The hash type to check. Must be specified. |
required |
from dirty_equals import IsHash
assert 'f1e069787ece74531d112559945c6871' == IsHash('md5')
assert b'f1e069787ece74531d112559945c6871' == IsHash('md5')
assert 'f1e069787ece74531d112559945c6871' != IsHash('sha-256')
assert 'F1E069787ECE74531D112559945C6871' == IsHash('md5')
assert '40bd001563085fc35165329ea1ff5c5ecbdbbeef' == IsHash('sha-1')
assert 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3' == IsHash(
'sha-256'
)
IsIP ¶
Bases: DirtyEquals[IP]
A class that checks if a value is a valid IP address, optionally checking IP version, netmask.
Parameters:
from ipaddress import IPv4Address, IPv4Network, IPv6Address
from dirty_equals import IsIP
assert '179.27.154.96' == IsIP
assert '179.27.154.96' == IsIP(version=4)
assert '2001:0db8:0a0b:12f0:0000:0000:0000:0001' == IsIP(version=6)
assert IPv4Address('127.0.0.1') == IsIP
assert IPv4Network('43.48.0.0/12') == IsIP
assert IPv6Address('::eeff:ae3f:d473') == IsIP
assert '54.43.53.219/10' == IsIP(version=4, netmask='255.192.0.0')
assert '54.43.53.219/10' == IsIP(version=4, netmask=4290772992)
assert '::ffff:aebf:d473/12' == IsIP(version=6, netmask='fff0::')
assert 3232235521 == IsIP
IsDataclassType ¶
Bases: DirtyEquals[Any]
Checks that an object is a dataclass type.
Inherits from DirtyEquals
.
from dataclasses import dataclass
from dirty_equals import IsDataclassType
@dataclass
class Foo:
a: int
b: int
foo = Foo(1, 2)
assert Foo == IsDataclassType
assert foo != IsDataclassType
Parameters:
IsDataclass ¶
IsDataclass(**fields: Any)
Bases: DirtyEquals[Any]
Checks that an object is an instance of a dataclass.
Inherits from DirtyEquals
and it can be initialised with specific keyword arguments to
check exactness of dataclass fields, by comparing the instance __dict__
with IsDict
.
Moreover it is possible to check for strictness and partialness of the dataclass, by setting the strict
and
partial
attributes using the .settings(strict=..., partial=...)
method.
Remark that passing no kwargs to IsDataclass
initialization means fields are not checked, not that the dataclass
is empty, namely IsDataclass()
is the same as IsDataclass
.
from dataclasses import dataclass
from dirty_equals import IsInt, IsDataclass
@dataclass
class Foo:
a: int
b: int
c: str
foo = Foo(1, 2, 'c')
assert foo == IsDataclass
assert foo == IsDataclass(a=IsInt, b=2, c='c')
assert foo == IsDataclass(b=2, a=1).settings(partial=True)
assert foo != IsDataclass(a=IsInt, b=2).settings(strict=True)
assert foo == IsDataclass(a=IsInt, b=2).settings(strict=True, partial=True)
assert foo != IsDataclass(b=2, a=1).settings(strict=True, partial=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
Any
|
key-value pairs of field-value to check for. |
{}
|
settings ¶
settings(*, strict: bool | None = None, partial: bool | None = None) -> IsDataclass
Allows to customise the behaviour of IsDataclass
, technically a new IsDataclass
to allow chaining.
IsPartialDataclass ¶
IsPartialDataclass(**fields: Any)
Bases: IsDataclass
Inherits from IsDataclass
with partial=True
by default.
from dataclasses import dataclass
from dirty_equals import IsInt, IsPartialDataclass
@dataclass
class Foo:
a: int
b: int
c: str = 'c'
foo = Foo(1, 2, 'c')
assert foo == IsPartialDataclass
assert foo == IsPartialDataclass(a=1)
assert foo == IsPartialDataclass(b=2, a=IsInt)
assert foo != IsPartialDataclass(b=2, a=IsInt).settings(strict=True)
assert Foo != IsPartialDataclass
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
Any
|
key-value pairs of field-value to check for. |
{}
|
IsStrictDataclass ¶
IsStrictDataclass(**fields: Any)
Bases: IsDataclass
Inherits from IsDataclass
with strict=True
by default.
from dataclasses import dataclass
from dirty_equals import IsInt, IsStrictDataclass
@dataclass
class Foo:
a: int
b: int
c: str = 'c'
foo = Foo(1, 2, 'c')
assert foo == IsStrictDataclass
assert foo == IsStrictDataclass(
a=IsInt,
b=2,
).settings(partial=True)
assert foo != IsStrictDataclass(
a=IsInt,
b=2,
).settings(partial=False)
assert foo != IsStrictDataclass(b=2, a=IsInt, c='c')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
Any
|
key-value pairs of field-value to check for. |
{}
|
IsEnum ¶
Bases: DirtyEquals[Enum]
Checks if an instance is an Enum.
Inherits from DirtyEquals
.
from enum import Enum, auto
from dirty_equals import IsEnum
class ExampleEnum(Enum):
a = auto()
b = auto()
a = ExampleEnum.a
assert a == IsEnum
assert a == IsEnum(ExampleEnum)
Parameters: