Skip to content
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
12 changes: 12 additions & 0 deletions docs/test_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from docs import GetRegistryLink


def test_registry_link():
assert (
GetRegistryLink()('lir.config.substitution.parse_categorical')
== ':class:`lir.config.substitution.parse_categorical <lir.config.substitution.CategoricalHyperparameter>`'
)
assert (
GetRegistryLink()('hyperparameter_types.categorical')
== ':class:`hyperparameter_types.categorical <lir.config.substitution.CategoricalHyperparameter>`'
)
12 changes: 11 additions & 1 deletion lir/config/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import warnings
from abc import ABC, abstractmethod
from collections.abc import Callable, Iterator, Mapping, Sequence
from dataclasses import dataclass
Expand Down Expand Up @@ -627,6 +628,15 @@ def parse(
) -> Any:
return func(config, output_dir) # type: ignore

def __call__(self, config: ConfigValue | None = None, output_dir: Path | None = None) -> Any:
if config and output_dir:
return self.parse(config, output_dir)
else:
warnings.warn(
DeprecationWarning('legacy invocation of annotated function (remove parentheses)'), stacklevel=2
)
return self

def reference(self) -> str:
# return the reference argument, if any
if reference is not None:
Expand All @@ -643,7 +653,7 @@ def reference(self) -> str:
# last resort: fallback to wrapped function name
return get_full_name(func)

return ConfigParserFunction
return ConfigParserFunction()


def pop_field(
Expand Down
24 changes: 12 additions & 12 deletions lir/config/substitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def _parse_categorical_option(spec: Any, path: str, option_index: int | None) ->
return HyperparameterOption(name, {path: value})


@config_parser(reference='lir.config.substitution.parse_categorical')
def parse_categorical(spec: ConfigValue, output_path: Path) -> 'CategoricalHyperparameter':
@config_parser
def parse_categorical(spec: ConfigValue, output_path: Path) -> CategoricalHyperparameter:
"""
Parse a categorical hyperparameter from configuration.

Expand Down Expand Up @@ -282,7 +282,7 @@ def _parse_clustered_option(spec: ConfigValue) -> HyperparameterOption:
return HyperparameterOption(option_name, substitutions)


@config_parser(reference='lir.config.substitution.parse_clustered')
@config_parser
def parse_clustered(spec: ConfigValue, output_path: Path) -> CategoricalHyperparameter:
"""
Parse the configuration section of a clustered hyperparameter.
Expand Down Expand Up @@ -316,7 +316,7 @@ def parse_clustered(spec: ConfigValue, output_path: Path) -> CategoricalHyperpar
return CategoricalHyperparameter(parameter_name, options)


@config_parser(reference='lir.config.substitution.parse_constant')
@config_parser
def parse_constant(spec: ConfigValue, output_path: Path) -> CategoricalHyperparameter:
"""
Parse the configuration section of a constant.
Expand Down Expand Up @@ -400,8 +400,8 @@ def options(self) -> list[HyperparameterOption]:
return [HyperparameterOption(str(value), {self.path: value}) for value in values]


@config_parser(reference='lir.config.substitution.parse_float')
def parse_float(spec: ConfigValue, output_path: Path) -> 'FloatHyperparameter':
@config_parser
def parse_float(spec: ConfigValue, output_path: Path) -> FloatHyperparameter:
"""
Parse a floating-point hyperparameter from configuration.

Expand Down Expand Up @@ -514,8 +514,8 @@ def options(self) -> list[HyperparameterOption]:
return options


@config_parser(reference='lir.config.substitution.parse_folder')
def parse_folder(spec: ConfigValue, output_path: Path) -> 'FolderHyperparameter':
@config_parser
def parse_folder(spec: ConfigValue, output_path: Path) -> FolderHyperparameter:
"""
Parse a folder hyperparameter from configuration.

Expand Down Expand Up @@ -564,13 +564,13 @@ def parse_parameter(

parser = registry.get(parameter_type, search_path=['hyperparameter_types'])
elif 'value' in spec:
parser = parse_constant() # type: ignore
parser = parse_constant # type: ignore
elif 'options' in spec and 'path' in spec:
parser = parse_categorical() # type: ignore
parser = parse_categorical # type: ignore
elif 'options' in spec and 'name' in spec:
parser = parse_clustered() # type: ignore
parser = parse_clustered # type: ignore
elif 'high' in spec:
parser = parse_float() # type: ignore
parser = parse_float # type: ignore
else:
raise YamlParseError(
spec.context,
Expand Down
13 changes: 4 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,10 @@ lint.select = [
"S", # bandit
"PIE", # flake8-pie
]
lint.per-file-ignores = {"tests/*" = [
"D", # Ignore missing docstrings in tests.
"S101", # Allow use of assert in tests.
], "*_test.py" = [
"D", # Ignore missing docstrings in tests.
"S101", # Allow use of assert in tests.
], "docs/snippets/*.py" = [
"D", # Ignore missing docstrings in snippets.
]}
lint.per-file-ignores = {"!lir/*" = [
"D", # Ignore missing docstrings in tests.
"S101", # Allow use of assert in tests.
]}
lint.pydocstyle.convention = "numpy"

[tool.mypy]
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_bootstraps.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ def test_interval_extrapolation(sample_steps_and_data):
)
def test_bootstrap_config(config):
config = ConfigValue.wrap([], config)
bootstrap().parse(config, Path('/'))
bootstrap(config, Path('/'))
4 changes: 3 additions & 1 deletion tests/config/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ def my_config_parser(config: ConfigValue, output_path: Path) -> int:
return pop_field(config, 'key', validate_type=int)


@pytest.mark.filterwarnings('ignore:legacy')
def test_config_parser():
config = ConfigValue.wrap([], {'key': 42})
assert my_config_parser().parse(config, Path('/')) == 42
assert my_config_parser(config.clone(), Path('/')) == 42
assert my_config_parser().parse(config.clone(), Path('/')) == 42


def test_generic_config_parser():
Expand Down
4 changes: 2 additions & 2 deletions tests/datasets/test_csv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_csv_parser(
parser_args['file'] = str(csv_file)
parser_args = ConfigValue.wrap([], parser_args)
try:
parser = feature_data_csv_file_parser().parse(parser_args, tmp_path)
parser = feature_data_csv_file_parser(parser_args, tmp_path)
actual_result = parser.get_instances()
if expected_result is not None:
assert actual_result == expected_result
Expand All @@ -173,7 +173,7 @@ def test_csv_parser_label_column_alias_warns(tmp_path: Path):

parser_args = ConfigValue.wrap([], {'label_column': 'label', 'file': str(csv_file)})
with pytest.warns(UserWarning, match='label_column'):
parser = feature_data_csv_file_parser().parse(parser_args, tmp_path)
parser = feature_data_csv_file_parser(parser_args, tmp_path)
actual_result = parser.get_instances()

assert actual_result == FeatureData(hypothesis=np.array([1]), features=np.ones((1, 2)))
Loading