Skip to content
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

Handle untyped interfaces in _check #18

Merged
merged 9 commits into from
Jan 24, 2024
Merged
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
10 changes: 10 additions & 0 deletions src/nnbench/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ def _check(params: dict[str, Any], benchmarks: list[Benchmark]) -> None:
)

param_type = param.annotation

if param_type == inspect.Parameter.empty:
logger.debug(f"Found untyped parameter {name!r} in benchmark {bm.fn.__name__!r}.")

if name in benchmark_interface and benchmark_interface[name].annotation != param_type:
orig_type = benchmark_interface[name].annotation
raise TypeError(
f"got non-unique types {orig_type}, {param_type} for parameter {name!r}"
)
benchmark_interface[name] = param

for name, param in benchmark_interface.items():
if name not in param_types and param.default == inspect.Parameter.empty:
raise ValueError(f"missing value for required parameter {name!r}")

if param.annotation == inspect.Parameter.empty:
continue

# only check type match if type supplied
if not issubclass(param_types[name], param.annotation):
raise TypeError(
f"expected type {param.annotation} for parameter {name!r}, "
Expand Down
7 changes: 7 additions & 0 deletions tests/test_argcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ def test_log_warn_on_overwrite_default(
with caplog.at_level(logging.DEBUG):
r.run(benchmark, params={"a": 1})
assert "using value 1 instead of default" in caplog.text


def test_untyped_interface(typecheckfolder: str) -> None:
benchmarks = os.path.join(typecheckfolder, "untyped_benchmarks.py")

r = runner.AbstractBenchmarkRunner()
r.run(benchmarks, params={"value": 2})
6 changes: 6 additions & 0 deletions tests/typechecking/untyped_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import nnbench


@nnbench.benchmark
def increment(value):
return value + 1
Loading