Other Types¶
FunctionCheck ¶
Bases:
Use a function to check if a value "equals" whatever you want to check
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
|
callable that takes a value and returns a bool. |
required |
from dirty_equals import FunctionCheck
def is_even(x):
return x % 2 == 0
assert 2 == FunctionCheck(is_even)
assert 3 != FunctionCheck(is_even)
IsInstance ¶
Bases:
A type which checks that the value is an instance of the expected type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_type |
|
The type to check against. |
required |
only_direct_instance |
|
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 ¶
Bases:
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 |
|
Value to compare the JSON to, if omitted, any JSON is accepted. |
|
**expected_kwargs |
|
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 ¶
Bases:
A class that checks if a value is a valid UUID, optionally checking UUID version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version |
|
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:
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:
| Name | Type | Description | Default |
|---|---|---|---|
*repr_args |
|
unnamed args to be used in |
()
|
**repr_kwargs |
|
named args to be used in |
{}
|
IsOneOf ¶
Bases:
A type which checks that the value is equal to one of the given values.
Can be useful with boolean operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_value |
|
Expected value for equals to return true. |
required |
*more_expected_values |
|
More expected values for equals to return true. |
()
|
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:
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 |
|
any scheme allowed, host required |
False
|
any_http_url |
|
scheme http or https, host required |
False
|
http_url |
|
scheme http or https, host required, max length 2083 |
False
|
file_url |
|
scheme file, host not required |
False
|
postgres_dsn |
|
user info required |
False
|
ampqp_dsn |
|
schema amqp or amqps, user info not required, host not required |
False
|
redis_dsn |
|
scheme redis or rediss, user info not required, host not required |
False
|
**expected_attributes |
|
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:
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 |
|
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:
A class that checks if a value is a valid IP address, optionally checking IP version, netmask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version |
|
The version of the IP to check, if omitted, versions 4 and 6 are both accepted. |
None
|
netmask |
|
The netmask of the IP to check, if omitted, any netmask is accepted. Requires version. |
None
|
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:
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:
| Name | Type | Description | Default |
|---|---|---|---|
*repr_args |
|
unnamed args to be used in |
()
|
**repr_kwargs |
|
named args to be used in |
{}
|
IsDataclass ¶
Bases:
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 |
|
key-value pairs of field-value to check for. |
{}
|
settings ¶
Allows to customise the behaviour of IsDataclass, technically a new IsDataclass to allow chaining.
IsPartialDataclass ¶
Bases:
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 |
|
key-value pairs of field-value to check for. |
{}
|
IsStrictDataclass ¶
Bases:
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 |
|
key-value pairs of field-value to check for. |
{}
|
IsEnum ¶
Bases:
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:
| Name | Type | Description | Default |
|---|---|---|---|
enum_cls |
|
Enum class to check against. |
|