From 4d015bddb66e5c328188e002eadbad4217e0d061 Mon Sep 17 00:00:00 2001 From: CircuitSacul Date: Wed, 7 Dec 2022 20:29:47 -0500 Subject: [PATCH] linting/workflows --- .github/workflows/ci.yml | 24 ++++++++++++++++++++++++ .github/workflows/pypi.yml | 25 +++++++++++++++++++++++++ example/client.py | 4 ++-- noxfile.py | 21 +++++++++++++++++++++ pyproject.toml | 15 ++++++++++++++- socketapp/client.py | 14 +++----------- socketapp/event.py | 2 +- socketapp/server.py | 12 ++---------- 8 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/pypi.yml create mode 100644 noxfile.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a29c788 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +name: CI + +on: + pull_request: + +jobs: + ci: + runs-on: ${{ matrix.os }}-latest + strategy: + matrix: + python-version: [ '3.8', '3.9', '3.10' ] + os: [ ubuntu, windows ] + + name: ${{ matrix.python-version }} ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install Nox + run: pip install nox + - name: Run Nox + run: nox diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml new file mode 100644 index 0000000..e40823d --- /dev/null +++ b/.github/workflows/pypi.yml @@ -0,0 +1,25 @@ +name: pypi + +on: + release: + types: [published] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: '3.8' + - name: Install Dependencies + run: pip install poetry + - name: Build & Upload + env: + PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} + run: | + poetry config pypi-token.pypi $PYPI_TOKEN + poetry version $(git describe --tags --abbrev=0) + poetry build + poetry publish diff --git a/example/client.py b/example/client.py index 278b376..a80b9c5 100644 --- a/example/client.py +++ b/example/client.py @@ -4,13 +4,13 @@ from socketapp import Client -async def send(client: Client): +async def send(client: Client) -> None: while True: await asyncio.sleep(5) await client.send(Message(data="hi"), client.clients) -async def main(client: Client): +async def main(client: Client) -> None: asyncio.create_task(send(client)) await client.run() diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000..531ee68 --- /dev/null +++ b/noxfile.py @@ -0,0 +1,21 @@ +import nox + + +@nox.session +def mypy(session: nox.Session) -> None: + session.install("poetry") + session.run("poetry", "install") + + session.run("mypy", ".") + + +@nox.session +def black(session: nox.Session) -> None: + session.install("black") + session.run("black", ".", "--check") + + +@nox.session +def ruff(session: nox.Session) -> None: + session.install("ruff") + session.run("ruff", ".") diff --git a/pyproject.toml b/pyproject.toml index c21febb..5eb8a66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,6 @@ python = "^3.8" websockets = "^10.4" pydantic = "^1.10.2" - [tool.poetry.group.dev.dependencies] ruff = "^0.0.168" nox = "^2022.11.21" @@ -21,3 +20,17 @@ mypy = "^0.991" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" + +[tool.mypy] +warn_return_any=true +warn_unused_configs=true +strict=true + +[tool.black] +skip-magic-trailing-comma=true + +[tool.ruff] +extend-select = [ + # isort + "I001" +] diff --git a/socketapp/client.py b/socketapp/client.py index 9c2d342..ad0b989 100644 --- a/socketapp/client.py +++ b/socketapp/client.py @@ -15,10 +15,7 @@ class Client: def __init__( - self, - host: str = "localhost", - port: int = 5000, - password: str | None = None, + self, host: str = "localhost", port: int = 5000, password: str | None = None ) -> None: self.host = host self.port = port @@ -61,17 +58,12 @@ async def send(self, event: Event, to: t.Collection[int]) -> None: if not await event.process_client(self, self.client_id): return - raw = json.dumps( - { - "to": list(to), - "event": event.to_dict(), - } - ) + raw = json.dumps({"to": list(to), "event": event.to_dict()}) await self.ws.send(raw) async def _handshake(self, ws: WebSocketClientProtocol) -> int: await ws.send(json.dumps({"password": self.password})) - return json.loads(await ws.recv())["client_id"] + return int(json.loads(await ws.recv())["client_id"]) async def _process_messages(self, ws: WebSocketClientProtocol) -> None: try: diff --git a/socketapp/event.py b/socketapp/event.py index baf7acf..6c1329c 100644 --- a/socketapp/event.py +++ b/socketapp/event.py @@ -33,7 +33,7 @@ def to_dict(self) -> dict[str, t.Any]: dct["cls_id"] = self.event_id return dct - def to_json(self) -> str: # type: ignore + def to_json(self) -> str: return json.dumps(self.to_dict()) @staticmethod diff --git a/socketapp/server.py b/socketapp/server.py index d775020..206daef 100644 --- a/socketapp/server.py +++ b/socketapp/server.py @@ -15,10 +15,7 @@ class Server: def __init__( - self, - host: str = "localhost", - port: int = 5000, - password: str | None = None, + self, host: str = "localhost", port: int = 5000, password: str | None = None ) -> None: self.host = host self.port = port @@ -106,12 +103,7 @@ async def _process_msg(self, msg: str | bytes, client_id: int) -> None: if not ret: return - to_send = json.dumps( - { - "author_id": client_id, - "event": event.to_dict(), - } - ) + to_send = json.dumps({"author_id": client_id, "event": event.to_dict()}) await asyncio.gather( *(ws.send(to_send) for (cid, ws) in self.clients.items() if cid in to),