Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pydantic_extra_types/pendulum_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,8 @@ def to_iso8601_string(self) -> str:

In addition to the standard ISO 8601 format, this method also supports the representation of fractions of a second and negative durations.

Args:
duration (Duration): The Duration object.

Returns:
str: The ISO 8601 string representation of the duration.
The ISO 8601 string representation of the duration.
"""
# Extracting components from the Duration object
years = self.years
Expand Down
48 changes: 25 additions & 23 deletions pydantic_extra_types/phone_numbers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""The `pydantic_extra_types.phone_numbers` module provides the
[`PhoneNumber`][pydantic_extra_types.phone_numbers.PhoneNumber] data type.

This class depends on the [phonenumbers] package, which is a Python port of Google's [libphonenumber].
This class depends on the [phonenumbers](https://pypi.orgt/phonenumbers/) package,
which is a Python port of Google's [libphonenumber](https://github.com/google/libphonenumber/).
"""

from __future__ import annotations
Expand All @@ -25,15 +26,14 @@


class PhoneNumber(str):
"""A wrapper around [phonenumbers](https://pypi.org/project/phonenumbers/) package, which
is a Python port of Google's [libphonenumber](https://github.com/google/libphonenumber/).
"""
"""A wrapper around the `phonenumbers.PhoneNumber` object."""

default_region_code: ClassVar[str | None] = None
"""The default region code to use when parsing phone numbers without an international prefix."""

supported_regions: list[str] = []
"""The supported regions. If empty, all regions are supported."""

default_region_code: ClassVar[str | None] = None
"""The default region code to use when parsing phone numbers without an international prefix."""
phone_format: str = 'RFC3966'
"""The format of the phone number."""

Expand Down Expand Up @@ -78,38 +78,40 @@ def __hash__(self) -> int:

@dataclass(frozen=True)
class PhoneNumberValidator:
"""A pydantic before validator for phone numbers using the [phonenumbers](https://pypi.org/project/phonenumbers/) package,
a Python port of Google's [libphonenumber](https://github.com/google/libphonenumber/).
"""An annotation to validate `phonenumbers.PhoneNumber` objects.

Intended to be used to create custom pydantic data types using the `typing.Annotated` type construct.
Example:
```python
from typing import Annotated, Union

Args:
default_region (str | None): The default region code to use when parsing phone numbers without an international prefix.
If `None` (default), the region must be supplied in the phone number as an international prefix.
number_format (str): The format of the phone number to return. See `phonenumbers.PhoneNumberFormat` for valid values.
supported_regions (list[str]): The supported regions. If empty, all regions are supported (default).
import phonenumbers
from pydantic import BaseModel
from pydantic_extra_types.phone_numbers import PhoneNumberValidator

Returns:
The formatted phone number.
MyNumberType = Annotated[Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator()]

Example:
MyNumberType = Annotated[
Union[str, phonenumbers.PhoneNumber],
PhoneNumberValidator()
]
USNumberType = Annotated[
Union[str, phonenumbers.PhoneNumber],
PhoneNumberValidator(supported_regions=['US'], default_region='US')
Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator(supported_regions=['US'], default_region='US')
]


class SomeModel(BaseModel):
phone_number: MyNumberType
us_number: USNumberType
```
"""

default_region: str | None = None
"""The default region code to use when parsing phone numbers without an international prefix.

If `None` (the default), the region must be supplied in the phone number as an international prefix.
"""

number_format: str = 'RFC3966'
"""The format of the phone number to return. See `phonenumbers.PhoneNumberFormat` for valid values."""

supported_regions: Sequence[str] | None = None
"""The supported regions. If empty (the default), all regions are supported."""

def __post_init__(self) -> None:
if self.default_region and self.default_region not in phonenumbers.SUPPORTED_REGIONS:
Expand Down