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 hint to partial path errors if anchor is available. #8380

Merged
merged 1 commit into from
Feb 25, 2025
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
28 changes: 27 additions & 1 deletion edb/edgeql/compiler/setgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,35 @@ def compile_path(expr: qlast.Path, *, ctx: context.ContextLevel) -> irast.Set:
if ctx.partial_path_prefix is not None:
path_tip = ctx.partial_path_prefix
else:
hint = None

# If there are anchors, suggest one
if anchors:
anchor_names: list[str] = [
key if isinstance(key, str) else key.name
for key in anchors
]

import edb.edgeql.codegen
suggestion = (
f'{anchor_names[0]}'
f'{edb.edgeql.codegen.generate_source(expr)}'
)

if len(anchor_names) == 1:
hint = (
f'Did you mean {suggestion}?'
)
else:
hint = (
f'Did you mean to use one of: {anchor_names}? '
f'eg. {suggestion}'
)

raise errors.QueryError(
'could not resolve partial path ',
span=expr.span
span=expr.span,
hint=hint
)

computables: list[irast.Set] = []
Expand Down
5 changes: 4 additions & 1 deletion tests/test_edgeql_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2609,7 +2609,10 @@ async def test_edgeql_expr_paths_03(self):
# syntactically legal (see test_edgeql_syntax_constants_09),
# but will fail to resolve to anything.
with self.assertRaisesRegex(
edgedb.QueryError, r'could not resolve partial path'):
edgedb.QueryError,
r'could not resolve partial path',
_hint=None
):
await self.con.execute(r"""
SELECT .1;
""")
Expand Down
1 change: 1 addition & 0 deletions tests/test_edgeql_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ async def test_edgeql_insert_fail_06(self):
with self.assertRaisesRegex(
edgedb.QueryError,
r"could not resolve partial path",
_hint=None
):
await self.con.execute('''
INSERT Person { name := .name };
Expand Down
12 changes: 8 additions & 4 deletions tests/test_edgeql_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,8 +1292,10 @@ async def test_edgeql_select_limit_07(self):

async def test_edgeql_select_limit_08(self):
with self.assertRaisesRegex(
edgedb.QueryError,
r'could not resolve partial path'):
edgedb.QueryError,
r'could not resolve partial path',
_hint=None
):

await self.con.query("""
SELECT
Expand All @@ -1303,8 +1305,10 @@ async def test_edgeql_select_limit_08(self):

async def test_edgeql_select_limit_09(self):
with self.assertRaisesRegex(
edgedb.QueryError,
r'could not resolve partial path'):
edgedb.QueryError,
r'could not resolve partial path',
_hint=None
):

await self.con.query("""
SELECT
Expand Down
22 changes: 20 additions & 2 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,11 @@ def test_schema_deletion_policy_on_prop_02(self):
};
"""

@tb.must_fail(errors.QueryError,
"could not resolve partial path")
@tb.must_fail(
errors.QueryError,
"could not resolve partial path",
hint="Did you mean __source__.name?"
)
def test_schema_partial_path_in_default_of_link_prop_01(self):
"""
module default {
Expand All @@ -475,6 +478,21 @@ def test_schema_partial_path_in_default_of_link_prop_01(self):
}
"""

@tb.must_fail(
errors.QueryError,
"could not resolve partial path",
hint="Did you mean __new__.wow?"
)
def test_schema_partial_path_in_trigger_01(self):
"""
type Foo {
property wow: bool;
trigger prohibit_queue after insert for each do (
select assert(.wow, message := "wow!")
);
}
"""

@tb.must_fail(errors.InvalidPropertyTargetError,
"invalid property type: expected a scalar type, "
"or a scalar collection, got object type 'test::Object'",
Expand Down