From e5c934369bf8f47360ab31e3331f75ac386ec5db Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 26 Jan 2022 17:25:51 +0100 Subject: [PATCH 01/20] Bump starlette from 0.17.1 to 0.18.0 --- fastapi/dependencies/utils.py | 11 ++++------- pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index d4028d067b882..d0a4040ef7d5b 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -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 dependency_cache = dependency_cache or {} sub_dependant: Dependant for sub_dependant in dependant.dependencies: diff --git a/pyproject.toml b/pyproject.toml index 77c01322fe6b0..867f545acd60d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] requires = [ - "starlette ==0.17.1", + "starlette ==0.18.0", "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" From 8d10fcabb93d201d57f376350e4a12ac8feaaf5a Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 26 Jan 2022 17:34:44 +0100 Subject: [PATCH 02/20] Fix linter --- fastapi/dependencies/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index d0a4040ef7d5b..9dccd354efe7e 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -465,7 +465,7 @@ async def solve_dependencies( if response is None: response = Response() del response.headers["content-length"] - response.status_code = None + response.status_code = None # type: ignore dependency_cache = dependency_cache or {} sub_dependant: Dependant for sub_dependant in dependant.dependencies: From 7331e081679b1feba82aa848965baa6b8dcf85ac Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 26 Jan 2022 17:48:50 +0100 Subject: [PATCH 03/20] Fix mypy --- fastapi/concurrency.py | 4 ++-- fastapi/routing.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fastapi/concurrency.py b/fastapi/concurrency.py index 04382c69e486b..11732dd063eb0 100644 --- a/fastapi/concurrency.py +++ b/fastapi/concurrency.py @@ -1,5 +1,5 @@ import sys -from typing import AsyncGenerator, ContextManager, TypeVar +from typing import AsyncGenerator, ContextManager, Optional, TypeVar from starlette.concurrency import iterate_in_threadpool as iterate_in_threadpool # noqa from starlette.concurrency import run_in_threadpool as run_in_threadpool # noqa @@ -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: diff --git a/fastapi/routing.py b/fastapi/routing.py index f6d5370d6a3c7..1f5cab9c41e4a 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -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] field.validate, response_content, {}, loc=("response",) ) if isinstance(errors_, ErrorWrapper): From fb2fdf5d17afb2cd436594cc7e1c5d924b7ab5a9 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 26 Jan 2022 17:50:14 +0100 Subject: [PATCH 04/20] Remove unused Optional import --- fastapi/concurrency.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/concurrency.py b/fastapi/concurrency.py index 11732dd063eb0..becac3f33db76 100644 --- a/fastapi/concurrency.py +++ b/fastapi/concurrency.py @@ -1,5 +1,5 @@ import sys -from typing import AsyncGenerator, ContextManager, Optional, TypeVar +from typing import AsyncGenerator, ContextManager, TypeVar from starlette.concurrency import iterate_in_threadpool as iterate_in_threadpool # noqa from starlette.concurrency import run_in_threadpool as run_in_threadpool # noqa From c096ac6e58c72331d6d2218f83dd8bd978d0b1fb Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:22:54 +0100 Subject: [PATCH 05/20] Bump starlette from 0.18.0 to 0.19.0 --- tests/test_extra_routes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_extra_routes.py b/tests/test_extra_routes.py index 6aba3e8dda535..7b824f8874096 100644 --- a/tests/test_extra_routes.py +++ b/tests/test_extra_routes.py @@ -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}) @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}") @@ -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) From a96de7f0cfb437227fdfc257665192edf0a1b667 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:23:58 +0100 Subject: [PATCH 06/20] Add starlette master branch --- pyproject.toml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 867f545acd60d..23dc7dc1f954c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] requires = [ - "starlette ==0.18.0", + "starlette @ git+ssh://git@github.com/encode/starlette@master#egg=starlette", "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" @@ -75,7 +75,7 @@ doc = [ "mdx-include >=1.4.1,<2.0.0", "mkdocs-markdownextradata-plugin >=0.1.7,<0.3.0", "typer-cli >=0.0.12,<0.0.13", - "pyyaml >=5.3.1,<6.0.0" + "pyyaml >=5.3.1,<6.0.0", ] dev = [ "python-jose[cryptography] >=3.3.0,<4.0.0", @@ -128,10 +128,7 @@ ignore_missing_imports = true check_untyped_defs = true [tool.pytest.ini_options] -addopts = [ - "--strict-config", - "--strict-markers", -] +addopts = ["--strict-config", "--strict-markers"] xfail_strict = true junit_family = "xunit2" filterwarnings = [ From cc64170f3ff2e20d9c3ece7f1ff057d3c7138860 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:25:31 +0100 Subject: [PATCH 07/20] Undo some changes --- pyproject.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 23dc7dc1f954c..11bca9a62460f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -128,7 +128,10 @@ ignore_missing_imports = true check_untyped_defs = true [tool.pytest.ini_options] -addopts = ["--strict-config", "--strict-markers"] +addopts = [ + "--strict-config", + "--strict-markers" +] xfail_strict = true junit_family = "xunit2" filterwarnings = [ From 03b0d4cbb1e0fd1cdd17bd643735a3cde1c87138 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:27:10 +0100 Subject: [PATCH 08/20] Add version on egg --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 11bca9a62460f..7bc0799a1be10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] requires = [ - "starlette @ git+ssh://git@github.com/encode/starlette@master#egg=starlette", + "starlette @ git+ssh://git@github.com/encode/starlette@master#egg=starlette-0.18.0", "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" From facb6d8e25e4422fb97bb46f1dc911d90f3a64b7 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:29:25 +0100 Subject: [PATCH 09/20] Try with only link --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 7bc0799a1be10..bf84faa26ecc2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] requires = [ - "starlette @ git+ssh://git@github.com/encode/starlette@master#egg=starlette-0.18.0", + "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" From a659c2b9920fba1555ed6d41b8939d57f39f9d83 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:31:08 +0100 Subject: [PATCH 10/20] Add starlette back --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index bf84faa26ecc2..eb2f17c4c4f78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] requires = [ - "git+https://github.com/encode/starlette.git#master", + "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" From 4305a0c5f9bcd1ece36ed858960326bb06d0627a Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:33:01 +0100 Subject: [PATCH 11/20] Remove HTTPException --- fastapi/exceptions.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index f4a837bb4e2bf..0cc8efceba67e 100644 --- a/fastapi/exceptions.py +++ b/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, - 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 RequestErrorModel: Type[BaseModel] = create_model("Request") WebSocketErrorModel: Type[BaseModel] = create_model("WebSocket") From b19e84832c95928870a2459e451bf4e2d39f4c64 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Jan 2022 20:35:26 +0100 Subject: [PATCH 12/20] Add noqa on reimport --- fastapi/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index 0cc8efceba67e..628c2b742cf16 100644 --- a/fastapi/exceptions.py +++ b/fastapi/exceptions.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, ValidationError, create_model from pydantic.error_wrappers import ErrorList -from starlette.exceptions import HTTPException as HTTPException +from starlette.exceptions import HTTPException as HTTPException # noqa RequestErrorModel: Type[BaseModel] = create_model("Request") WebSocketErrorModel: Type[BaseModel] = create_model("WebSocket") From 1b3342b427da5c7023d66b9d53d1a65d45ee7e06 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 28 Jan 2022 18:02:55 +0100 Subject: [PATCH 13/20] Empty commit From 09c70c43c79cc4820f4359f58c7ad68d2cad797d Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 30 Jan 2022 17:13:56 +0100 Subject: [PATCH 14/20] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index eb2f17c4c4f78..b1f6004f05840 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,7 +75,7 @@ doc = [ "mdx-include >=1.4.1,<2.0.0", "mkdocs-markdownextradata-plugin >=0.1.7,<0.3.0", "typer-cli >=0.0.12,<0.0.13", - "pyyaml >=5.3.1,<6.0.0", + "pyyaml >=5.3.1,<6.0.0" ] dev = [ "python-jose[cryptography] >=3.3.0,<4.0.0", From 880dd0d2192b8677b274a3ccc6823c76380ad54c Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 1 Feb 2022 00:31:23 +0100 Subject: [PATCH 15/20] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b1f6004f05840..357338d476de0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -129,7 +129,7 @@ check_untyped_defs = true [tool.pytest.ini_options] addopts = [ - "--strict-config", + "--strict-config", "--strict-markers" ] xfail_strict = true From c48a3993572fea39da012e8b5cf9e166a2364645 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 1 Feb 2022 15:46:19 +0100 Subject: [PATCH 16/20] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 357338d476de0..f513254be742d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -130,7 +130,7 @@ check_untyped_defs = true [tool.pytest.ini_options] addopts = [ "--strict-config", - "--strict-markers" + "--strict-markers" ] xfail_strict = true junit_family = "xunit2" From 7b519b80bf35aba73a39326b82d1b8be8ae45beb Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 2 Feb 2022 00:03:09 +0100 Subject: [PATCH 17/20] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f513254be742d..e10b424b67b4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -130,7 +130,7 @@ check_untyped_defs = true [tool.pytest.ini_options] addopts = [ "--strict-config", - "--strict-markers" + "--strict-markers", ] xfail_strict = true junit_family = "xunit2" From d382aa5e6b54eb220f2d574c09f57520ddd08d7c Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 16 Mar 2022 17:19:34 +0100 Subject: [PATCH 18/20] Ignore WSGIMiddleware deprecation warning on tests --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e10b424b67b4a..baff85ebdb5a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] requires = [ - "starlette @ git+https://github.com/encode/starlette.git#master", + "starlette==0.19.0", "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" @@ -138,4 +138,5 @@ filterwarnings = [ "error", # TODO: needed by asyncio in Python 3.9.7 https://bugs.python.org/issue45097, try to remove on 3.9.8 'ignore:The loop argument is deprecated since Python 3\.8, and scheduled for removal in Python 3\.10:DeprecationWarning:asyncio', + 'ignore:starlette.middleware.wsgi is deprecated and will be removed in a future release\..*:DeprecationWarning:starlette' ] From b6003a7c8b7bbd31c6ad03083fe563c5b3fc41bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 9 May 2022 12:22:04 -0500 Subject: [PATCH 19/20] =?UTF-8?q?=F0=9F=94=A7=20Fix=20TOML=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 72a7719e158e1..fc803f8fc5da7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -140,7 +140,7 @@ filterwarnings = [ "error", # TODO: needed by asyncio in Python 3.9.7 https://bugs.python.org/issue45097, try to remove on 3.9.8 'ignore:The loop argument is deprecated since Python 3\.8, and scheduled for removal in Python 3\.10:DeprecationWarning:asyncio', - 'ignore:starlette.middleware.wsgi is deprecated and will be removed in a future release\..*:DeprecationWarning:starlette' + 'ignore:starlette.middleware.wsgi is deprecated and will be removed in a future release\..*:DeprecationWarning:starlette', # TODO: remove after dropping support for Python 3.6 'ignore:Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.:UserWarning:jose', ] From fd46adb8b13b52f4b11479391364b7e7ed89f47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 9 May 2022 12:30:07 -0500 Subject: [PATCH 20/20] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Re-implement=20FastA?= =?UTF-8?q?PI's=20own=20HTTPException=20accepting=20a=20dict=20detail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/exceptions.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index 628c2b742cf16..fcb7187488f02 100644 --- a/fastapi/exceptions.py +++ b/fastapi/exceptions.py @@ -1,8 +1,21 @@ -from typing import Any, Sequence, Type +from typing import Any, Dict, Optional, Sequence, Type from pydantic import BaseModel, ValidationError, create_model from pydantic.error_wrappers import ErrorList -from starlette.exceptions import HTTPException as HTTPException # noqa +from starlette.exceptions import HTTPException as StarletteHTTPException + + +class HTTPException(StarletteHTTPException): + def __init__( + self, + status_code: int, + detail: Any = None, + headers: Optional[Dict[str, Any]] = None, + ) -> None: + super().__init__( + status_code=status_code, detail=detail, headers=headers # type: ignore + ) + RequestErrorModel: Type[BaseModel] = create_model("Request") WebSocketErrorModel: Type[BaseModel] = create_model("WebSocket")