From 06a113da301b79651b4be3b51572445c806c89b9 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Wed, 4 Sep 2024 21:22:38 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20Fix=20complaints=20for=20flake8-?= =?UTF-8?q?bugbear=20and=20flake8-simplify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi_cdn_host/cli.py | 4 +- fastapi_cdn_host/client.py | 65 +++++++++---------- pyproject.toml | 6 +- ...est_private_cdn_with_default_asset_path.py | 7 +- tests/no_need_to_patch/test_no_openapi_url.py | 2 +- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/fastapi_cdn_host/cli.py b/fastapi_cdn_host/cli.py index ae24fba..ac69c0e 100644 --- a/fastapi_cdn_host/cli.py +++ b/fastapi_cdn_host/cli.py @@ -90,13 +90,13 @@ async def play(progress, task) -> None: cost = seconds * expected quick = int(total * threshold) delay = cost / quick - for i in range(quick): + for _ in range(quick): await anyio.sleep(delay) progress.advance(task) cost = seconds - cost slow = total - quick delay = cost / slow - for i in range(slow): + for _ in range(slow): await anyio.sleep(delay) progress.advance(task) diff --git a/fastapi_cdn_host/client.py b/fastapi_cdn_host/client.py index 7bd39e3..55e1306 100644 --- a/fastapi_cdn_host/client.py +++ b/fastapi_cdn_host/client.py @@ -202,19 +202,19 @@ async def bulk_fetch( ) -> Union[List[str], List[bytes]]: total = len(urls) results = [None] * total - async with httpx.AsyncClient( - timeout=total_seconds, follow_redirects=True - ) as client: - async with anyio.create_task_group() as tg: - for i, url in enumerate(urls): - tg.start_soon(cls.fetch, client, url, results, i, get_content) - if not get_content: - threshold = 1 if return_first_completed else total - 1 - for _ in range(math.ceil(total_seconds / wait_seconds)): - await anyio.sleep(wait_seconds) - if sum(r is not None for r in results) >= threshold: - tg.cancel_scope.cancel() - break + async with ( + httpx.AsyncClient(timeout=total_seconds, follow_redirects=True) as client, + anyio.create_task_group() as tg, + ): + for i, url in enumerate(urls): + tg.start_soon(cls.fetch, client, url, results, i, get_content) + if not get_content: + threshold = 1 if return_first_completed else total - 1 + for _ in range(math.ceil(total_seconds / wait_seconds)): + await anyio.sleep(wait_seconds) + if sum(r is not None for r in results) >= threshold: + tg.cancel_scope.cancel() + break if get_content: return [i or b"" for i in results] return [url for url, res in zip(urls, results) if res is not None] @@ -291,10 +291,7 @@ def fill_root_path(urls, root): @classmethod def build_swagger_path(cls, asset_path: Union[str, Tuple[str, str]]) -> str: - if isinstance(asset_path, str): - path_fmt = asset_path - else: - path_fmt = asset_path[0] + path_fmt = asset_path if isinstance(asset_path, str) else asset_path[0] version = cls.swagger_ui_version # unpkg/jsdelivr: 'swagger-ui@5/xxx' if "@" not in path_fmt: # cdnjs/bootcdn/...: 'swagger-ui/5.17.14/xxx' version = cls.swagger_ui_full_version @@ -479,20 +476,20 @@ def auto_mount_static( def _generate_asset_urls_from_local_files( self, gs, mount=None, app=None, static_root=None, favicon=None ) -> AssetUrl: - if mount: - uri_path = mount.path - else: - uri_path = self.auto_mount_static(app, static_root) + uri_path = mount.path if mount else self.auto_mount_static(app, static_root) css_file = self.get_latest_one(gs) - if _js := list(static_root.rglob("swagger-ui*.js")): - js_file = self.get_latest_one(_js) - else: - js_file = css_file.with_name(CdnHostBuilder.swagger_files["js"]) + js_file = ( + self.get_latest_one(_js) + if (_js := list(static_root.rglob("swagger-ui*.js"))) + else (css_file.with_name(CdnHostBuilder.swagger_files["js"])) + ) redoc_name = CdnHostBuilder.redoc_file - if _redoc := list(static_root.rglob(redoc_name)): - redoc_file = self.get_latest_one(_redoc) - else: - redoc_file = css_file.with_name(redoc_name) + + redoc_file = ( + self.get_latest_one(_redoc) + if (_redoc := list(static_root.rglob(redoc_name))) + else (css_file.with_name(redoc_name)) + ) css = self.file_to_uri(css_file, static_root, uri_path) js = self.file_to_uri(js_file, static_root, uri_path) @@ -555,9 +552,7 @@ def monkey_patch_for_docs_ui( route_index: Dict[str, int] = { getattr(route, "path", ""): index for index, route in enumerate(app.routes) } - if docs_url: - if (index := route_index.get(docs_url)) is not None: - DocsBuilder(index).update_docs_entrypoint(urls, app, docs_url, lock=lock) - if redoc_url: - if (index := route_index.get(redoc_url)) is not None: - DocsBuilder(index).update_redoc_entrypoint(urls, app, redoc_url, lock=lock) + if docs_url and (index := route_index.get(docs_url)) is not None: + DocsBuilder(index).update_docs_entrypoint(urls, app, docs_url, lock=lock) + if redoc_url and (index := route_index.get(redoc_url)) is not None: + DocsBuilder(index).update_redoc_entrypoint(urls, app, redoc_url, lock=lock) diff --git a/pyproject.toml b/pyproject.toml index 7cb3ad1..746fa7e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,11 @@ explicit_package_bases = true check_untyped_defs = true [tool.ruff.lint] -extend-select = ["I"] +extend-select = [ + "I", # isort + "B", # flake8-bugbear + "SIM", # flake8-simplify +] [tool.ruff.lint.per-file-ignores] "test_*.py" = ["E501"] diff --git a/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py b/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py index 62fff82..407abef 100644 --- a/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py +++ b/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py @@ -29,9 +29,10 @@ async def test_docs(client: AsyncClient): # nosec css_url = MY_CDN + "/swagger-ui-dist@5/swagger-ui.css" js_url = MY_CDN + "/swagger-ui-dist@5/swagger-ui-bundle.js" redoc_url = MY_CDN + "/redoc@next/bundles/redoc.standalone.js" - CdnHostBuilder.build_swagger_path( - "/swagger-ui-dist@{version}/swagger-ui.css" - ) == "/swagger-ui-dist@5/swagger-ui.css" + assert ( + CdnHostBuilder.build_swagger_path("/swagger-ui-dist@{version}/swagger-ui.css") + == "/swagger-ui-dist@5/swagger-ui.css" + ) favicon_url = MY_CDN + "/favicon.ico" with UvicornServer("media_server:app", port=PORT).run_in_thread(): response = await client.get("/docs") diff --git a/tests/no_need_to_patch/test_no_openapi_url.py b/tests/no_need_to_patch/test_no_openapi_url.py index ac00019..75d832b 100644 --- a/tests/no_need_to_patch/test_no_openapi_url.py +++ b/tests/no_need_to_patch/test_no_openapi_url.py @@ -35,4 +35,4 @@ def test_log(caplog): def test_get_latest_one(tmp_path): (a := tmp_path / "a.txt").touch() (b := tmp_path / "b.txt").touch() - StaticBuilder.get_latest_one([a, b]) == b + assert StaticBuilder.get_latest_one([a, b]) == b