Skip to content

Commit

Permalink
chore/ruff formatting (#128)
Browse files Browse the repository at this point in the history
Why
===

Breaking out ruff changes from #123 

What changed
============

Separation of concerns, increasing revertability

Test plan
=========

CI
  • Loading branch information
blast-hardcheese authored Dec 2, 2024
1 parent 7ce86bd commit 798e59e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Format check
run: |
uv run black --check .
uv run ruff format --check .
- name: Lint check
run: |
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = [
Expand Down
20 changes: 12 additions & 8 deletions replit_river/codegen/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import re
import subprocess
from pathlib import Path
from textwrap import dedent
from typing import (
Expand All @@ -16,7 +17,6 @@
cast,
)

import black
from pydantic import BaseModel, Field, RootModel

from replit_river.codegen.format import reindent
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions replit_river/codegen/server.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
9 changes: 0 additions & 9 deletions scripts/format.sh

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/lint.sh

This file was deleted.

18 changes: 11 additions & 7 deletions tests/river_fixtures/clientserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions tests/test_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 0 additions & 48 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 798e59e

Please sign in to comment.