From d35a76368e8545011a95c7b5e94f1ee1ebd28001 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 26 Dec 2024 17:44:37 -0500 Subject: [PATCH 1/3] Enable Ruff PERF --- pyproject.toml | 9 +++++++-- scripts/create_baseline_stubs.py | 13 +++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 501066318b0b..a0f6332a164e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ select = [ "B", # flake8-bugbear "FA", # flake8-future-annotations "I", # isort + "PERF", # Perflint "RUF", # Ruff-specific and unused-noqa "UP", # pyupgrade # Flake8 base rules @@ -86,11 +87,15 @@ ignore = [ ### # Rules we don't want or don't agree with ### - # Slower and more verbose https://github.com/astral-sh/ruff/issues/7871 - "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` # Used for direct, non-subclass type comparison, for example: `type(val) is str` # see https://github.com/astral-sh/ruff/issues/6465 "E721", # Do not compare types, use `isinstance()` + # Can easily produce false-positives. Worth checking but don't block CI. + # Anyway Python 3.11 introduced "zero cost" exception handling for the non-error path. + # Our tests & scripts run on modern Python versions. + "PERF203", # try-except within a loop incurs performance overhead + # Slower and more verbose https://github.com/astral-sh/ruff/issues/7871 + "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` ### # False-positives, but already checked by type-checkers ### diff --git a/scripts/create_baseline_stubs.py b/scripts/create_baseline_stubs.py index 69f5d2b307e3..4c01b9d39991 100755 --- a/scripts/create_baseline_stubs.py +++ b/scripts/create_baseline_stubs.py @@ -19,6 +19,7 @@ import sys import urllib.parse from importlib.metadata import distribution +from itertools import chain import aiohttp import termcolor @@ -90,13 +91,13 @@ async def get_upstream_repo_url(project: str) -> str | None: # Order the project URLs so that we put the ones # that are most likely to point to the source code first - urls_to_check: list[str] = [] url_names_probably_pointing_to_source = ("Source", "Repository", "Homepage") - for url_name in url_names_probably_pointing_to_source: - if url := project_urls.get(url_name): - urls_to_check.append(url) - urls_to_check.extend( - url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source + project_urls_probably_pointing_to_source = ( + project_urls.get(url_name) for url_name in url_names_probably_pointing_to_source + ) + urls_to_check = chain( + (url for url in project_urls_probably_pointing_to_source if url), + (url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source), ) for url in urls_to_check: From 47a2432ebec22a42896f65b53750481f729bd4f7 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 26 Dec 2024 18:16:20 -0500 Subject: [PATCH 2/3] Actually make it faster --- scripts/create_baseline_stubs.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/create_baseline_stubs.py b/scripts/create_baseline_stubs.py index 4c01b9d39991..ee5bc2375998 100755 --- a/scripts/create_baseline_stubs.py +++ b/scripts/create_baseline_stubs.py @@ -19,7 +19,6 @@ import sys import urllib.parse from importlib.metadata import distribution -from itertools import chain import aiohttp import termcolor @@ -92,12 +91,13 @@ async def get_upstream_repo_url(project: str) -> str | None: # Order the project URLs so that we put the ones # that are most likely to point to the source code first url_names_probably_pointing_to_source = ("Source", "Repository", "Homepage") - project_urls_probably_pointing_to_source = ( - project_urls.get(url_name) for url_name in url_names_probably_pointing_to_source - ) - urls_to_check = chain( - (url for url in project_urls_probably_pointing_to_source if url), - (url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source), + + urls_to_check: list[str] = [] + for url_name in url_names_probably_pointing_to_source: + if url := project_urls.get(url_name): + urls_to_check.append(url) # noqa: PERF401 # False-positive with walrus, this code is the fastest form + urls_to_check.extend( + [url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source] ) for url in urls_to_check: From 7e4df9cea31ab1569db0fc66e1e1faa41e3685cb Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 26 Dec 2024 18:41:27 -0500 Subject: [PATCH 3/3] Just wrap the walrus in parenthesis --- scripts/create_baseline_stubs.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/scripts/create_baseline_stubs.py b/scripts/create_baseline_stubs.py index ee5bc2375998..c0b986d52eea 100755 --- a/scripts/create_baseline_stubs.py +++ b/scripts/create_baseline_stubs.py @@ -91,14 +91,9 @@ async def get_upstream_repo_url(project: str) -> str | None: # Order the project URLs so that we put the ones # that are most likely to point to the source code first url_names_probably_pointing_to_source = ("Source", "Repository", "Homepage") - - urls_to_check: list[str] = [] - for url_name in url_names_probably_pointing_to_source: - if url := project_urls.get(url_name): - urls_to_check.append(url) # noqa: PERF401 # False-positive with walrus, this code is the fastest form - urls_to_check.extend( - [url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source] - ) + urls_to_check = [url for url_name in url_names_probably_pointing_to_source if (url := project_urls.get(url_name))] + [ + url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source + ] for url in urls_to_check: # Remove `www.`; replace `http://` with `https://`