--- /dev/null
+[flake8]
+max-line-length = 88
+select = C,E,F,W,B,B9
+ignore = E203, E501, W503
+exclude = __init__.py
from starlette.websockets import WebSocket
try:
- from pydantic.fields import FieldInfo, ModelField
+ from pydantic.fields import ModelField
except ImportError: # pragma: nocover
# TODO: remove when removing support for Pydantic < 1.0.0
- from pydantic import Schema as FieldInfo # type: ignore
from pydantic.fields import Field as ModelField # type: ignore
print(data.client_secret)
return data
-
+
It creates the following Form request parameters in your endpoint:
grant_type: the OAuth2 spec says it is required and MUST be the fixed string "password".
print(data.client_secret)
return data
-
+
It creates the following Form request parameters in your endpoint:
grant_type: the OAuth2 spec says it is required and MUST be the fixed string "password".
"pytest-cov ==2.10.0",
"pytest-asyncio >=0.14.0,<0.15.0",
"mypy ==0.782",
+ "flake8 >=3.8.3,<4.0.0",
"black ==19.10b0",
"isort >=5.0.6,<6.0.0",
"requests >=2.24.0,<3.0.0",
set -x
mypy fastapi
+flake8 fastapi tests
black fastapi tests --check
isort fastapi tests docs_src scripts --check-only
@app.get("/query/int/default")
-def get_query_type_optional(query: int = 10):
+def get_query_type_int_default(query: int = 10):
return f"foo bar {query}"
},
},
},
- "summary": "Get Query Type Optional",
- "operationId": "get_query_type_optional_query_int_default_get",
+ "summary": "Get Query Type Int Default",
+ "operationId": "get_query_type_int_default_query_int_default_get",
"parameters": [
{
"required": False,
assert response.headers["content-type"] == "text/html; charset=utf-8"
assert "swagger-ui-dist" in response.text
assert (
- f"oauth2RedirectUrl: window.location.origin + '/docs/oauth2-redirect'"
+ "oauth2RedirectUrl: window.location.origin + '/docs/oauth2-redirect'"
in response.text
)
@app.get("/async-callable-dependency")
-async def get_callable_dependency(value: str = Depends(async_callable_dependency)):
+async def get_async_callable_dependency(
+ value: str = Depends(async_callable_dependency),
+):
return value
@app.get("/async-callable-gen-dependency")
-async def get_callable_gen_dependency(
+async def get_async_callable_gen_dependency(
value: str = Depends(async_callable_gen_dependency),
):
return value
def test_async_state():
- assert state["/async"] == f"asyncgen not started"
+ assert state["/async"] == "asyncgen not started"
response = client.get("/async")
assert response.status_code == 200, response.text
- assert response.json() == f"asyncgen started"
- assert state["/async"] == f"asyncgen completed"
+ assert response.json() == "asyncgen started"
+ assert state["/async"] == "asyncgen completed"
def test_sync_state():
- assert state["/sync"] == f"generator not started"
+ assert state["/sync"] == "generator not started"
response = client.get("/sync")
assert response.status_code == 200, response.text
- assert response.json() == f"generator started"
- assert state["/sync"] == f"generator completed"
+ assert response.json() == "generator started"
+ assert state["/sync"] == "generator completed"
def test_async_raise_other():
def test_sync_async_state():
response = client.get("/sync_async")
assert response.status_code == 200, response.text
- assert response.json() == f"asyncgen started"
- assert state["/async"] == f"asyncgen completed"
+ assert response.json() == "asyncgen started"
+ assert state["/async"] == "asyncgen completed"
def test_sync_sync_state():
response = client.get("/sync_sync")
assert response.status_code == 200, response.text
- assert response.json() == f"generator started"
- assert state["/sync"] == f"generator completed"
+ assert response.json() == "generator started"
+ assert state["/sync"] == "generator completed"
def test_sync_async_raise_other():
def test_encode_model_with_alias_raises():
with pytest.raises(ValidationError):
- model = ModelWithAlias(foo="Bar")
+ ModelWithAlias(foo="Bar")
def test_encode_model_with_alias():
media_type = "application/vnd.api+json"
+
# NOTE: These are not valid JSON:API resources
# but they are fine for testing requestBody with custom media_type
class Product(BaseModel):
@app.get("/no-alias/dict", response_model=ModelNoAlias)
-def by_alias_dict():
+def no_alias_dict():
return {"name": "Foo"}
@app.get("/no-alias/model", response_model=ModelNoAlias)
-def by_alias_model():
+def no_alias_model():
return ModelNoAlias(name="Foo")
@app.get("/no-alias/list", response_model=List[ModelNoAlias])
-def by_alias_list():
+def no_alias_list():
return [{"name": "Foo"}, {"name": "Bar"}]
},
"/no-alias/dict": {
"get": {
- "summary": "By Alias Dict",
- "operationId": "by_alias_dict_no_alias_dict_get",
+ "summary": "No Alias Dict",
+ "operationId": "no_alias_dict_no_alias_dict_get",
"responses": {
"200": {
"description": "Successful Response",
},
"/no-alias/model": {
"get": {
- "summary": "By Alias Model",
- "operationId": "by_alias_model_no_alias_model_get",
+ "summary": "No Alias Model",
+ "operationId": "no_alias_model_no_alias_model_get",
"responses": {
"200": {
"description": "Successful Response",
},
"/no-alias/list": {
"get": {
- "summary": "By Alias List",
- "operationId": "by_alias_list_no_alias_list_get",
+ "summary": "No Alias List",
+ "operationId": "no_alias_list_no_alias_list_get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
- "title": "Response By Alias List No Alias List Get",
+ "title": "Response No Alias List No Alias List Get",
"type": "array",
"items": {
"$ref": "#/components/schemas/ModelNoAlias"
@app.post("/login")
# Here we use string annotations to test them
-def read_current_user(form_data: "OAuth2PasswordRequestFormStrict" = Depends()):
+def login(form_data: "OAuth2PasswordRequestFormStrict" = Depends()):
return form_data
},
},
},
- "summary": "Read Current User",
- "operationId": "read_current_user_login_post",
+ "summary": "Login",
+ "operationId": "login_login_post",
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
- "$ref": "#/components/schemas/Body_read_current_user_login_post"
+ "$ref": "#/components/schemas/Body_login_login_post"
}
}
},
},
"components": {
"schemas": {
- "Body_read_current_user_login_post": {
- "title": "Body_read_current_user_login_post",
+ "Body_login_login_post": {
+ "title": "Body_login_login_post",
"required": ["grant_type", "username", "password"],
"type": "object",
"properties": {
@app.post("/login")
-def read_current_user(form_data: OAuth2PasswordRequestFormStrict = Depends()):
+def login(form_data: OAuth2PasswordRequestFormStrict = Depends()):
return form_data
@app.get("/users/me")
-def read_current_user(current_user: Optional[User] = Depends(get_current_user)):
+def read_users_me(current_user: Optional[User] = Depends(get_current_user)):
if current_user is None:
return {"msg": "Create an account first"}
return current_user
},
},
},
- "summary": "Read Current User",
- "operationId": "read_current_user_login_post",
+ "summary": "Login",
+ "operationId": "login_login_post",
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
- "$ref": "#/components/schemas/Body_read_current_user_login_post"
+ "$ref": "#/components/schemas/Body_login_login_post"
}
}
},
"content": {"application/json": {"schema": {}}},
}
},
- "summary": "Read Current User",
- "operationId": "read_current_user_users_me_get",
+ "summary": "Read Users Me",
+ "operationId": "read_users_me_users_me_get",
"security": [{"OAuth2": []}],
}
},
},
"components": {
"schemas": {
- "Body_read_current_user_login_post": {
- "title": "Body_read_current_user_login_post",
+ "Body_login_login_post": {
+ "title": "Body_login_login_post",
"required": ["grant_type", "username", "password"],
"type": "object",
"properties": {
@app.get("/", response_model=Model, response_model_exclude_unset=True)
-def get() -> ModelSubclass:
+def get_root() -> ModelSubclass:
return ModelSubclass(sub={}, y=1, z=0)
@app.get(
"/exclude_unset", response_model=ModelDefaults, response_model_exclude_unset=True
)
-def get() -> ModelDefaults:
+def get_exclude_unset() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
response_model=ModelDefaults,
response_model_exclude_defaults=True,
)
-def get() -> ModelDefaults:
+def get_exclude_defaults() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
@app.get(
"/exclude_none", response_model=ModelDefaults, response_model_exclude_none=True
)
-def get() -> ModelDefaults:
+def get_exclude_none() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
-def get() -> ModelDefaults:
+def get_exclude_unset_none() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
@app.get("/items/{item_id}")
-async def create_item(item_id: str):
+async def read_item(item_id: str):
if item_id not in items:
raise HTTPException(
status_code=404,
@app.get("/starlette-items/{item_id}")
-async def create_item(item_id: str):
+async def read_starlette_item(item_id: str):
if item_id not in items:
raise StarletteHTTPException(status_code=404, detail="Item not found")
return {"item": items[item_id]}
},
},
},
- "summary": "Create Item",
- "operationId": "create_item_items__item_id__get",
+ "summary": "Read Item",
+ "operationId": "read_item_items__item_id__get",
"parameters": [
{
"required": True,
},
},
},
- "summary": "Create Item",
- "operationId": "create_item_starlette_items__item_id__get",
+ "summary": "Read Starlette Item",
+ "operationId": "read_starlette_item_starlette_items__item_id__get",
"parameters": [
{
"required": True,
response = client.get("/docs")
assert response.status_code == 200, response.text
print(response.text)
- assert f"ui.initOAuth" in response.text
- assert f'"appName": "The Predendapp"' in response.text
- assert f'"clientId": "the-foo-clients"' in response.text
+ assert "ui.initOAuth" in response.text
+ assert '"appName": "The Predendapp"' in response.text
+ assert '"clientId": "the-foo-clients"' in response.text
def test_response():
assert data["text"] == note["text"]
assert data["completed"] == note["completed"]
assert "id" in data
- response = client.get(f"/notes/")
+ response = client.get("/notes/")
assert response.status_code == 200, response.text
assert data in response.json()
body_missing = {
"detail": [
- {"loc": ["body"], "msg": "field required", "type": "value_error.missing",}
+ {"loc": ["body"], "msg": "field required", "type": "value_error.missing"}
]
}
@router.websocket("/router2")
-async def routerindex(websocket: WebSocket):
+async def routerindex2(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Hello, router!")
await websocket.close()