From 39e2c27f39fe85b55e164bdadc6beda05fb7b21b Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:22:30 -0700 Subject: [PATCH 01/13] Add ruff as development dependency Signed-off-by: Mattt Zmuda --- pyproject.toml | 2 +- requirements-dev.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 3376263a..4daef486 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ license = { file = "LICENSE" } authors = [{ name = "Replicate, Inc." }] requires-python = ">=3.8" dependencies = ["packaging", "pydantic>1", "requests>2"] -optional-dependencies = { dev = ["black", "pytest", "responses"] } +optional-dependencies = { dev = ["black", "pytest", "responses", "ruff"] } [project.urls] homepage = "https://replicate.com" diff --git a/requirements-dev.txt b/requirements-dev.txt index f10b82a5..c3865e8e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -43,6 +43,8 @@ requests==2.28.2 # responses responses==0.23.1 # via replicate (pyproject.toml) +ruff==0.0.261 + # via replicate (pyproject.toml) types-pyyaml==6.0.12.9 # via responses typing-extensions==4.5.0 From 03421b07b926b50bd548893533065732cb760f06 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:24:00 -0700 Subject: [PATCH 02/13] Add lint step to CI workflow Signed-off-by: Mattt Zmuda --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 77e78619..72bec4a4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,6 +24,8 @@ jobs: cache: "pip" - name: Install dependencies run: python -m pip install -r requirements.txt -r requirements-dev.txt . + - name: Lint + run: python -m ruff . - name: Test run: python -m pytest From 867cde64da7ddcb0928b5ea0695c4ba83e383182 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:25:08 -0700 Subject: [PATCH 03/13] Apply automatic fixes made by ruff Signed-off-by: Mattt Zmuda --- replicate/model.py | 3 +-- replicate/prediction.py | 4 ++-- replicate/training.py | 8 +++----- tests/test_prediction.py | 7 +++---- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/replicate/model.py b/replicate/model.py index e56204b2..151ebcbc 100644 --- a/replicate/model.py +++ b/replicate/model.py @@ -1,4 +1,3 @@ -from typing import List from replicate.base_model import BaseModel from replicate.collection import Collection @@ -12,7 +11,7 @@ class Model(BaseModel): def predict(self, *args, **kwargs): raise ReplicateException( - f"The `model.predict()` method has been removed, because it's unstable: if a new version of the model you're using is pushed and its API has changed, your code may break. Use `version.predict()` instead. See https://github.com/replicate/replicate-python#readme" + "The `model.predict()` method has been removed, because it's unstable: if a new version of the model you're using is pushed and its API has changed, your code may break. Use `version.predict()` instead. See https://github.com/replicate/replicate-python#readme" ) @property diff --git a/replicate/prediction.py b/replicate/prediction.py index 1d5df1a6..0e4d2349 100644 --- a/replicate/prediction.py +++ b/replicate/prediction.py @@ -3,7 +3,7 @@ from replicate.base_model import BaseModel from replicate.collection import Collection -from replicate.exceptions import ModelError, ReplicateException +from replicate.exceptions import ModelError from replicate.files import upload_file from replicate.json import encode_json from replicate.version import Version @@ -92,7 +92,7 @@ def get(self, id: str) -> Prediction: return self.prepare_model(obj) def list(self) -> List[Prediction]: - resp = self._client._request("GET", f"/v1/predictions") + resp = self._client._request("GET", "/v1/predictions") # TODO: paginate predictions = resp.json()["results"] for prediction in predictions: diff --git a/replicate/training.py b/replicate/training.py index 3cbe920f..bf43a465 100644 --- a/replicate/training.py +++ b/replicate/training.py @@ -1,13 +1,11 @@ import re -import time -from typing import Any, Dict, Iterator, List, Optional +from typing import Any, Dict, List, Optional from replicate.base_model import BaseModel from replicate.collection import Collection -from replicate.exceptions import ModelError, ReplicateException +from replicate.exceptions import ReplicateException from replicate.files import upload_file from replicate.json import encode_json -from replicate.version import Version class Training(BaseModel): @@ -55,7 +53,7 @@ def create( ) if not match: raise ReplicateException( - f"version must be in format username/model_name:version_id" + "version must be in format username/model_name:version_id" ) username = match.group("username") model_name = match.group("model_name") diff --git a/tests/test_prediction.py b/tests/test_prediction.py index bbfecb9d..a4621722 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -1,7 +1,6 @@ import responses from responses import matchers -import replicate from .factories import create_client, create_version @@ -41,7 +40,7 @@ def test_create_works_with_webhooks(): }, ) - prediction = client.predictions.create( + client.predictions.create( version=version, input={"text": "world"}, webhook="https://example.com/webhook", @@ -156,8 +155,8 @@ def test_async_timings(): ) assert prediction.created_at == "2022-04-26T20:00:40.658234Z" - assert prediction.completed_at == None - assert prediction.output == None + assert prediction.completed_at is None + assert prediction.output is None prediction.wait() assert prediction.created_at == "2022-04-26T20:00:40.658234Z" assert prediction.completed_at == "2022-04-26T20:02:27.648305Z" From 7e3547246d5221e346cdeb65f3232e267370289b Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:30:07 -0700 Subject: [PATCH 04/13] Ignore rule E501 Signed-off-by: Mattt Zmuda --- pyproject.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 4daef486..04cc6676 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,3 +19,8 @@ repository = "https://github.com/replicate/replicate-python" [tool.pytest.ini_options] testpaths = "tests/" + +[tool.ruff] +ignore = [ + "E501", # Line too long +] From 1e6874e3f5066d626e15627a019548100e3a3f57 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:42:46 -0700 Subject: [PATCH 05/13] Explicitly select ruff rule sets Signed-off-by: Mattt Zmuda --- pyproject.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 04cc6676..da2ebf87 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,11 @@ repository = "https://github.com/replicate/replicate-python" testpaths = "tests/" [tool.ruff] +select = [ + "E", # pycodestyle error + "F", # Pyflakes + "W", # pycodestyle warning +] ignore = [ "E501", # Line too long ] From 624cfaece7516a723b388935ee249817ebdbac05 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:43:44 -0700 Subject: [PATCH 06/13] Enable isort rules in ruff Signed-off-by: Mattt Zmuda --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index da2ebf87..f56421e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,7 @@ testpaths = "tests/" select = [ "E", # pycodestyle error "F", # Pyflakes + "I", # isort "W", # pycodestyle warning ] ignore = [ From f4dec2a58e0a494d463d0b9bf43031f57f4619ba Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:43:55 -0700 Subject: [PATCH 07/13] Apply automatic fixes made by ruff for isort violations Signed-off-by: Mattt Zmuda --- replicate/base_model.py | 2 +- replicate/json.py | 1 - replicate/model.py | 1 - tests/test_prediction.py | 1 - tests/test_version.py | 3 ++- 5 files changed, 3 insertions(+), 5 deletions(-) diff --git a/replicate/base_model.py b/replicate/base_model.py index 51fadf21..164ca045 100644 --- a/replicate/base_model.py +++ b/replicate/base_model.py @@ -1,6 +1,6 @@ from typing import ForwardRef -import pydantic +import pydantic Client = ForwardRef("Client") Collection = ForwardRef("Collection") diff --git a/replicate/json.py b/replicate/json.py index c23f40cc..bab3b4d8 100644 --- a/replicate/json.py +++ b/replicate/json.py @@ -3,7 +3,6 @@ from types import GeneratorType from typing import Any, Callable - try: import numpy as np # type: ignore diff --git a/replicate/model.py b/replicate/model.py index 151ebcbc..5d240955 100644 --- a/replicate/model.py +++ b/replicate/model.py @@ -1,4 +1,3 @@ - from replicate.base_model import BaseModel from replicate.collection import Collection from replicate.exceptions import ReplicateException diff --git a/tests/test_prediction.py b/tests/test_prediction.py index a4621722..a0d08ae9 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -1,7 +1,6 @@ import responses from responses import matchers - from .factories import create_client, create_version diff --git a/tests/test_version.py b/tests/test_version.py index 59984224..fb08ec50 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -2,9 +2,10 @@ import pytest import responses -from replicate.exceptions import ModelError from responses import matchers +from replicate.exceptions import ModelError + from .factories import ( create_version, create_version_with_iterator_output, From fc7f7fa648731f918c3218dc9e9a8c2606f05f28 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:44:40 -0700 Subject: [PATCH 08/13] Enable pyupgrade rules in ruff Signed-off-by: Mattt Zmuda --- pyproject.toml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f56421e7..a1a8dd03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,10 +22,11 @@ testpaths = "tests/" [tool.ruff] select = [ - "E", # pycodestyle error - "F", # Pyflakes - "I", # isort - "W", # pycodestyle warning + "E", # pycodestyle error + "F", # Pyflakes + "I", # isort + "W", # pycodestyle warning + "UP", # pyupgrade ] ignore = [ "E501", # Line too long From 7d0bc2498b14dc0a69a704b4882e631bd567e9a6 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:45:01 -0700 Subject: [PATCH 09/13] Apply automatic fix made by ruff for pyupgrade violation Signed-off-by: Mattt Zmuda --- replicate/collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/replicate/collection.py b/replicate/collection.py index 1b9c1368..94766aae 100644 --- a/replicate/collection.py +++ b/replicate/collection.py @@ -35,4 +35,4 @@ def prepare_model(self, attrs): model._collection = self return model else: - raise Exception("Can't create %s from %s" % (self.model.__name__, attrs)) + raise Exception(f"Can't create {self.model.__name__} from {attrs}") From 273cb4e27b36e4d5b16c2c4ea98a3e32eac342d6 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 7 Apr 2023 15:58:46 -0700 Subject: [PATCH 10/13] Enable flake8-bandit rules in ruff Signed-off-by: Mattt Zmuda --- pyproject.toml | 16 +++++++++++----- replicate/files.py | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a1a8dd03..1cdbcdd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,12 +22,18 @@ testpaths = "tests/" [tool.ruff] select = [ - "E", # pycodestyle error - "F", # Pyflakes - "I", # isort - "W", # pycodestyle warning - "UP", # pyupgrade + "E", # pycodestyle error + "F", # Pyflakes + "I", # isort + "W", # pycodestyle warning + "UP", # pyupgrade + "S", # flake8-bandit + "BLE", # flake8-blind-except ] ignore = [ "E501", # Line too long + "S113", # Probable use of requests call without timeout ] + +[tool.ruff.per-file-ignores] +"tests/*" = ["S101", "S106"] diff --git a/replicate/files.py b/replicate/files.py index 82f70c89..c93d411f 100644 --- a/replicate/files.py +++ b/replicate/files.py @@ -15,7 +15,7 @@ def upload_file(fh: io.IOBase, output_file_prefix: str = None) -> str: if output_file_prefix is not None: name = getattr(fh, "name", "output") url = output_file_prefix + os.path.basename(name) - resp = requests.put(url, files={"file": fh}) + resp = requests.put(url, files={"file": fh}, timeout=None) resp.raise_for_status() return url From b812008d92f05e8d6f89d960b051a6584f77ae0a Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 10 Apr 2023 04:32:31 -0700 Subject: [PATCH 11/13] Enable flake8-boolean-trap rules in ruff Signed-off-by: Mattt Zmuda --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 1cdbcdd8..c0ff3b8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ select = [ "UP", # pyupgrade "S", # flake8-bandit "BLE", # flake8-blind-except + "FBT", # flake8-boolean-trap ] ignore = [ "E501", # Line too long From 56be306bb8b8621f1a4c8671bb4f31a41c13185b Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 10 Apr 2023 04:32:50 -0700 Subject: [PATCH 12/13] Enable flake8-bugbear rules in ruff Signed-off-by: Mattt Zmuda --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index c0ff3b8b..24400d23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ select = [ "S", # flake8-bandit "BLE", # flake8-blind-except "FBT", # flake8-boolean-trap + "B", # flake8-bugbear ] ignore = [ "E501", # Line too long From e2ef60060825cb761d081eba48cc48661ac3430f Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 10 Apr 2023 04:33:06 -0700 Subject: [PATCH 13/13] Fix 'B028 No explicit `stacklevel` keyword argument found' Signed-off-by: Mattt Zmuda --- replicate/version.py | 1 + 1 file changed, 1 insertion(+) diff --git a/replicate/version.py b/replicate/version.py index cf3376a8..02a5fa5d 100644 --- a/replicate/version.py +++ b/replicate/version.py @@ -18,6 +18,7 @@ def predict(self, **kwargs) -> Union[Any, Iterator[Any]]: warnings.warn( "version.predict() is deprecated. Use replicate.run() instead. It will be removed before version 1.0.", DeprecationWarning, + stacklevel=1, ) prediction = self._client.predictions.create(version=self, input=kwargs)