Fedir Zadniprovskyi 2024-05-21
style: add ruff
@94c75432f4ac1f02aed03a31b42bd5017441065f
.pre-commit-config.yaml
--- .pre-commit-config.yaml
+++ .pre-commit-config.yaml
@@ -8,18 +8,21 @@
       - id: end-of-file-fixer
       - id: check-yaml
       - id: check-added-large-files
-
-  - repo: https://github.com/pre-commit/mirrors-mypy
-    rev: v1.10.0
-    hooks:
-      - id: mypy
-
-  # - repo: https://github.com/PyCQA/isort
-  #   rev: 5.13.2
+  # TODO: enable
+  # - repo: https://github.com/pre-commit/mirrors-mypy
+  #   rev: v1.10.0
   #   hooks:
-  #     - id: isort
-  #
-  # - repo: https://github.com/psf/black
-  #   rev: 24.4.2
+  #     - id: mypy
+  #       args: [--strict]
+  # TODO: enable
+  # - repo: https://github.com/RobertCraigie/pyright-python
+  #   rev: v1.1.363
   #   hooks:
-  #     - id: black
+  #   - id: pyright
+  # Disabled because it doesn't work on NixOS
+  # - repo: https://github.com/astral-sh/ruff-pre-commit
+  #   rev: v0.4.4
+  #   hooks:
+  #     - id: ruff # linter
+  #       args: [--fix]
+  #     - id: ruff-format
flake.nix
--- flake.nix
+++ flake.nix
@@ -26,6 +26,7 @@
               pv
               pyright
               python311
+              ruff
               websocat
             ];
             shellHook = ''
pyproject.toml
--- pyproject.toml
+++ pyproject.toml
@@ -27,6 +27,9 @@
 httpx = "^0.27.0"
 httpx-ws = "^0.6.0"
 
+[tool.ruff]
+target-version = "py311"
+
 [build-system]
 requires = ["poetry-core"]
 build-backend = "poetry.core.masonry.api"
speaches/audio.py
--- speaches/audio.py
+++ speaches/audio.py
@@ -12,7 +12,7 @@
 
 
 def audio_samples_from_file(file: BinaryIO) -> NDArray[np.float32]:
-    audio_and_sample_rate: tuple[NDArray[np.float32], Any] = sf.read(  # type: ignore
+    audio_and_sample_rate = sf.read(
         file,
         format="RAW",
         channels=1,
@@ -22,7 +22,7 @@
         endian="LITTLE",
     )
     audio = audio_and_sample_rate[0]
-    return audio
+    return audio  # type: ignore
 
 
 class Audio:
@@ -68,12 +68,12 @@
         self.modify_event = asyncio.Event()
 
     def extend(self, data: NDArray[np.float32]) -> None:
-        assert self.closed == False
+        assert not self.closed
         super().extend(data)
         self.modify_event.set()
 
     def close(self) -> None:
-        assert self.closed == False
+        assert not self.closed
         self.closed = True
         self.modify_event.set()
         logger.info("AudioStream closed")
speaches/core.py
--- speaches/core.py
+++ speaches/core.py
@@ -92,14 +92,14 @@
 
 
 def test_segment_is_eos():
-    assert Segment("Hello").is_eos == False
-    assert Segment("Hello...").is_eos == False
-    assert Segment("Hello.").is_eos == True
-    assert Segment("Hello!").is_eos == True
-    assert Segment("Hello?").is_eos == True
-    assert Segment("Hello. Yo").is_eos == False
-    assert Segment("Hello. Yo...").is_eos == False
-    assert Segment("Hello. Yo.").is_eos == True
+    assert not Segment("Hello").is_eos
+    assert not Segment("Hello...").is_eos
+    assert Segment("Hello.").is_eos
+    assert Segment("Hello!").is_eos
+    assert Segment("Hello?").is_eos
+    assert not Segment("Hello. Yo").is_eos
+    assert not Segment("Hello. Yo...").is_eos
+    assert Segment("Hello. Yo.").is_eos
 
 
 def to_full_sentences(words: list[Word]) -> list[Segment]:
speaches/main.py
--- speaches/main.py
+++ speaches/main.py
@@ -7,8 +7,14 @@
 from io import BytesIO
 from typing import Annotated
 
-from fastapi import (Depends, FastAPI, Response, UploadFile, WebSocket,
-                     WebSocketDisconnect)
+from fastapi import (
+    Depends,
+    FastAPI,
+    Response,
+    UploadFile,
+    WebSocket,
+    WebSocketDisconnect,
+)
 from fastapi.websockets import WebSocketState
 from faster_whisper import WhisperModel
 from faster_whisper.vad import VadOptions, get_speech_timestamps
@@ -18,8 +24,11 @@
 from speaches.config import SAMPLES_PER_SECOND, Language, config
 from speaches.core import Transcription
 from speaches.logger import logger
-from speaches.server_models import (ResponseFormat, TranscriptionResponse,
-                                    TranscriptionVerboseResponse)
+from speaches.server_models import (
+    ResponseFormat,
+    TranscriptionResponse,
+    TranscriptionVerboseResponse,
+)
 from speaches.transcriber import audio_transcriber
 
 whisper: WhisperModel = None  # type: ignore
Add a comment
List