Predicates and factories

Predicates are functions that return a boolean value given a single argument. These modules contain predicate functions, and functions that return predicates, that can be composed and used for phantom types.

Boolean logic

phantom.predicates.boolean.true(_value)[source]

Always return True.

Parameters

_value (object) –

Return type

typing.Literal[True]

phantom.predicates.boolean.false(_value)[source]

Always return False.

Parameters

_value (object) –

Return type

typing.Literal[False]

phantom.predicates.boolean.negate(predicate)[source]

Negate a given predicate.

Parameters

predicate (typing.Callable[[-T], bool]) –

Return type

typing.Callable[[-T], bool]

phantom.predicates.boolean.truthy(value)[source]

Return True for truthy objects.

Parameters

value (object) –

Return type

bool

phantom.predicates.boolean.falsy(value)[source]

Return True for falsy objects.

Parameters

value (object) –

Return type

bool

phantom.predicates.boolean.both(p, q)[source]

Create a new predicate that succeeds when both of the given predicates succeed.

Parameters
  • p (typing.Callable[[-T], bool]) –

  • q (typing.Callable[[-T], bool]) –

Return type

typing.Callable[[-T], bool]

phantom.predicates.boolean.either(p, q)[source]

Create a new predicate that succeeds when at least one of the given predicates succeed.

Parameters
  • p (typing.Callable[[-T], bool]) –

  • q (typing.Callable[[-T], bool]) –

Return type

typing.Callable[[-T], bool]

phantom.predicates.boolean.xor(p, q)[source]

Create a new predicate that succeeds when one of the given predicates succeed, but not both.

Parameters
  • p (typing.Callable[[-T], bool]) –

  • q (typing.Callable[[-T], bool]) –

Return type

typing.Callable[[-T], bool]

phantom.predicates.boolean.all_of(predicates)[source]

Create a new predicate that succeeds when all of the given predicates succeed.

Parameters

predicates (typing.Iterable[typing.Callable[[-T], bool]]) –

Return type

typing.Callable[[-T], bool]

phantom.predicates.boolean.any_of(predicates)[source]

Create a new predicate that succeeds when at least one of the given predicates succeed.

Parameters

predicates (typing.Iterable[typing.Callable[[-T], bool]]) –

Return type

typing.Callable[[-T], bool]

phantom.predicates.boolean.one_of(predicates)[source]

Create a new predicate that succeeds when exactly one of the given predicates succeed.

Parameters

predicates (typing.Iterable[typing.Callable[[-T], bool]]) –

Return type

typing.Callable[[-T], bool]

Collection

phantom.predicates.collection.contains(value)[source]

Create a new predicate that succeeds when its argument contains value.

Parameters

value (object) –

Return type

typing.Callable[[Container], bool]

phantom.predicates.collection.contained(container)[source]

Create a new predicate that succeeds when its argument is contained by container.

Parameters

container (typing.Container) –

Return type

typing.Callable[[object], bool]

phantom.predicates.collection.count(predicate)[source]

Create a predicate that succeeds when the size of its argument satisfies the given predicate.

Parameters

predicate (typing.Callable[[int], bool]) –

Return type

typing.Callable[[Sized], bool]

phantom.predicates.collection.exists(predicate)[source]

Create a predicate that succeeds when one or more items in its argument satisfies predicate.

Parameters

predicate (typing.Callable[[~_O], bool]) –

Return type

typing.Callable[[Iterable], bool]

phantom.predicates.collection.every(predicate)[source]

Create a predicate that succeeds when all items in its argument satisfy predicate.

Parameters

predicate (typing.Callable[[~_O], bool]) –

Return type

typing.Callable[[Iterable], bool]

Datetime

phantom.predicates.datetime.is_tz_aware(dt)[source]

Return True if dt is timezone aware.

Parameters

dt (datetime.datetime) –

Return type

bool

phantom.predicates.datetime.is_tz_naive(dt)[source]

Return True if dt is timezone naive.

Parameters

dt (datetime.datetime) –

Return type

bool

Generic

phantom.predicates.generic.equal(a)[source]

Create a new predicate that succeeds when its argument is equal to a.

Parameters

a (object) –

Return type

typing.Callable[[object], bool]

phantom.predicates.generic.identical(a)[source]

Create a new predicate that succeeds when its argument is identical to a.

Parameters

a (object) –

Return type

typing.Callable[[object], bool]

phantom.predicates.generic.of_type(t)[source]

Create a new predicate that succeeds when its argument is an instance of t.

Parameters

t (typing.Union[type, typing.Tuple[type, …]]) –

Return type

typing.Callable[[object], bool]

Numeric intervals

Numeric

Regular expressions

phantom.predicates.re.is_match(pattern)[source]

Create a new predicate that succeeds when the start of its argument matches the given pattern.

Parameters

pattern (typing.Pattern[str]) –

Return type

typing.Callable[[str], bool]

phantom.predicates.re.is_full_match(pattern)[source]

Create a new predicate that succeeds when its whole argument matches the given pattern.

Parameters

pattern (typing.Pattern[str]) –

Return type

typing.Callable[[str], bool]