Skip to content

Commit ce3ebd2

Browse files
docs: improve example formatting in PhoneNumber documentation (#347)
1 parent bede935 commit ce3ebd2

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

pydantic_extra_types/phone_numbers.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,54 +35,56 @@ class PhoneNumber(str):
3535
### Normal usage:
3636
3737
```python
38-
from pydantic import BaseModel
39-
from pydantic_extra_types.phone_numbers import PhoneNumber
38+
from pydantic import BaseModel
39+
from pydantic_extra_types.phone_numbers import PhoneNumber
40+
41+
42+
class Contact(BaseModel):
43+
name: str
44+
phone: PhoneNumber
4045
41-
class Contact(BaseModel):
42-
name: str
43-
phone: PhoneNumber
4446
45-
c = Contact(name='Alice', phone='+1 650-253-0000')
46-
print(c.phone)
47-
>> tel:+1-650-253-0000 (formatted using RFC3966 by default)
47+
c = Contact(name='Alice', phone='+1 650-253-0000')
48+
print(c.phone)
49+
# > tel:+1-650-253-0000 (formatted using RFC3966 by default)
4850
```
4951
5052
### Changing defaults by subclassing:
5153
5254
```python
53-
from pydantic_extra_types.phone_numbers import PhoneNumber
55+
from pydantic_extra_types.phone_numbers import PhoneNumber
5456
55-
class USPhone(PhoneNumber):
56-
default_region_code = 'US'
57-
supported_regions = ['US']
58-
phone_format = 'NATIONAL'
5957
60-
# Now parsing will accept national numbers for the US
61-
p = USPhone('650-253-0000')
62-
print(p)
63-
>> 650-253-0000
58+
class USPhone(PhoneNumber):
59+
default_region_code = 'US'
60+
supported_regions = ['US']
61+
phone_format = 'NATIONAL'
62+
63+
64+
# Now parsing will accept national numbers for the US
65+
p = USPhone('650-253-0000')
66+
print(p)
67+
# > 650-253-0000
6468
```
6569
6670
### Changing defaults by using the provided validator annotation:
6771
6872
```python
69-
from typing import Annotated, Union
70-
import phonenumbers
71-
from pydantic import BaseModel
72-
from pydantic_extra_types.phone_numbers import PhoneNumberValidator
73+
from typing import Annotated, Union
74+
import phonenumbers
75+
from pydantic import BaseModel
76+
from pydantic_extra_types.phone_numbers import PhoneNumberValidator
7377
74-
E164NumberType = Annotated[
75-
Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator(number_format="E164")
76-
]
78+
E164NumberType = Annotated[Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator(number_format='E164')]
7779
7880
79-
class Model(BaseModel):
80-
phone: E164NumberType
81+
class Model(BaseModel):
82+
phone: E164NumberType
8183
8284
83-
m = Model(phone="+1 650-253-0000")
84-
print(m.phone)
85-
>> +16502530000
85+
m = Model(phone='+1 650-253-0000')
86+
print(m.phone)
87+
# > +16502530000
8688
```
8789
8890
"""

0 commit comments

Comments
 (0)