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.base.Phantom

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

Bases: phantom.ext.phonenumbers.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)
phantom.ext.phonenumbers.is_formatted_phone_number(number)[source]
Parameters

number (str) –

Return type

bool

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

Normalize phone_number using phonenumbers.PhoneNumberFormat.E164.

Raises

InvalidPhoneNumber

Exceptions

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

Bases: phonenumbers.phonenumberutil.NumberParseException, TypeError

Parameters
  • error_type (int) –

  • msg (str) –

Country codes

Requires the iso3166 package which can be installed with:

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

Types

class phantom.ext.iso3166.Alpha2(instance)[source]

Bases: str, phantom.base.Phantom

classmethod parse(instance)[source]

Normalize mixed case country code.

Raises

InvalidCountryCode

Parameters

instance (object) –

Return type

phantom.ext.iso3166.Alpha2

Functions

phantom.ext.iso3166.normalize_alpha2_country_code(country_code)[source]

Normalize mixed case country code.

Raises

InvalidCountryCode

Parameters

country_code (str) –

Return type

phantom.ext.iso3166.Alpha2

Exceptions

exception phantom.ext.iso3166.InvalidCountryCode[source]

Bases: TypeError