Skip to content

Commit

Permalink
🚨 Fix complaints for flake8-bugbear and flake8-simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Sep 4, 2024
1 parent 6f220f7 commit 06a113d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions fastapi_cdn_host/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
65 changes: 30 additions & 35 deletions fastapi_cdn_host/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/no_need_to_patch/test_no_openapi_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 06a113d

Please sign in to comment.