Skip to content

Commit

Permalink
fix: update error message in case a name inherits from multiple types
Browse files Browse the repository at this point in the history
This should make the error more clear to the user: instead of just saying that the name is "already present" in the list, we clarify that the problem is that not only it occurs, but it is because it occurs (again) as inheriting name.
  • Loading branch information
marcofavorito committed Jun 13, 2023
1 parent a1ec5b3 commit 79c0d24
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pddl/parser/typed_list_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ def _check_item_name_already_present(self, item_name: name) -> None:
"""
if item_name in self._item_to_types:
types_list = sorted(map(str, self._item_to_types[item_name]))
types_list_str = f" with types {types_list}" if len(types_list) > 0 else ""
if len(types_list) > 0:
raise ValueError(
f"duplicate name '{item_name}' in typed list already inherits from types {types_list}"
)
raise ValueError(
f"duplicate name '{item_name}' in typed list already present"
+ types_list_str
)

def _check_tags_already_present(
Expand Down
35 changes: 31 additions & 4 deletions tests/test_parser/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ def test_hierarchical_types() -> None:
}


def test_hierarchical_types_2() -> None:
"""Test correct parsing of hierarchical types, Storage domain."""
domain_str = dedent(
"""
(define (domain logistics)
(:requirements :strips :typing)
(:types hoist surface place area - object
container depot - place
storearea transitarea - area
crate - surface)
)
"""
)
domain = DomainParser()(domain_str)
assert domain.types == {
"hoist": "object",
"surface": "object",
"place": "object",
"area": "object",
"container": "place",
"depot": "place",
"storearea": "area",
"transitarea": "area",
"crate": "surface",
}


def test_types_repetition_in_simple_typed_lists_not_allowed() -> None:
"""Check types repetition in simple typed lists is detected and a parsing error is raised."""
domain_str = dedent(
Expand All @@ -69,7 +96,7 @@ def test_types_repetition_in_simple_typed_lists_not_allowed() -> None:

with pytest.raises(
lark.exceptions.VisitError,
match=".*error while parsing tokens \\['a', 'b', 'c', 'a'\\]: "
match=r".*error while parsing tokens \['a', 'b', 'c', 'a'\]: "
"duplicate name 'a' in typed list already present",
):
DomainParser()(domain_str)
Expand All @@ -88,8 +115,8 @@ def test_types_repetition_in_typed_lists_not_allowed() -> None:

with pytest.raises(
lark.exceptions.VisitError,
match=".*error while parsing tokens \\['a', '-', 't1', 'b', 'c', '-', 't2', 'a', '-', 't3'\\]: "
"duplicate name 'a' in typed list already present with types \\['t1'\\]",
match=r".*error while parsing tokens \['a', '-', 't1', 'b', 'c', '-', 't2', 'a', '-', 't3'\]: "
r"duplicate name 'a' in typed list already inherits from types \['t1'\]",
):
DomainParser()(domain_str)

Expand Down Expand Up @@ -167,7 +194,7 @@ def test_constants_repetition_in_typed_lists_not_allowed() -> None:
with pytest.raises(
lark.exceptions.VisitError,
match=".*error while parsing tokens \\['c1', '-', 't1', 'c1', '-', 't2'\\]: "
"duplicate name 'c1' in typed list already present with types \\['t1'\\]",
"duplicate name 'c1' in typed list already inherits from types \\['t1'\\]",
):
DomainParser()(domain_str)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser/test_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_problem_objects_repetition_in_typed_lists_not_allowed() -> None:
with pytest.raises(
lark.exceptions.VisitError,
match=r".*error while parsing tokens \['a', '-', 't1', 'b', '-', 't2', 'c', '-', 't3', 'a', '-', 't4'\]: "
r"duplicate name 'a' in typed list already present with types \['t1'\]",
r"duplicate name 'a' in typed list already inherits from types \['t1'\]",
):
ProblemParser()(problem_str)

Expand Down

0 comments on commit 79c0d24

Please sign in to comment.