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

Add warning for supplied and default parameter collision #19

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions src/nnbench/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def _check(params: dict[str, Any], benchmarks: list[Benchmark]) -> None:
benchmark_interface: dict[str, inspect.Parameter] = {}
for bm in benchmarks:
for name, param in inspect.signature(bm.fn).parameters.items():
if not param.default == inspect.Parameter.empty and name in params:
logger.warning(
f"received parameter {name!r}={params[name]}, overwrite the default ({name!r}={param.default}) in function '{bm.fn.__name__}'"
)
maxmynter marked this conversation as resolved.
Show resolved Hide resolved

param_type = param.annotation
if name in benchmark_interface and benchmark_interface[name].annotation != param_type:
orig_type = benchmark_interface[name].annotation
Expand Down
12 changes: 12 additions & 0 deletions tests/test_argcheck.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os

import pytest
Expand All @@ -22,3 +23,14 @@ def test_error_on_duplicate_params(typecheckfolder: str) -> None:
with pytest.raises(TypeError, match="got non-unique types.*"):
r = runner.AbstractBenchmarkRunner()
r.run(benchmarks, params={"x": 1, "y": 1})


def test_log_warn_on_overwrite_default(
typecheckfolder: str, caplog: pytest.LogCaptureFixture
) -> None:
benchmark = os.path.join(typecheckfolder, "default_benchmarks.py")
r = runner.AbstractBenchmarkRunner()
with caplog.at_level(logging.WARNING):
r.run(benchmark, params={"a": 1})
print("TEXT", caplog.text)
assert "overwrite" in caplog.text
6 changes: 6 additions & 0 deletions tests/typechecking/default_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import nnbench


@nnbench.benchmark
def add_two(a: int = 0) -> int:
return a + 2
Loading