-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Hide lists and function tensors behind experimental flag (#501)
Closes #437. BREAKING CHANGE: Lists and function tensors are no longer available by default. `guppylang.enable_experimental_features()` must be called before compilation to enable them. --------- Co-authored-by: Alan Lawrence <[email protected]>
- Loading branch information
Showing
19 changed files
with
232 additions
and
2 deletions.
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
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
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,73 @@ | ||
from ast import expr | ||
from types import TracebackType | ||
|
||
from guppylang.ast_util import AstNode | ||
from guppylang.error import GuppyError | ||
|
||
EXPERIMENTAL_FEATURES_ENABLED = False | ||
|
||
|
||
class enable_experimental_features: | ||
"""Enables experimental Guppy features. | ||
Can be used as a context manager to enable experimental features in a `with` block. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
global EXPERIMENTAL_FEATURES_ENABLED | ||
self.original = EXPERIMENTAL_FEATURES_ENABLED | ||
EXPERIMENTAL_FEATURES_ENABLED = True | ||
|
||
def __enter__(self) -> None: | ||
pass | ||
|
||
def __exit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc_val: BaseException | None, | ||
exc_tb: TracebackType | None, | ||
) -> None: | ||
global EXPERIMENTAL_FEATURES_ENABLED | ||
EXPERIMENTAL_FEATURES_ENABLED = self.original | ||
|
||
|
||
class disable_experimental_features: | ||
"""Disables experimental Guppy features. | ||
Can be used as a context manager to enable experimental features in a `with` block. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
global EXPERIMENTAL_FEATURES_ENABLED | ||
self.original = EXPERIMENTAL_FEATURES_ENABLED | ||
EXPERIMENTAL_FEATURES_ENABLED = False | ||
|
||
def __enter__(self) -> None: | ||
pass | ||
|
||
def __exit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc_val: BaseException | None, | ||
exc_tb: TracebackType | None, | ||
) -> None: | ||
global EXPERIMENTAL_FEATURES_ENABLED | ||
EXPERIMENTAL_FEATURES_ENABLED = self.original | ||
|
||
|
||
def check_function_tensors_enabled(node: expr | None = None) -> None: | ||
if not EXPERIMENTAL_FEATURES_ENABLED: | ||
raise GuppyError( | ||
"Function tensors are an experimental feature. Use " | ||
"`guppylang.enable_experimental_features()` to enable them.", | ||
node, | ||
) | ||
|
||
|
||
def check_lists_enabled(loc: AstNode | None = None) -> None: | ||
if not EXPERIMENTAL_FEATURES_ENABLED: | ||
raise GuppyError( | ||
"Lists are an experimental feature and not fully supported yet. Use " | ||
"`guppylang.enable_experimental_features()` to enable them.", | ||
loc, | ||
) |
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
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,7 @@ | ||
Guppy compilation failed. Error in file $FILE:19 | ||
|
||
17: @guppy(module) | ||
18: def main() -> tuple[int, int]: | ||
19: return (f, g)(1, 2) | ||
^^^^^^ | ||
GuppyError: Function tensors are an experimental feature. Use `guppylang.enable_experimental_features()` to enable them. |
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,22 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
|
||
module = GuppyModule("test") | ||
|
||
|
||
@guppy(module) | ||
def f(x: int) -> int: | ||
return x | ||
|
||
|
||
@guppy(module) | ||
def g(x: int) -> int: | ||
return x | ||
|
||
|
||
@guppy(module) | ||
def main() -> tuple[int, int]: | ||
return (f, g)(1, 2) | ||
|
||
|
||
module.compile() |
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,6 @@ | ||
Guppy compilation failed. Error in file $FILE:9 | ||
|
||
7: @guppy(module) | ||
8: def main(x: linst[int]) -> linst[int]: | ||
^^^^^^^^^^ | ||
GuppyError: Lists are an experimental feature and not fully supported yet. Use `guppylang.enable_experimental_features()` to enable them. |
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,13 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.prelude.builtins import linst | ||
from guppylang.module import GuppyModule | ||
|
||
module = GuppyModule("test") | ||
|
||
|
||
@guppy(module) | ||
def main(x: linst[int]) -> linst[int]: | ||
return x | ||
|
||
|
||
module.compile() |
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,7 @@ | ||
Guppy compilation failed. Error in file $FILE:9 | ||
|
||
7: @guppy(module) | ||
8: def main() -> None: | ||
9: [i for i in range(10)] | ||
^^^^^^^^^^^^^^^^^^^^^^ | ||
GuppyError: Lists are an experimental feature and not fully supported yet. Use `guppylang.enable_experimental_features()` to enable them. |
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,12 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
|
||
module = GuppyModule("test") | ||
|
||
|
||
@guppy(module) | ||
def main() -> None: | ||
[i for i in range(10)] | ||
|
||
|
||
module.compile() |
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,7 @@ | ||
Guppy compilation failed. Error in file $FILE:9 | ||
|
||
7: @guppy(module) | ||
8: def main() -> None: | ||
9: [1, 2, 3] | ||
^^^^^^^^^ | ||
GuppyError: Lists are an experimental feature and not fully supported yet. Use `guppylang.enable_experimental_features()` to enable them. |
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,12 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
|
||
module = GuppyModule("test") | ||
|
||
|
||
@guppy(module) | ||
def main() -> None: | ||
[1, 2, 3] | ||
|
||
|
||
module.compile() |
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,6 @@ | ||
Guppy compilation failed. Error in file $FILE:8 | ||
|
||
6: @guppy(module) | ||
7: def main(x: list[int]) -> list[int]: | ||
^^^^^^^^^ | ||
GuppyError: Lists are an experimental feature and not fully supported yet. Use `guppylang.enable_experimental_features()` to enable them. |
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,12 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
|
||
module = GuppyModule("test") | ||
|
||
|
||
@guppy(module) | ||
def main(x: list[int]) -> list[int]: | ||
return x | ||
|
||
|
||
module.compile() |
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,21 @@ | ||
import pathlib | ||
import pytest | ||
|
||
from guppylang.experimental import disable_experimental_features | ||
from tests.error.util import run_error_test | ||
|
||
path = pathlib.Path(__file__).parent.resolve() / "experimental_errors" | ||
files = [ | ||
x | ||
for x in path.iterdir() | ||
if x.is_file() and x.suffix == ".py" and x.name != "__init__.py" | ||
] | ||
|
||
# Turn paths into strings, otherwise pytest doesn't display the names | ||
files = [str(f) for f in files] | ||
|
||
|
||
@pytest.mark.parametrize("file", files) | ||
def test_experimental_errors(file, capsys): | ||
with disable_experimental_features(): | ||
run_error_test(file, capsys) |