Browse Source
* docs: Fix pydantic example in python-types.md * 📝 Update Python Types Intro to include Optional Co-authored-by: Sebastián Ramírez <[email protected]>pull/1569/head
committed by
GitHub
4 changed files with 77 additions and 41 deletions
@ -1,7 +1,8 @@ |
|||||
class Person: |
from typing import Optional |
||||
def __init__(self, name: str): |
|
||||
self.name = name |
|
||||
|
|
||||
|
|
||||
def get_person_name(one_person: Person): |
def say_hi(name: Optional[str] = None): |
||||
return one_person.name |
if name is not None: |
||||
|
print(f"Hey {name}!") |
||||
|
else: |
||||
|
print("Hello World") |
||||
|
@ -1,23 +1,7 @@ |
|||||
from datetime import datetime |
class Person: |
||||
from typing import List |
def __init__(self, name: str): |
||||
|
self.name = name |
||||
|
|
||||
from pydantic import BaseModel |
|
||||
|
|
||||
|
def get_person_name(one_person: Person): |
||||
class User(BaseModel): |
return one_person.name |
||||
id: int |
|
||||
name = "John Doe" |
|
||||
signup_ts: datetime = None |
|
||||
friends: List[int] = [] |
|
||||
|
|
||||
|
|
||||
external_data = { |
|
||||
"id": "123", |
|
||||
"signup_ts": "2017-06-01 12:22", |
|
||||
"friends": [1, "2", b"3"], |
|
||||
} |
|
||||
user = User(**external_data) |
|
||||
print(user) |
|
||||
# > User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3] |
|
||||
print(user.id) |
|
||||
# > 123 |
|
||||
|
@ -0,0 +1,23 @@ |
|||||
|
from datetime import datetime |
||||
|
from typing import List, Optional |
||||
|
|
||||
|
from pydantic import BaseModel |
||||
|
|
||||
|
|
||||
|
class User(BaseModel): |
||||
|
id: int |
||||
|
name = "John Doe" |
||||
|
signup_ts: Optional[datetime] = None |
||||
|
friends: List[int] = [] |
||||
|
|
||||
|
|
||||
|
external_data = { |
||||
|
"id": "123", |
||||
|
"signup_ts": "2017-06-01 12:22", |
||||
|
"friends": [1, "2", b"3"], |
||||
|
} |
||||
|
user = User(**external_data) |
||||
|
print(user) |
||||
|
# > User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3] |
||||
|
print(user.id) |
||||
|
# > 123 |
Loading…
Reference in new issue