|
| 1 | +"""Tests to enforce keyword-only argument conventions for add_to_nwbfile methods. |
| 2 | +
|
| 3 | +These tests ensure that all interface add_to_nwbfile methods enforce keyword-only arguments |
| 4 | +after nwbfile and metadata. Only nwbfile and metadata should be positional. |
| 5 | +
|
| 6 | +During the deprecation period (before August 2026), methods use *args with FutureWarning. |
| 7 | +After the deprecation period, methods should use bare * for keyword-only enforcement. |
| 8 | +
|
| 9 | +See the developer style guide for details on the convention. |
| 10 | +""" |
| 11 | + |
| 12 | +import inspect |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from neuroconv.datainterfaces import interface_list |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "interface_class", |
| 21 | + interface_list, |
| 22 | + ids=lambda cls: cls.__name__, |
| 23 | +) |
| 24 | +def test_add_to_nwbfile_only_nwbfile_metadata_positional(interface_class): |
| 25 | + """Only nwbfile and metadata should be positional in add_to_nwbfile.""" |
| 26 | + if "add_to_nwbfile" not in interface_class.__dict__: |
| 27 | + pytest.skip(f"{interface_class.__name__} does not override add_to_nwbfile") |
| 28 | + |
| 29 | + add_to_nwbfile_method = getattr(interface_class, "add_to_nwbfile") |
| 30 | + sig = inspect.signature(add_to_nwbfile_method) |
| 31 | + |
| 32 | + # No add_to_nwbfile uses POSITIONAL_ONLY (/), so POSITIONAL_OR_KEYWORD means "can be passed positionally" |
| 33 | + can_be_passed_positionally = lambda param: param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD |
| 34 | + positional_params = {name for name, param in sig.parameters.items() if can_be_passed_positionally(param)} |
| 35 | + |
| 36 | + allowed_positional_params = {"self", "nwbfile", "metadata"} |
| 37 | + assert positional_params == allowed_positional_params, ( |
| 38 | + f"{interface_class.__name__}.add_to_nwbfile() positional parameters are {positional_params}, " |
| 39 | + f"expected {allowed_positional_params}. " |
| 40 | + f"All other parameters should be keyword-only (use * or *args)." |
| 41 | + ) |
0 commit comments