|
1 | 1 | """The `pydantic_extra_types.phone_numbers` module provides the |
2 | 2 | [`PhoneNumber`][pydantic_extra_types.phone_numbers.PhoneNumber] data type. |
3 | 3 |
|
4 | | -This class depends on the [phonenumbers] package, which is a Python port of Google's [libphonenumber]. |
| 4 | +This class depends on the [phonenumbers](https://pypi.orgt/phonenumbers/) package, |
| 5 | +which is a Python port of Google's [libphonenumber](https://github.com/google/libphonenumber/). |
5 | 6 | """ |
6 | 7 |
|
7 | 8 | from __future__ import annotations |
|
25 | 26 |
|
26 | 27 |
|
27 | 28 | class PhoneNumber(str): |
28 | | - """A wrapper around [phonenumbers](https://pypi.org/project/phonenumbers/) package, which |
29 | | - is a Python port of Google's [libphonenumber](https://github.com/google/libphonenumber/). |
30 | | - """ |
31 | | - |
32 | | - supported_regions: list[str] = [] |
33 | | - """The supported regions. If empty, all regions are supported.""" |
| 29 | + """A wrapper around the `phonenumbers.PhoneNumber` object.""" |
34 | 30 |
|
35 | 31 | default_region_code: ClassVar[str | None] = None |
36 | 32 | """The default region code to use when parsing phone numbers without an international prefix.""" |
| 33 | + |
| 34 | + supported_regions: list[str] = [] |
| 35 | + """The supported regions. If empty, all regions are supported.""" |
| 36 | + |
37 | 37 | phone_format: str = 'RFC3966' |
38 | 38 | """The format of the phone number.""" |
39 | 39 |
|
@@ -78,38 +78,40 @@ def __hash__(self) -> int: |
78 | 78 |
|
79 | 79 | @dataclass(frozen=True) |
80 | 80 | class PhoneNumberValidator: |
81 | | - """A pydantic before validator for phone numbers using the [phonenumbers](https://pypi.org/project/phonenumbers/) package, |
82 | | - a Python port of Google's [libphonenumber](https://github.com/google/libphonenumber/). |
| 81 | + """An annotation to validate `phonenumbers.PhoneNumber` objects. |
83 | 82 |
|
84 | | - Intended to be used to create custom pydantic data types using the `typing.Annotated` type construct. |
| 83 | + Example: |
| 84 | + ```python |
| 85 | + from typing import Annotated, Union |
85 | 86 |
|
86 | | - Args: |
87 | | - default_region (str | None): The default region code to use when parsing phone numbers without an international prefix. |
88 | | - If `None` (default), the region must be supplied in the phone number as an international prefix. |
89 | | - number_format (str): The format of the phone number to return. See `phonenumbers.PhoneNumberFormat` for valid values. |
90 | | - supported_regions (list[str]): The supported regions. If empty, all regions are supported (default). |
| 87 | + import phonenumbers |
| 88 | + from pydantic import BaseModel |
| 89 | + from pydantic_extra_types.phone_numbers import PhoneNumberValidator |
91 | 90 |
|
92 | | - Returns: |
93 | | - The formatted phone number. |
| 91 | + MyNumberType = Annotated[Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator()] |
94 | 92 |
|
95 | | - Example: |
96 | | - MyNumberType = Annotated[ |
97 | | - Union[str, phonenumbers.PhoneNumber], |
98 | | - PhoneNumberValidator() |
99 | | - ] |
100 | 93 | USNumberType = Annotated[ |
101 | | - Union[str, phonenumbers.PhoneNumber], |
102 | | - PhoneNumberValidator(supported_regions=['US'], default_region='US') |
| 94 | + Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator(supported_regions=['US'], default_region='US') |
103 | 95 | ] |
104 | 96 |
|
| 97 | +
|
105 | 98 | class SomeModel(BaseModel): |
106 | 99 | phone_number: MyNumberType |
107 | 100 | us_number: USNumberType |
| 101 | + ``` |
108 | 102 | """ |
109 | 103 |
|
110 | 104 | default_region: str | None = None |
| 105 | + """The default region code to use when parsing phone numbers without an international prefix. |
| 106 | +
|
| 107 | + If `None` (the default), the region must be supplied in the phone number as an international prefix. |
| 108 | + """ |
| 109 | + |
111 | 110 | number_format: str = 'RFC3966' |
| 111 | + """The format of the phone number to return. See `phonenumbers.PhoneNumberFormat` for valid values.""" |
| 112 | + |
112 | 113 | supported_regions: Sequence[str] | None = None |
| 114 | + """The supported regions. If empty (the default), all regions are supported.""" |
113 | 115 |
|
114 | 116 | def __post_init__(self) -> None: |
115 | 117 | if self.default_region and self.default_region not in phonenumbers.SUPPORTED_REGIONS: |
|
0 commit comments