-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: Update remaining code to use new diagnostics #606
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
04282ac
feat: Update top-level definitions to use new diagnostics
mark-koch ac682d3
feat: Update type parsing to use new diagnostics
mark-koch 611a006
feat: Update remaining code to use new diagnostics
mark-koch 0b3a662
Set singular=True
mark-koch 978e0f2
Refactor zero check
mark-koch 2b23832
Register default child diagnostics in __post_init__
mark-koch afe8801
Move custom function parsing out of decorator and add test
mark-koch d8b5c78
Add NoSignatureError.Suggestion by default
mark-koch a7f6aa1
Improve coverage
mark-koch 52e7221
Merge branch 'diag/defs' into diag/tys
mark-koch d0950c9
Merge branch 'diag/defs' into diag/rest
mark-koch a6f615a
Make suggestions default child diagnostics
mark-koch 575d373
Merge branch 'diag/tys' into diag/rest
mark-koch 2b69b80
Merge remote-tracking branch 'origin/feat/diagnostics' into diag/rest
mark-koch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from dataclasses import dataclass | ||
from typing import ClassVar | ||
|
||
from guppylang.diagnostic import Error | ||
|
||
|
||
@dataclass(frozen=True) | ||
class UnsupportedError(Error): | ||
title: ClassVar[str] = "Unsupported" | ||
span_label: ClassVar[str] = "{things} {is_are} not supported{extra}" | ||
things: str | ||
singular: bool = False | ||
unsupported_in: str = "" | ||
|
||
@property | ||
def is_are(self) -> str: | ||
return "is" if self.singular else "are" | ||
|
||
@property | ||
def extra(self) -> str: | ||
return f" in {self.unsupported_in}" if self.unsupported_in else "" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class UnexpectedError(Error): | ||
title: ClassVar[str] = "Unexpected {things}" | ||
span_label: ClassVar[str] = "Unexpected {things}{extra}" | ||
things: str | ||
unexpected_in: str = "" | ||
|
||
@property | ||
def extra(self) -> str: | ||
return f" in {self.unexpected_in}" if self.unexpected_in else "" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExpectedError(Error): | ||
title: ClassVar[str] = "Expected {things}" | ||
span_label: ClassVar[str] = "Expected {things}{extra}" | ||
things: str | ||
got: str = "" | ||
|
||
@property | ||
def extra(self) -> str: | ||
return f", got {self.got}" if self.got else "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from hugr.package import FuncDefnPointer, ModulePointer | ||
|
||
import guppylang | ||
from guppylang.ast_util import annotate_location, has_empty_body | ||
from guppylang.ast_util import annotate_location | ||
from guppylang.definition.common import DefId, Definition | ||
from guppylang.definition.const import RawConstDef | ||
from guppylang.definition.custom import ( | ||
|
@@ -33,7 +33,7 @@ | |
from guppylang.definition.parameter import ConstVarDef, TypeVarDef | ||
from guppylang.definition.struct import RawStructDef | ||
from guppylang.definition.ty import OpaqueTypeDef, TypeDef | ||
from guppylang.error import GuppyError, MissingModuleError, pretty_errors | ||
from guppylang.error import MissingModuleError, pretty_errors | ||
from guppylang.ipython_inspect import get_ipython_globals, is_running_ipython | ||
from guppylang.module import ( | ||
GuppyModule, | ||
|
@@ -149,7 +149,7 @@ def _get_python_caller(self, fn: PyFunc | None = None) -> ModuleIdentifier: | |
break | ||
frame = frame.f_back | ||
else: | ||
raise GuppyError("Could not find a caller for the `@guppy` decorator") | ||
raise RuntimeError("Could not find a caller for the `@guppy` decorator") | ||
|
||
# Jupyter notebook cells all get different dummy filenames. However, | ||
# we want the whole notebook to correspond to a single implicit | ||
|
@@ -173,7 +173,7 @@ def init_module(self, import_builtins: bool = True) -> None: | |
module_id = self._get_python_caller() | ||
if module_id in self._modules: | ||
msg = f"Module {module_id.name} is already initialised" | ||
raise GuppyError(msg) | ||
raise ValueError(msg) | ||
self._modules[module_id] = GuppyModule(module_id.name, import_builtins) | ||
|
||
@pretty_errors | ||
|
@@ -310,11 +310,6 @@ def custom( | |
|
||
def dec(f: PyFunc) -> RawCustomFunctionDef: | ||
func_ast, docstring = parse_py_func(f, self._sources) | ||
if not has_empty_body(func_ast): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's going on here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this check to |
||
raise GuppyError( | ||
"Body of custom function declaration must be empty", | ||
func_ast.body[0], | ||
) | ||
call_checker = checker or DefaultCallChecker() | ||
func = RawCustomFunctionDef( | ||
DefId.fresh(mod), | ||
|
@@ -432,7 +427,7 @@ def get_module( | |
other_module = find_guppy_module_in_py_module(value) | ||
if other_module and other_module != module: | ||
defs[x] = value | ||
except GuppyError: | ||
except ValueError: | ||
pass | ||
module.load(**defs) | ||
return module | ||
|
@@ -453,7 +448,7 @@ def compile_function(self, f_def: RawFunctionDef) -> FuncDefnPointer: | |
"""Compiles a single function definition.""" | ||
module = f_def.id.module | ||
if not module: | ||
raise GuppyError("Function definition must belong to a module") | ||
raise ValueError("Function definition must belong to a module") | ||
compiled_module = module.compile() | ||
assert module._compiled is not None, "Module should be compiled" | ||
globs = module._compiled.globs | ||
|
@@ -482,7 +477,7 @@ def _parse_expr_string(ty_str: str, parse_err: str, sources: SourceMap) -> ast.e | |
try: | ||
expr_ast = ast.parse(ty_str, mode="eval").body | ||
except SyntaxError: | ||
raise GuppyError(parse_err) from None | ||
raise SyntaxError(parse_err) from None | ||
|
||
# Try to annotate the type AST with source information. This requires us to | ||
# inspect the stack frame of the caller | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guppy errors are now reserved for things that go wrong during compilation. This error is thrown immediately when the user calls the decorator, so we shouldn't use
GuppyError
Same for other errors in this file