Skip to content

Commit

Permalink
clarify UnknownIdentifier error message when namespace does exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Apr 9, 2021
1 parent 5b66f2f commit 1ffdaad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions WDL/Error.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ def __init__(self, node: SourceNode, message: str) -> None:


class UnknownIdentifier(ValidationError):
def __init__(self, node: SourceNode) -> None:
def __init__(self, node: SourceNode, message: Optional[str] = None) -> None:
# avoiding circular dep:
# assert isinstance(node, WDL.Expr.Ident)
super().__init__(node, "Unknown identifier " + str(node))
if not message:
message = "Unknown identifier " + str(node)
super().__init__(node, message)


class NoSuchInput(ValidationError):
Expand Down
6 changes: 5 additions & 1 deletion WDL/Expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,11 @@ def _infer_type(self, type_env: Env.Bindings[Type.Base]) -> Type.Base:
# attempt to resolve "expr.member" and if that works, transform
# expr to Ident("expr.member")
if self.expr._ident + "." + self.member not in type_env:
raise Error.UnknownIdentifier(self) from None
message = None
if type_env.has_namespace(self.expr._ident):
# specific error message when namespace exists but member doesn't
message = f"No {self.member} in namespace {self.expr._ident}"
raise Error.UnknownIdentifier(self, message=message) from None
self.expr = Ident(self.pos, self._ident)
self.expr.infer_type(type_env, self._stdlib, self._check_quant)
self.member = None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_1doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ def test_errors(self):
}
"""
doc = WDL.parse_document(doc)
with self.assertRaises(WDL.Error.UnknownIdentifier):
with self.assertRaisesRegex(WDL.Error.UnknownIdentifier, " in namespace "):
doc.typecheck()

doc = r"""
Expand Down

0 comments on commit 1ffdaad

Please sign in to comment.