Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/array-compr
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-koch committed Nov 5, 2024
2 parents 37a7118 + 2e9da6b commit 18f8d87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions guppylang/prelude/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@ class SizedIter:
Annotating an iterator with an incorrect size is undefined behaviour.
"""

def __class_getitem__(cls, item: Any) -> type:
# Dummy implementation to allow subscripting of the `SizedIter` type in
# positions that are evaluated by the Python interpreter
return cls

@guppy.custom(NoopCompiler())
def __new__(iterator: L @ owned) -> "SizedIter[L, n]": # type: ignore[type-arg]
"""Casts an iterator into a `SizedIter`."""
Expand Down
22 changes: 21 additions & 1 deletion tests/integration/test_range.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from guppylang.decorator import guppy
from guppylang.prelude.builtins import nat, range
from guppylang.prelude.builtins import nat, range, SizedIter, Range
from guppylang.module import GuppyModule
from tests.util import compile_guppy

Expand All @@ -20,7 +20,27 @@ def negative() -> int:
total += 100 + x
return total

@guppy(module)
def non_static() -> int:
total = 0
n = 4
for x in range(n + 1):
total += x + 100 # Make the initial 0 obvious
return total

compiled = module.compile()
validate(compiled)
run_int_fn(compiled, expected=510)
run_int_fn(compiled, expected=0, fn_name="negative")
run_int_fn(compiled, expected=510, fn_name="non_static")


def test_static_size(validate):
module = GuppyModule("test")

@guppy(module)
def negative() -> SizedIter[Range, 10]:
return range(10)

validate(module.compile())

0 comments on commit 18f8d87

Please sign in to comment.