Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⬆ Upgrade Starlette from 0.18.0 to 0.19.0 #4488

Merged
merged 21 commits into from May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi/concurrency.py
Expand Up @@ -25,7 +25,7 @@ async def contextmanager_in_threadpool(
try:
yield await run_in_threadpool(cm.__enter__)
except Exception as e:
ok = await run_in_threadpool(cm.__exit__, type(e), e, None)
ok: bool = await run_in_threadpool(cm.__exit__, type(e), e, None)
if not ok:
raise e
else:
Expand Down
11 changes: 4 additions & 7 deletions fastapi/dependencies/utils.py
Expand Up @@ -462,13 +462,10 @@ async def solve_dependencies(
]:
values: Dict[str, Any] = {}
errors: List[ErrorWrapper] = []
response = response or Response(
content=None,
status_code=None, # type: ignore
headers=None, # type: ignore # in Starlette
media_type=None, # type: ignore # in Starlette
background=None, # type: ignore # in Starlette
)
if response is None:
response = Response()
del response.headers["content-length"]
response.status_code = None # type: ignore
dependency_cache = dependency_cache or {}
sub_dependant: Dependant
for sub_dependant in dependant.dependencies:
Expand Down
16 changes: 2 additions & 14 deletions fastapi/exceptions.py
@@ -1,20 +1,8 @@
from typing import Any, Dict, Optional, Sequence, Type
from typing import Any, Sequence, Type

from pydantic import BaseModel, ValidationError, create_model
from pydantic.error_wrappers import ErrorList
from starlette.exceptions import HTTPException as StarletteHTTPException


class HTTPException(StarletteHTTPException):
def __init__(
self,
status_code: int,
detail: Any = None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several examples and places in the code base that expect detail to be any JSON-able data, not only a string (as is with Starlette).

So, only for this little type hint we still need the custom HTTPException class in FastAPI.

headers: Optional[Dict[str, Any]] = None,
) -> None:
super().__init__(status_code=status_code, detail=detail)
self.headers = headers

from starlette.exceptions import HTTPException as HTTPException # noqa

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the renaming of import, i.e., as HTTPException needed at the last?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from starlette.exceptions import HTTPException as HTTPException # noqa
from starlette.exceptions import HTTPException

I suppose there's not reason for noqa anymore.


RequestErrorModel: Type[BaseModel] = create_model("Request")
WebSocketErrorModel: Type[BaseModel] = create_model("WebSocket")
Expand Down
2 changes: 1 addition & 1 deletion fastapi/routing.py
Expand Up @@ -126,7 +126,7 @@ async def serialize_response(
if is_coroutine:
value, errors_ = field.validate(response_content, {}, loc=("response",))
else:
value, errors_ = await run_in_threadpool(
value, errors_ = await run_in_threadpool( # type: ignore[misc]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding proper type hints for these names?

field.validate, response_content, {}, loc=("response",)
)
if isinstance(errors_, ErrorWrapper):
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Expand Up @@ -35,7 +35,7 @@ classifiers = [
"Topic :: Internet :: WWW/HTTP",
]
requires = [
"starlette ==0.17.1",
"starlette @ git+https://github.com/encode/starlette.git#master",
"pydantic >=1.6.2,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0",
]
description-file = "README.md"
Expand Down Expand Up @@ -129,8 +129,8 @@ check_untyped_defs = true

[tool.pytest.ini_options]
addopts = [
"--strict-config",
"--strict-markers",
"--strict-config",
Kludex marked this conversation as resolved.
Show resolved Hide resolved
"--strict-markers"
]
xfail_strict = true
junit_family = "xunit2"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_extra_routes.py
Expand Up @@ -32,12 +32,12 @@ def delete_item(item_id: str, item: Item):

@app.head("/items/{item_id}")
def head_item(item_id: str):
return JSONResponse(headers={"x-fastapi-item-id": item_id})
return JSONResponse(None, headers={"x-fastapi-item-id": item_id})
Kludex marked this conversation as resolved.
Show resolved Hide resolved


@app.options("/items/{item_id}")
def options_item(item_id: str):
return JSONResponse(headers={"x-fastapi-item-id": item_id})
return JSONResponse(None, headers={"x-fastapi-item-id": item_id})


@app.patch("/items/{item_id}")
Expand All @@ -47,7 +47,7 @@ def patch_item(item_id: str, item: Item):

@app.trace("/items/{item_id}")
def trace_item(item_id: str):
return JSONResponse(media_type="message/http")
return JSONResponse(None, media_type="message/http")


client = TestClient(app)
Expand Down