From bbe46ef7dcc57d6467b23947cafc0d6542ec74e1 Mon Sep 17 00:00:00 2001 From: Abigail Hartman Date: Mon, 15 Apr 2024 17:00:08 -0700 Subject: [PATCH] Use ruff instead of black for performance boost --- .github/workflows/ruff.yml | 8 ++++++++ .pre-commit-config.yaml | 17 ++++++++--------- app.py | 18 ++++++------------ backend/history/cosmosdbservice.py | 12 ++++++------ poetry.lock | 28 +++++++++++++++++++++++++++- pyproject.toml | 4 ++++ 6 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/ruff.yml diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml new file mode 100644 index 0000000000..b6690e058c --- /dev/null +++ b/.github/workflows/ruff.yml @@ -0,0 +1,8 @@ +name: Ruff +on: [push, pull_request] +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f06714e6f7..cc8dbcc626 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,11 +1,10 @@ repos: - # Using this mirror lets us use mypyc-compiled black, which is about 2x faster - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.3.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.3.7 hooks: - - id: black - # It is recommended to specify the latest version of Python - # supported by your project here, or alternatively use - # pre-commit's default_language_version, see - # https://pre-commit.com/#top_level-default_language_version - language_version: python3.10 \ No newline at end of file + # Run the linter. + - id: ruff + args: [ --fix ] + # Run the formatter. + - id: ruff-format \ No newline at end of file diff --git a/app.py b/app.py index 82078d3052..573632f767 100644 --- a/app.py +++ b/app.py @@ -1078,14 +1078,10 @@ async def delete_conversation(): raise Exception("CosmosDB is not configured or not working") ## delete the conversation messages from cosmos first - deleted_messages = await cosmos_conversation_client.delete_messages( - conversation_id, user_id - ) + await cosmos_conversation_client.delete_messages(conversation_id, user_id) ## Now delete the conversation - deleted_conversation = await cosmos_conversation_client.delete_conversation( - user_id, conversation_id - ) + await cosmos_conversation_client.delete_conversation(user_id, conversation_id) await cosmos_conversation_client.cosmosdb_client.close() @@ -1246,12 +1242,12 @@ async def delete_all_conversations(): # delete each conversation for conversation in conversations: ## delete the conversation messages from cosmos first - deleted_messages = await cosmos_conversation_client.delete_messages( + await cosmos_conversation_client.delete_messages( conversation["id"], user_id ) ## Now delete the conversation - deleted_conversation = await cosmos_conversation_client.delete_conversation( + await cosmos_conversation_client.delete_conversation( user_id, conversation["id"] ) await cosmos_conversation_client.cosmosdb_client.close() @@ -1289,9 +1285,7 @@ async def clear_messages(): raise Exception("CosmosDB is not configured or not working") ## delete the conversation messages from cosmos - deleted_messages = await cosmos_conversation_client.delete_messages( - conversation_id, user_id - ) + await cosmos_conversation_client.delete_messages(conversation_id, user_id) return ( jsonify( @@ -1367,7 +1361,7 @@ async def generate_title(conversation_messages): title = json.loads(response.choices[0].message.content)["title"] return title - except Exception as e: + except Exception: return messages[-2]["content"] diff --git a/backend/history/cosmosdbservice.py b/backend/history/cosmosdbservice.py index dd66ee1264..b86201fce4 100644 --- a/backend/history/cosmosdbservice.py +++ b/backend/history/cosmosdbservice.py @@ -51,16 +51,16 @@ async def ensure(self): return False, "CosmosDB client not initialized correctly" try: - database_info = await self.database_client.read() - except: + await self.database_client.read() + except Exception: return ( False, f"CosmosDB database {self.database_name} on account {self.cosmosdb_endpoint} not found", ) try: - container_info = await self.container_client.read() - except: + await self.container_client.read() + except Exception: return False, f"CosmosDB container {self.container_name} not found" return True, "CosmosDB client initialized successfully" @@ -131,7 +131,7 @@ async def get_conversation(self, user_id, conversation_id): {"name": "@conversationId", "value": conversation_id}, {"name": "@userId", "value": user_id}, ] - query = f"SELECT * FROM c where c.id = @conversationId and c.type='conversation' and c.userId = @userId" + query = "SELECT * FROM c where c.id = @conversationId and c.type='conversation' and c.userId = @userId" conversations = [] async for item in self.container_client.query_items( query=query, parameters=parameters @@ -187,7 +187,7 @@ async def get_messages(self, user_id, conversation_id): {"name": "@conversationId", "value": conversation_id}, {"name": "@userId", "value": user_id}, ] - query = f"SELECT * FROM c WHERE c.conversationId = @conversationId AND c.type='message' AND c.userId = @userId ORDER BY c.timestamp ASC" + query = "SELECT * FROM c WHERE c.conversationId = @conversationId AND c.type='message' AND c.userId = @userId ORDER BY c.timestamp ASC" messages = [] async for item in self.container_client.query_items( query=query, parameters=parameters diff --git a/poetry.lock b/poetry.lock index 0cf6003ab4..7e146bec2d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2063,6 +2063,32 @@ requests = ">=2.0.0" [package.extras] rsa = ["oauthlib[signedtoken] (>=3.0.0)"] +[[package]] +name = "ruff" +version = "0.3.7" +description = "An extremely fast Python linter and code formatter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.3.7-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:0e8377cccb2f07abd25e84fc5b2cbe48eeb0fea9f1719cad7caedb061d70e5ce"}, + {file = "ruff-0.3.7-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:15a4d1cc1e64e556fa0d67bfd388fed416b7f3b26d5d1c3e7d192c897e39ba4b"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d28bdf3d7dc71dd46929fafeec98ba89b7c3550c3f0978e36389b5631b793663"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:379b67d4f49774ba679593b232dcd90d9e10f04d96e3c8ce4a28037ae473f7bb"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c060aea8ad5ef21cdfbbe05475ab5104ce7827b639a78dd55383a6e9895b7c51"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:ebf8f615dde968272d70502c083ebf963b6781aacd3079081e03b32adfe4d58a"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:d48098bd8f5c38897b03604f5428901b65e3c97d40b3952e38637b5404b739a2"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:da8a4fda219bf9024692b1bc68c9cff4b80507879ada8769dc7e985755d662ea"}, + {file = "ruff-0.3.7-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c44e0149f1d8b48c4d5c33d88c677a4aa22fd09b1683d6a7ff55b816b5d074f"}, + {file = "ruff-0.3.7-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:3050ec0af72b709a62ecc2aca941b9cd479a7bf2b36cc4562f0033d688e44fa1"}, + {file = "ruff-0.3.7-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:a29cc38e4c1ab00da18a3f6777f8b50099d73326981bb7d182e54a9a21bb4ff7"}, + {file = "ruff-0.3.7-py3-none-musllinux_1_2_i686.whl", hash = "sha256:5b15cc59c19edca917f51b1956637db47e200b0fc5e6e1878233d3a938384b0b"}, + {file = "ruff-0.3.7-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:e491045781b1e38b72c91247cf4634f040f8d0cb3e6d3d64d38dcf43616650b4"}, + {file = "ruff-0.3.7-py3-none-win32.whl", hash = "sha256:bc931de87593d64fad3a22e201e55ad76271f1d5bfc44e1a1887edd0903c7d9f"}, + {file = "ruff-0.3.7-py3-none-win_amd64.whl", hash = "sha256:5ef0e501e1e39f35e03c2acb1d1238c595b8bb36cf7a170e7c1df1b73da00e74"}, + {file = "ruff-0.3.7-py3-none-win_arm64.whl", hash = "sha256:789e144f6dc7019d1f92a812891c645274ed08af6037d11fc65fcbc183b7d59f"}, + {file = "ruff-0.3.7.tar.gz", hash = "sha256:d5c1aebee5162c2226784800ae031f660c350e7a3402c4d1f8ea4e97e232e3ba"}, +] + [[package]] name = "setuptools" version = "69.2.0" @@ -2521,4 +2547,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "fb8a94cb79f48ddad3e1206dbcfb6446acb8a9480370cfaf94e22a1dcddf4829" +content-hash = "7ee5c9222525cdb1b4099b590200cee99a9b7ae1a937122638c51750a4ab7fee" diff --git a/pyproject.toml b/pyproject.toml index 616d9af0ec..a733e45be6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,8 +31,12 @@ pytest = "7.4.0" pytest-asyncio = "0.23.2" chardet = "5.2.0" pre-commit = "^3.7.0" +ruff = "^0.3.7" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" + +[tool.ruff] +include = ["app.py", "backend/*.py", "backend/**/*.py"]