Skip to content

Error message improvements #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/server/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (r *Runner) wait() {
err := r.cmd.Wait()
if err != nil {
runnerLogs := r.rotateLogs()
log.Errorw("python runner exited with error", "pid", r.cmd.Process.Pid, "error", err, "logs", runnerLogs)
log.Errorw("python runner exited with error", "pid", r.cmd.Process.Pid, "error", err)
for _, pr := range r.pending {
pr.mu.Lock()
now := util.NowIso()
Expand Down
16 changes: 9 additions & 7 deletions python/coglet/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ def _check_parent(child: type, parent: type) -> bool:


def _validate_setup(f: Callable) -> None:
assert inspect.isfunction(f), f'not a function {f}'
assert inspect.isfunction(f), 'setup is not a function'
spec = inspect.getfullargspec(f)

assert spec.args[:1] == ['self'], "setup() must have 'self' first argument"
non_default_parameter_args = spec.args
if spec.defaults is not None:
non_default_parameter_args = non_default_parameter_args[: -len(spec.defaults)]
assert non_default_parameter_args == ['self'] or non_default_parameter_args == [
'self',
'weights',
], f'unexpected setup() arguments: {non_default_parameter_args}'
extra_args = ', '.join(
[a for a in non_default_parameter_args if a not in {'self', 'weights'}]
)
assert extra_args == '', f'unexpected setup() arguments: {extra_args}'
assert spec.varargs is None, 'setup() must not have *args'
assert spec.varkw is None, 'setup() must not have **kwargs'
assert spec.kwonlyargs == [], 'setup() must not have keyword-only args'
Expand All @@ -31,11 +33,11 @@ def _validate_setup(f: Callable) -> None:


def _validate_predict(f: Callable, is_class_fn: bool) -> None:
assert inspect.isfunction(f), f'not a function: {f}'
assert inspect.isfunction(f), 'predict is not a function'
spec = inspect.getfullargspec(f)

if is_class_fn:
assert spec.args[0] == 'self', "predict() must have 'self' first argument"
assert spec.args[:1] == ['self'], "predict() must have 'self' first argument"
assert spec.varargs is None, 'predict() must not have *args'
assert spec.varkw is None, 'predict() must not have **kwargs'
assert spec.kwonlyargs == [], 'predict() must not have keyword-only args'
Expand Down
1 change: 1 addition & 0 deletions python/tests/errors/cog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
predict: "predict.py:Predictor"
10 changes: 10 additions & 0 deletions python/tests/errors/predict_fn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from cog import BasePredictor

ERROR = 'predict is not a function'


class Predictor(BasePredictor):
def setup(self) -> None:
pass

predict = 0
11 changes: 11 additions & 0 deletions python/tests/errors/predict_kwarg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'predict() must not have **kwargs'


class Predictor(BasePredictor):
def setup(self) -> None:
pass

def predict(self, **kwargs) -> None:
pass
11 changes: 11 additions & 0 deletions python/tests/errors/predict_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'predict() must not return None'


class Predictor(BasePredictor):
def setup(self) -> None:
pass

def predict(self, s: str) -> None:
pass
11 changes: 11 additions & 0 deletions python/tests/errors/predict_self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = "predict() must have 'self' first argument"


class Predictor(BasePredictor):
def setup(self) -> None:
pass

def predict() -> str:
return ''
11 changes: 11 additions & 0 deletions python/tests/errors/predict_vararg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'predict() must not have *args'


class Predictor(BasePredictor):
def setup(self) -> None:
pass

def predict(self, *args) -> None:
pass
11 changes: 11 additions & 0 deletions python/tests/errors/setup_arg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'unexpected setup() arguments: x, y'


class Predictor(BasePredictor):
def setup(self, x: int, y: int) -> None:
pass

def predict(self, s: str) -> str:
return s
10 changes: 10 additions & 0 deletions python/tests/errors/setup_fn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from cog import BasePredictor

ERROR = 'setup is not a function'


class Predictor(BasePredictor):
setup = 0

def predict(self, s: str) -> str:
return s
11 changes: 11 additions & 0 deletions python/tests/errors/setup_kwargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'setup() must not have **kwargs'


class Predictor(BasePredictor):
def setup(self, **kwargs) -> None:
pass

def predict(self, s: str) -> str:
return s
11 changes: 11 additions & 0 deletions python/tests/errors/setup_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'setup() must return None'


class Predictor(BasePredictor):
def setup(self) -> int:
return 0

def predict(self, s: str) -> str:
return s
11 changes: 11 additions & 0 deletions python/tests/errors/setup_self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = "setup() must have 'self' first argument"


class Predictor(BasePredictor):
def setup() -> None:
pass

def predict(self, s: str) -> str:
return s
11 changes: 11 additions & 0 deletions python/tests/errors/setup_varargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cog import BasePredictor

ERROR = 'setup() must not have *args'


class Predictor(BasePredictor):
def setup(self, *args) -> None:
pass

def predict(self, s: str) -> str:
return s
27 changes: 27 additions & 0 deletions python/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import importlib
import os.path
import pkgutil
import re
from typing import List

import pytest

from coglet import inspector


def get_predictors() -> List[str]:
errors_dir = os.path.join(os.path.dirname(__file__), 'errors')
return [name for _, name, _ in pkgutil.iter_modules([errors_dir])]


def run_error(module_name: str, predictor_name: str) -> None:
m = importlib.import_module(module_name)
err_msg = getattr(m, 'ERROR')
with pytest.raises(AssertionError, match=re.escape(err_msg)):
inspector.create_predictor(module_name, predictor_name)


@pytest.mark.parametrize('predictor', get_predictors())
def test_predictor(predictor):
module_name = f'tests.errors.{predictor}'
run_error(module_name, 'Predictor')
6 changes: 5 additions & 1 deletion script/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ check_python() {
uv tool run ruff check
uv tool run ruff format --check
fi
.venv/bin/mypy . --exclude build --exclude python/tests/cases --exclude python/tests/runners --exclude python/tests/schemas
.venv/bin/mypy . --exclude build \
--exclude python/tests/cases \
--exclude python/tests/errors \
--exclude python/tests/runners \
--exclude python/tests/schemas
}

if [ $# -eq 0 ]; then
Expand Down