Skip to content

String Types

IsAnyStr

IsAnyStr(*, min_length: Optional[int] = None, max_length: Optional[int] = None, case: Literal['upper', 'lower', None] = None, regex: Union[None, T, Pattern[T]] = None, regex_flags: int = 0)

Bases: DirtyEquals[T]

Comparison of str or bytes objects.

This class allow comparison with both str and bytes but is subclassed by IsStr and IsBytes which restrict comparison to str or bytes respectively.

Parameters:

Name Type Description Default
min_length Optional[int]

minimum length of the string/bytes

None
max_length Optional[int]

maximum length of the string/bytes

None
case Literal['upper', 'lower', None]

check case of the string/bytes

None
regex Union[None, T, Pattern[T]]

regular expression to match the string/bytes with, re.fullmatch is used. This can be a compiled regex, or a string or bytes.

None
regex_flags int

optional flags for the regular expression

0

Examples:

IsAnyStr
from dirty_equals import IsAnyStr

assert 'foobar' == IsAnyStr()
assert b'foobar' == IsAnyStr()
assert 123 != IsAnyStr()
assert 'foobar' == IsAnyStr(regex='foo...')
assert 'foobar' == IsAnyStr(regex=b'foo...')  # (1)!

assert 'foobar' == IsAnyStr(min_length=6)
assert 'foobar' != IsAnyStr(min_length=8)

assert 'foobar' == IsAnyStr(case='lower')
assert 'Foobar' != IsAnyStr(case='lower')

  1. regex can be either a string or bytes, IsAnyStr will take care of conversion so checks work.

IsStr

IsStr(*, min_length: Optional[int] = None, max_length: Optional[int] = None, case: Literal['upper', 'lower', None] = None, regex: Union[None, T, Pattern[T]] = None, regex_flags: int = 0)

Bases: IsAnyStr[str]

Checks if the value is a string, and optionally meets some constraints.

IsStr is a subclass of IsAnyStr and therefore allows all the same arguments.

Examples:

IsStr
from dirty_equals import IsStr

assert 'foobar' == IsStr()
assert b'foobar' != IsStr()
assert 'foobar' == IsStr(regex='foo...')

assert 'FOOBAR' == IsStr(min_length=5, max_length=10, case='upper')

Parameters:

Name Type Description Default
min_length Optional[int]

minimum length of the string/bytes

None
max_length Optional[int]

maximum length of the string/bytes

None
case Literal['upper', 'lower', None]

check case of the string/bytes

None
regex Union[None, T, Pattern[T]]

regular expression to match the string/bytes with, re.fullmatch is used. This can be a compiled regex, or a string or bytes.

None
regex_flags int

optional flags for the regular expression

0

Examples:

IsAnyStr
from dirty_equals import IsAnyStr

assert 'foobar' == IsAnyStr()
assert b'foobar' == IsAnyStr()
assert 123 != IsAnyStr()
assert 'foobar' == IsAnyStr(regex='foo...')
assert 'foobar' == IsAnyStr(regex=b'foo...')  # (1)!

assert 'foobar' == IsAnyStr(min_length=6)
assert 'foobar' != IsAnyStr(min_length=8)

assert 'foobar' == IsAnyStr(case='lower')
assert 'Foobar' != IsAnyStr(case='lower')

  1. regex can be either a string or bytes, IsAnyStr will take care of conversion so checks work.

IsBytes

IsBytes(*, min_length: Optional[int] = None, max_length: Optional[int] = None, case: Literal['upper', 'lower', None] = None, regex: Union[None, T, Pattern[T]] = None, regex_flags: int = 0)

Bases: IsAnyStr[bytes]

Checks if the value is a bytes object, and optionally meets some constraints.

IsBytes is a subclass of IsAnyStr and therefore allows all the same arguments.

Examples:

IsBytes
from dirty_equals import IsBytes

assert b'foobar' == IsBytes()
assert 'foobar' != IsBytes()
assert b'foobar' == IsBytes(regex=b'foo...')

assert b'FOOBAR' == IsBytes(min_length=5, max_length=10, case='upper')

Parameters:

Name Type Description Default
min_length Optional[int]

minimum length of the string/bytes

None
max_length Optional[int]

maximum length of the string/bytes

None
case Literal['upper', 'lower', None]

check case of the string/bytes

None
regex Union[None, T, Pattern[T]]

regular expression to match the string/bytes with, re.fullmatch is used. This can be a compiled regex, or a string or bytes.

None
regex_flags int

optional flags for the regular expression

0

Examples:

IsAnyStr
from dirty_equals import IsAnyStr

assert 'foobar' == IsAnyStr()
assert b'foobar' == IsAnyStr()
assert 123 != IsAnyStr()
assert 'foobar' == IsAnyStr(regex='foo...')
assert 'foobar' == IsAnyStr(regex=b'foo...')  # (1)!

assert 'foobar' == IsAnyStr(min_length=6)
assert 'foobar' != IsAnyStr(min_length=8)

assert 'foobar' == IsAnyStr(case='lower')
assert 'Foobar' != IsAnyStr(case='lower')

  1. regex can be either a string or bytes, IsAnyStr will take care of conversion so checks work.