Skip to content

Commit 533a0ba

Browse files
committed
parser honours defaults by default
1 parent 92d538a commit 533a0ba

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/hyperbase/parsers/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ def get_parser(
3131
compatibility, keyword arguments are merged into *params* (explicit
3232
*params* entries take precedence).
3333
34+
Any parameter the caller does not supply is seeded with the
35+
``"default"`` declared in the parser's :meth:`Parser.accepted_params`,
36+
so a programmatic caller gets the same effective configuration as the
37+
REPL/CLI front-ends (which pre-fill those defaults before constructing
38+
the parser). A declared default of ``None`` means "no default" — it is
39+
left unset so the parser's own ``__init__`` fallback or validation
40+
applies.
41+
3442
Raises :class:`ValueError` if the parser is not installed.
3543
"""
3644
parsers = list_parsers()
@@ -41,6 +49,9 @@ def get_parser(
4149
)
4250
merged: dict[str, Any] = {**kwargs, **(params or {})}
4351
cls = parsers[name].load()
52+
for pname, info in cls.accepted_params().items():
53+
if pname not in merged and info.get("default") is not None:
54+
merged[pname] = info["default"]
4455
return cls(merged)
4556

4657

tests/test_parser_plugin.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ def parse_sentence(self, sentence):
8282

8383
parser = get_parser("mock", params={"lang": "en"})
8484
assert isinstance(parser, MockParser)
85-
assert parser.params == {"lang": "en"}
85+
# get_parser seeds any unset accepted_params() default (here the base
86+
# class's max_depth) so programmatic callers match the REPL/CLI, while
87+
# preserving the caller's own values.
88+
default_depth = MockParser.accepted_params()["max_depth"]["default"]
89+
assert parser.params == {"lang": "en", "max_depth": default_depth}
90+
91+
# A caller-supplied value wins over the seeded schema default.
92+
overridden = get_parser("mock", params={"max_depth": 7})
93+
assert overridden.params["max_depth"] == 7
8694

8795
@patch("hyperbase.parsers.entry_points")
8896
def test_plugin_load_failure(self, mock_eps):

0 commit comments

Comments
 (0)