From 798e59eab9bbbc19df1285210b184cbaabf1227b Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Mon, 2 Dec 2024 14:38:36 -0800 Subject: [PATCH] chore/ruff formatting (#128) Why === Breaking out ruff changes from #123 What changed ============ Separation of concerns, increasing revertability Test plan ========= CI --- .github/workflows/ci.yml | 2 +- Makefile | 5 +-- pyproject.toml | 2 +- replit_river/codegen/client.py | 20 +++++++----- replit_river/codegen/server.py | 12 +++---- scripts/format.sh | 9 ------ scripts/lint.sh | 11 ------- tests/river_fixtures/clientserver.py | 18 +++++++---- tests/test_handshake.py | 7 ++-- uv.lock | 48 ---------------------------- 10 files changed, 39 insertions(+), 95 deletions(-) delete mode 100755 scripts/format.sh delete mode 100755 scripts/lint.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a90ac4..f5dc2b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: - name: Format check run: | - uv run black --check . + uv run ruff format --check . - name: Lint check run: | diff --git a/Makefile b/Makefile index f2fa8a8..7b15491 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ lint: - uv run black --check . + uv run ruff format --check . uv run ruff check . uv run mypy . uv run pyright-python . + uv run deptry . format: - uv run black . + uv run ruff format . uv run ruff check . --fix diff --git a/pyproject.toml b/pyproject.toml index 1e18ae4..11afef7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,6 @@ classifiers = [ dependencies = [ "pydantic==2.9.2", "aiochannel>=1.2.1", - "black>=23.11,<25.0", "grpcio-tools>=1.59.3", "grpcio>=1.59.3", "msgpack-types>=0.3.0", @@ -63,6 +62,7 @@ disallow_untyped_defs = true warn_return_any = true [tool.pytest.ini_options] +asyncio_default_fixture_loop_scope = "function" asyncio_mode = "auto" # auto-detect async tests/fixtures addopts = "--tb=short" env = [ diff --git a/replit_river/codegen/client.py b/replit_river/codegen/client.py index a8e3732..8a5e4b0 100644 --- a/replit_river/codegen/client.py +++ b/replit_river/codegen/client.py @@ -1,5 +1,6 @@ import json import re +import subprocess from pathlib import Path from textwrap import dedent from typing import ( @@ -16,7 +17,6 @@ cast, ) -import black from pydantic import BaseModel, Field, RootModel from replit_river.codegen.format import reindent @@ -831,9 +831,14 @@ def __init__(self, client: river.Client[Any]): exclude_none=True, ) """ - if isinstance( - procedure.input, RiverConcreteType - ) and procedure.input.type not in ["object", "array"]: + if ( + ( + isinstance(procedure.input, RiverConcreteType) + and procedure.input.type not in ["object", "array"] + ) + or isinstance(procedure.input, RiverNotType) + or procedure.input is None + ): render_input_method = "lambda x: x" assert ( @@ -1077,11 +1082,10 @@ def schema_to_river_client_codegen( module_path.parent.mkdir(mode=0o755, parents=True, exist_ok=True) with open(module_path, "w") as f: try: - f.write( - black.format_str( - contents, mode=black.FileMode(string_normalization=False) - ) + popen = subprocess.Popen( + ["ruff", "format", "-"], stdin=subprocess.PIPE, stdout=f ) + popen.communicate(contents.encode()) except: f.write(contents) raise diff --git a/replit_river/codegen/server.py b/replit_river/codegen/server.py index 0d0501e..dfebe73 100644 --- a/replit_river/codegen/server.py +++ b/replit_river/codegen/server.py @@ -1,10 +1,10 @@ import collections import os.path +import subprocess import tempfile from textwrap import dedent from typing import DefaultDict, List, Sequence -import black import grpc_tools # type: ignore from google.protobuf import descriptor_pb2 from google.protobuf.descriptor import FieldDescriptor @@ -412,11 +412,11 @@ def proto_to_river_server_codegen( with open(descriptor_path, "rb") as f: fds.ParseFromString(f.read()) pb_module_name = os.path.splitext(os.path.basename(proto_path))[0] - contents = black.format_str( - "\n".join(generate_river_module(module_name, pb_module_name, fds)), - mode=black.FileMode(string_normalization=False), - ) + contents = "\n".join(generate_river_module(module_name, pb_module_name, fds)) os.makedirs(target_directory, exist_ok=True) output_path = f"{target_directory}/{pb_module_name}_river.py" with open(output_path, "w") as f: - f.write(contents) + popen = subprocess.Popen( + ["ruff", "format", "-"], stdin=subprocess.PIPE, stdout=f + ) + popen.communicate(contents.encode()) diff --git a/scripts/format.sh b/scripts/format.sh deleted file mode 100755 index 43d90aa..0000000 --- a/scripts/format.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -set -ex - -black . - -# Some of the ruff fixes go beyond "formatting". -# Consider switching to `ruff format` if it becomes available. -ruff check --fix --show-fixes . diff --git a/scripts/lint.sh b/scripts/lint.sh deleted file mode 100755 index 3116f09..0000000 --- a/scripts/lint.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -set -ex - -black . - -# Some of the ruff fixes go beyond "formatting". -# Consider switching to `ruff format` if it becomes available. -ruff check --fix --show-fixes . -mypy . -deptry . diff --git a/tests/river_fixtures/clientserver.py b/tests/river_fixtures/clientserver.py index b54489b..7c9d317 100644 --- a/tests/river_fixtures/clientserver.py +++ b/tests/river_fixtures/clientserver.py @@ -38,14 +38,18 @@ async def client( transport_options: TransportOptions, no_logging_error: NoErrors, ) -> AsyncGenerator[Client, None]: - async def websocket_uri_factory() -> UriAndMetadata[None]: - return { - "uri": "ws://localhost:8765", - "metadata": None, - } - try: - async with serve(server.serve, "localhost", 8765): + async with serve(server.serve, "127.0.0.1") as binding: + sockets = list(binding.sockets) + assert len(sockets) == 1, "Too many sockets!" + socket = sockets[0] + + async def websocket_uri_factory() -> UriAndMetadata[None]: + return { + "uri": "ws://%s:%d" % socket.getsockname(), + "metadata": None, + } + client: Client[Literal[None]] = Client[None]( uri_and_metadata_factory=websocket_uri_factory, client_id="test_client", diff --git a/tests/test_handshake.py b/tests/test_handshake.py index bd4e4c4..3c872d2 100644 --- a/tests/test_handshake.py +++ b/tests/test_handshake.py @@ -17,9 +17,12 @@ def transport_options() -> TransportOptions: @pytest.mark.asyncio @pytest.mark.parametrize("handlers", [{}]) async def test_handshake_timeout(server: Server) -> None: - async with serve(server.serve, "localhost", 8765): + async with serve(server.serve, "127.0.0.1") as binding: + sockets = list(binding.sockets) + assert len(sockets) == 1, "Too many sockets!" + socket = sockets[0] start = time() - ws = await websockets.connect("ws://localhost:8765") + ws = await websockets.connect("ws://%s:%d" % socket.getsockname()) with pytest.raises(ConnectionClosedOK): await ws.recv() diff = time() - start diff --git a/uv.lock b/uv.lock index 1eb9451..2e9aa67 100644 --- a/uv.lock +++ b/uv.lock @@ -23,34 +23,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/78/b6/6307fbef88d9b5ee7421e68d78a9f162e0da4900bc5f5793f6d3d0e34fb8/annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53", size = 13643 }, ] -[[package]] -name = "black" -version = "24.10.0" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "click" }, - { name = "mypy-extensions" }, - { name = "packaging" }, - { name = "pathspec" }, - { name = "platformdirs" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/d8/0d/cc2fb42b8c50d80143221515dd7e4766995bd07c56c9a3ed30baf080b6dc/black-24.10.0.tar.gz", hash = "sha256:846ea64c97afe3bc677b761787993be4991810ecc7a4a937816dd6bddedc4875", size = 645813 } -wheels = [ - { url = "https://files.pythonhosted.org/packages/c2/cc/7496bb63a9b06a954d3d0ac9fe7a73f3bf1cd92d7a58877c27f4ad1e9d41/black-24.10.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:5a2221696a8224e335c28816a9d331a6c2ae15a2ee34ec857dcf3e45dbfa99ad", size = 1607468 }, - { url = "https://files.pythonhosted.org/packages/2b/e3/69a738fb5ba18b5422f50b4f143544c664d7da40f09c13969b2fd52900e0/black-24.10.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f9da3333530dbcecc1be13e69c250ed8dfa67f43c4005fb537bb426e19200d50", size = 1437270 }, - { url = "https://files.pythonhosted.org/packages/c9/9b/2db8045b45844665c720dcfe292fdaf2e49825810c0103e1191515fc101a/black-24.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4007b1393d902b48b36958a216c20c4482f601569d19ed1df294a496eb366392", size = 1737061 }, - { url = "https://files.pythonhosted.org/packages/a3/95/17d4a09a5be5f8c65aa4a361444d95edc45def0de887810f508d3f65db7a/black-24.10.0-cp311-cp311-win_amd64.whl", hash = "sha256:394d4ddc64782e51153eadcaaca95144ac4c35e27ef9b0a42e121ae7e57a9175", size = 1423293 }, - { url = "https://files.pythonhosted.org/packages/90/04/bf74c71f592bcd761610bbf67e23e6a3cff824780761f536512437f1e655/black-24.10.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:b5e39e0fae001df40f95bd8cc36b9165c5e2ea88900167bddf258bacef9bbdc3", size = 1644256 }, - { url = "https://files.pythonhosted.org/packages/4c/ea/a77bab4cf1887f4b2e0bce5516ea0b3ff7d04ba96af21d65024629afedb6/black-24.10.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:d37d422772111794b26757c5b55a3eade028aa3fde43121ab7b673d050949d65", size = 1448534 }, - { url = "https://files.pythonhosted.org/packages/4e/3e/443ef8bc1fbda78e61f79157f303893f3fddf19ca3c8989b163eb3469a12/black-24.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:14b3502784f09ce2443830e3133dacf2c0110d45191ed470ecb04d0f5f6fcb0f", size = 1761892 }, - { url = "https://files.pythonhosted.org/packages/52/93/eac95ff229049a6901bc84fec6908a5124b8a0b7c26ea766b3b8a5debd22/black-24.10.0-cp312-cp312-win_amd64.whl", hash = "sha256:30d2c30dc5139211dda799758559d1b049f7f14c580c409d6ad925b74a4208a8", size = 1434796 }, - { url = "https://files.pythonhosted.org/packages/d0/a0/a993f58d4ecfba035e61fca4e9f64a2ecae838fc9f33ab798c62173ed75c/black-24.10.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:1cbacacb19e922a1d75ef2b6ccaefcd6e93a2c05ede32f06a21386a04cedb981", size = 1643986 }, - { url = "https://files.pythonhosted.org/packages/37/d5/602d0ef5dfcace3fb4f79c436762f130abd9ee8d950fa2abdbf8bbc555e0/black-24.10.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:1f93102e0c5bb3907451063e08b9876dbeac810e7da5a8bfb7aeb5a9ef89066b", size = 1448085 }, - { url = "https://files.pythonhosted.org/packages/47/6d/a3a239e938960df1a662b93d6230d4f3e9b4a22982d060fc38c42f45a56b/black-24.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ddacb691cdcdf77b96f549cf9591701d8db36b2f19519373d60d31746068dbf2", size = 1760928 }, - { url = "https://files.pythonhosted.org/packages/dd/cf/af018e13b0eddfb434df4d9cd1b2b7892bab119f7a20123e93f6910982e8/black-24.10.0-cp313-cp313-win_amd64.whl", hash = "sha256:680359d932801c76d2e9c9068d05c6b107f2584b2a5b88831c83962eb9984c1b", size = 1436875 }, - { url = "https://files.pythonhosted.org/packages/8d/a7/4b27c50537ebca8bec139b872861f9d2bf501c5ec51fcf897cb924d9e264/black-24.10.0-py3-none-any.whl", hash = "sha256:3bb2b7a1f7b685f85b11fed1ef10f8a9148bceb49853e47a294a3dd963c1dd7d", size = 206898 }, -] - [[package]] name = "click" version = "8.1.7" @@ -422,24 +394,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/08/aa/cc0199a5f0ad350994d660967a8efb233fe0416e4639146c089643407ce6/packaging-24.1-py3-none-any.whl", hash = "sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124", size = 53985 }, ] -[[package]] -name = "pathspec" -version = "0.12.1" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/ca/bc/f35b8446f4531a7cb215605d100cd88b7ac6f44ab3fc94870c120ab3adbf/pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712", size = 51043 } -wheels = [ - { url = "https://files.pythonhosted.org/packages/cc/20/ff623b09d963f88bfde16306a54e12ee5ea43e9b597108672ff3a408aad6/pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08", size = 31191 }, -] - -[[package]] -name = "platformdirs" -version = "4.3.6" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/13/fc/128cc9cb8f03208bdbf93d3aa862e16d376844a14f9a0ce5cf4507372de4/platformdirs-4.3.6.tar.gz", hash = "sha256:357fb2acbc885b0419afd3ce3ed34564c13c9b95c89360cd9563f73aa5e2b907", size = 21302 } -wheels = [ - { url = "https://files.pythonhosted.org/packages/3c/a6/bc1012356d8ece4d66dd75c4b9fc6c1f6650ddd5991e421177d9f8f671be/platformdirs-4.3.6-py3-none-any.whl", hash = "sha256:73e575e1408ab8103900836b97580d5307456908a03e92031bab39e4554cc3fb", size = 18439 }, -] - [[package]] name = "pluggy" version = "1.5.0" @@ -607,7 +561,6 @@ version = "0.0.0a0" source = { virtual = "." } dependencies = [ { name = "aiochannel" }, - { name = "black" }, { name = "grpcio" }, { name = "grpcio-tools" }, { name = "msgpack" }, @@ -640,7 +593,6 @@ dev = [ [package.metadata] requires-dist = [ { name = "aiochannel", specifier = ">=1.2.1" }, - { name = "black", specifier = ">=23.11,<25.0" }, { name = "grpcio", specifier = ">=1.59.3" }, { name = "grpcio-tools", specifier = ">=1.59.3" }, { name = "msgpack", specifier = ">=1.0.7" },