mirror of
https://github.com/fastapi/fastapi.git
synced 2026-07-14 17:03:09 -04:00
📝 Add Python type hints tutorial
This commit is contained in:
3
docs/tutorial/src/python-types/tutorial003.py
Normal file
3
docs/tutorial/src/python-types/tutorial003.py
Normal file
@@ -0,0 +1,3 @@
|
||||
def get_name_with_age(name: str, age: int):
|
||||
name_with_age = name + " is this old: " + age
|
||||
return name_with_age
|
||||
3
docs/tutorial/src/python-types/tutorial004.py
Normal file
3
docs/tutorial/src/python-types/tutorial004.py
Normal file
@@ -0,0 +1,3 @@
|
||||
def get_name_with_age(name: str, age: int):
|
||||
name_with_age = name + " is this old: " + str(age)
|
||||
return name_with_age
|
||||
2
docs/tutorial/src/python-types/tutorial005.py
Normal file
2
docs/tutorial/src/python-types/tutorial005.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def get_items(item_a: str, item_b: int, item_c: float, item_d: bool, item_e: bytes):
|
||||
return item_a, item_b, item_c, item_d, item_d, item_e
|
||||
6
docs/tutorial/src/python-types/tutorial006.py
Normal file
6
docs/tutorial/src/python-types/tutorial006.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
def process_items(items: List[str]):
|
||||
for item in items:
|
||||
print(item)
|
||||
5
docs/tutorial/src/python-types/tutorial007.py
Normal file
5
docs/tutorial/src/python-types/tutorial007.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from typing import Tuple, Set
|
||||
|
||||
|
||||
def process_items(items_t: Tuple[int], items_s: Set[bytes]):
|
||||
return items_t, items_s
|
||||
7
docs/tutorial/src/python-types/tutorial008.py
Normal file
7
docs/tutorial/src/python-types/tutorial008.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from typing import Dict
|
||||
|
||||
|
||||
def process_items(prices: Dict[str, float]):
|
||||
for item_name, item_price in prices.items():
|
||||
print(item_name)
|
||||
print(item_price)
|
||||
7
docs/tutorial/src/python-types/tutorial009.py
Normal file
7
docs/tutorial/src/python-types/tutorial009.py
Normal file
@@ -0,0 +1,7 @@
|
||||
class Person:
|
||||
def __init__(self, name: str):
|
||||
self.name = name
|
||||
|
||||
|
||||
def get_person_name(one_person: Person):
|
||||
return one_person.name
|
||||
16
docs/tutorial/src/python-types/tutorial010.py
Normal file
16
docs/tutorial/src/python-types/tutorial010.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
|
||||
class User(BaseModel):
|
||||
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
|
||||
@@ -1,5 +1,4 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from sqlalchemy import Boolean, Column, Integer, String, create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base, declared_attr
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
Reference in New Issue
Block a user