External wrappers

A collection of phantom types that wraps functionality of well maintained implementations of third-party validation libraries. Importing from phantom.ext.* should be a hint that more dependencies need to be installed.

Phone numbers

Requires the phonenumbers package which can be installed with:

$ python3 -m pip install phantom-types[phonenumbers]

Types

class phantom.ext.phonenumbers.PhoneNumber(instance)[source]

Bases: str, Phantom

class phantom.ext.phonenumbers.FormattedPhoneNumber(instance)[source]

Bases: PhoneNumber

classmethod parse(instance)[source]

Normalize number using phonenumbers.PhoneNumberFormat.E164.

Raises:

InvalidPhoneNumber

Parameters:

instance (object)

Return type:

phantom.ext.phonenumbers.FormattedPhoneNumber

Functions

phantom.ext.phonenumbers.is_phone_number(phone_number, country_code=None)
Parameters:
  • phone_number (str)

  • country_code (str | None)

Return type:

phonenumbers.phonenumber.PhoneNumber

phantom.ext.phonenumbers.is_formatted_phone_number(number)[source]
Parameters:

number (str)

Return type:

typing.TypeGuard[phantom.ext.phonenumbers.FormattedPhoneNumber]

phantom.ext.phonenumbers.normalize_phone_number(phone_number, country_code=None)[source]

Normalize phone_number using phonenumbers.PhoneNumberFormat.E164.

Raises:

InvalidPhoneNumber

Parameters:
  • phone_number (str)

  • country_code (str | None)

Return type:

phantom.ext.phonenumbers.FormattedPhoneNumber

Exceptions

exception phantom.ext.phonenumbers.InvalidPhoneNumber(error_type=99, msg='Invalid number')[source]

Bases: NumberParseException, TypeError

Parameters:
  • error_type (int)

  • msg (str)