Skip to content
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
6 changes: 3 additions & 3 deletions .github/workflows/pypi-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
name: Build the distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ vars.PYTHON_VERSION }}
- name: Install Poetry
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Build Basilisp distributions
run: poetry build
- name: Upload build artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v6
with:
path: dist/
if-no-files-found: error
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/run-clojure-test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ jobs:
run-clojure-test-suite:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
with:
path: basilisp
- uses: actions/checkout@v4
- uses: actions/checkout@v5
with:
path: clojure-test-suite
repository: basilisp-lang/clojure-test-suite
- uses: actions/setup-python@v5
- uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install pip and dependencies
Expand Down
28 changes: 14 additions & 14 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ jobs:
version: '3.14'
tox-env: py314,py314-lint
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.version }}
- name: Cache dependencies
id: cache-deps
uses: actions/cache@v4
uses: actions/cache@v5
with:
path: |
.tox
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
for filename in .coverage.*; do mv "$filename" "coverage/$filename.py${{ matrix.version }}"; done;
- name: Archive code coverage results
if: "startsWith (matrix.os, 'ubuntu')"
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v6
with:
name: code-coverage.py${{ matrix.version }}
path: coverage/.coverage.*
Expand All @@ -95,13 +95,13 @@ jobs:
- version: '3.11'
tox-env: pypy311
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: pypy${{ matrix.version }}
- name: Cache dependencies
id: cache-deps
uses: actions/cache@v4
uses: actions/cache@v5
with:
path: |
.tox
Expand All @@ -127,7 +127,7 @@ jobs:
mkdir coverage
for filename in .coverage.*; do mv "$filename" "coverage/$filename.pypy${{ matrix.version }}"; done;
- name: Archive code coverage results
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v6
with:
name: code-coverage.pypy${{ matrix.version }}
path: coverage/.coverage.*
Expand All @@ -154,13 +154,13 @@ jobs:
version: ['3.10']
tox-env: ['py310']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.version }}
- name: Cache dependencies
id: cache-deps
uses: actions/cache@v4
uses: actions/cache@v5
with:
path: |
.tox
Expand Down Expand Up @@ -196,13 +196,13 @@ jobs:
- run-tests
- run-pypy-tests
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ vars.PYTHON_VERSION }}
- name: Cache dependencies
id: cache-deps
uses: actions/cache@v4
uses: actions/cache@v5
with:
path: |
.tox
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-pypi-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ jobs:
name: Build the distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ vars.PYTHON_VERSION }}
- name: Install Poetry
run: curl -sSL https://install.python-poetry.org | python3 -
- name: Build Basilisp distributions
run: poetry build
- name: Upload build artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v6
with:
path: dist/
if-no-files-found: error
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
* Add `test` and `tests` directories to `PYTHONPATH` automatically during `basilisp test` CLI invocations (#1069)

### Fixed
* Fix a bug where transient vectors were not callable and did not support `nth` (#1331)

## [v0.5.0]
### Added
* Added support for Python 3.14 (#1282)
Expand Down
20 changes: 13 additions & 7 deletions src/basilisp/lang/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,28 @@ class ICounted(Sized, ABC):
__slots__ = ()


class IIndexed(ICounted, ABC):
"""``IIndexed`` is a marker interface for types can be accessed by index.
K = TypeVar("K")
V = TypeVar("V")


class IIndexed(ICounted, Generic[V], ABC):
"""``IIndexed`` is an interface for types can be accessed by index.

Of the builtin collections, only Vectors are ``IIndexed`` . ``IIndexed`` types
respond ``True`` to the :lpy:fn:`indexed?` predicate.

.. seealso::

:lpy:fn:`indexed?`"""
:lpy:fn:`indexed?`, :lpy:fn:`nth`"""

__slots__ = ()

NTH_SENTINEL = object()

@abstractmethod
def nth(self, k: int, notfound: V | None = NTH_SENTINEL) -> V | None: # type: ignore[assignment]
raise NotImplementedError()


T_ExceptionInfo = TypeVar("T_ExceptionInfo", bound="IPersistentMap")

Expand All @@ -109,10 +119,6 @@ def data(self) -> T_ExceptionInfo:
raise NotImplementedError()


K = TypeVar("K")
V = TypeVar("V")


class IMapEntry(Generic[K, V], ABC):
"""``IMapEntry`` values are produced :lpy:fn:`seq` ing over any
:py:class:`IAssociative` (such as a Basilisp map).
Expand Down
23 changes: 13 additions & 10 deletions src/basilisp/lang/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
IAssociative,
IBlockingDeref,
IDeref,
IIndexed,
ILookup,
IMapEntry,
IPersistentCollection,
Expand Down Expand Up @@ -1437,39 +1438,41 @@ def _count_sized(coll: Sized):
return len(coll)


__nth_sentinel = object()


@functools.singledispatch
def nth(coll, i: int, notfound=__nth_sentinel):
def nth(coll, i: int, notfound=IIndexed.NTH_SENTINEL):
"""Returns the ith element of coll (0-indexed), if it exists.
None otherwise. If i is out of bounds, throws an IndexError unless
notfound is specified."""
raise TypeError(f"nth not supported on object of type {type(coll)}")


@nth.register(type(None))
def _nth_none(_: None, i: int, notfound=__nth_sentinel) -> None:
return notfound if notfound is not __nth_sentinel else None # type: ignore[return-value]
def _nth_none(_: None, i: int, notfound=IIndexed.NTH_SENTINEL) -> None:
return notfound if notfound is not IIndexed.NTH_SENTINEL else None # type: ignore[return-value]


@nth.register(Sequence)
def _nth_sequence(coll: Sequence, i: int, notfound=__nth_sentinel):
def _nth_sequence(coll: Sequence, i: int, notfound=IIndexed.NTH_SENTINEL):
try:
return coll[i]
except IndexError as ex:
if notfound is not __nth_sentinel:
if notfound is not IIndexed.NTH_SENTINEL:
return notfound
raise ex


@nth.register(IIndexed)
def _nth_iindexed(coll: IIndexed, i: int, notfound=IIndexed.NTH_SENTINEL):
return coll.nth(i, notfound=notfound)


@nth.register(ISeq)
def _nth_iseq(coll: ISeq, i: int, notfound=__nth_sentinel):
def _nth_iseq(coll: ISeq, i: int, notfound=IIndexed.NTH_SENTINEL):
for j, e in enumerate(coll):
if i == j:
return e

if notfound is not __nth_sentinel:
if notfound is not IIndexed.NTH_SENTINEL:
return notfound

raise IndexError(f"Index {i} out of bounds")
Expand Down
20 changes: 20 additions & 0 deletions src/basilisp/lang/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from basilisp.lang.interfaces import (
IEvolveableCollection,
IIndexed,
ILispObject,
IMapEntry,
IPersistentMap,
Expand Down Expand Up @@ -51,6 +52,9 @@ def __eq__(self, other):
def __len__(self):
return len(self._inner)

def __call__(self, k: int) -> T | None:
return self._inner[k]

def cons_transient(self, *elems: T) -> "TransientVector[T]": # type: ignore[override]
for elem in elems:
self._inner.append(elem)
Expand Down Expand Up @@ -82,6 +86,14 @@ def val_at(self, k: int, default=None):
except IndexError:
return default

def nth(self, k: int, notfound=IIndexed.NTH_SENTINEL):
try:
return self._inner[k]
except IndexError:
if notfound is not IIndexed.NTH_SENTINEL:
return notfound
raise

def pop_transient(self) -> "TransientVector[T]":
if len(self) == 0:
raise IndexError("Cannot pop an empty vector")
Expand Down Expand Up @@ -202,6 +214,14 @@ def val_at(self, k: int, default: T | None = None) -> T | None:
except (IndexError, TypeError):
return default

def nth(self, k: int, notfound=IIndexed.NTH_SENTINEL):
try:
return self._inner[k]
except IndexError:
if notfound is not IIndexed.NTH_SENTINEL:
return notfound
raise

def empty(self) -> "PersistentVector[T]":
return EMPTY.with_meta(self._meta)

Expand Down
22 changes: 21 additions & 1 deletion tests/basilisp/core/test_transients.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@
[]
[:a :b :c]))

(testing "callable"
(are [v idx res] (= res ((transient v) idx))
[1] 0 1
[:a :b :c] 1 :b)

(are [v idx] (thrown? python/IndexError ((transient v) idx))
[] 0
[] 1
[:a :b :c] 4))

(testing "count"
(let [trx (volatile! (transient [:a :b :c]))]
(is (= 3 (count @trx)))
Expand All @@ -116,11 +126,21 @@

(testing "get"
(are [x y z] (= z (get (transient x) y))
[] 1 nil
[] 1 nil
[:a :b :c] 1 :b)

(is (= :e (get (transient [:a :b :c]) 4 :e))))

(testing "nth"
(are [v idx res] (= res (nth (transient v) idx))
[1] 0 1
[:a :b :c] 1 :b)

(are [v idx] (thrown? python/IndexError (nth (transient v) idx))
[] 0
[] 1
[:a :b :c] 4))

(testing "not seqable"
Comment thread
chrisrink10 marked this conversation as resolved.
(is (thrown? python/TypeError (seq (transient []))))))

Expand Down
38 changes: 38 additions & 0 deletions tests/basilisp/vector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,44 @@ def test_val_at():
assert "default" == vec.EMPTY.val_at("key", "default")


@pytest.mark.parametrize(
"v,i", [(vec.v("a", "b"), 2), (vec.EMPTY, 0), (vec.EMPTY, 1), (vec.EMPTY, -1)]
)
def test_nth_invalid_index(v: vec.PersistentVector, i):
with pytest.raises(IndexError):
v.nth(i)


@pytest.mark.parametrize(
"res,v,i",
[("a", vec.v("a"), 0), ("b", vec.v("a", "b"), 1), ("b", vec.v("a", "b"), -1)],
)
def test_nth(res, v: vec.PersistentVector, i: int):
assert res == v.nth(i)


@pytest.mark.parametrize(
"res,v,i,default",
[
("other", vec.EMPTY, -1, "other"),
("other", vec.v("a"), 1, "other"),
("b", vec.v("a", "b"), 1, "other"),
("other", vec.v("a", "b"), 8, "other"),
("b", vec.v("a", "b"), -1, "other"),
],
)
def test_nth_default(res, v: vec.PersistentVector, i: int, default):
assert res == v.nth(i, notfound=default)


def test_nth_invalid_keys():
with pytest.raises(TypeError):
vec.EMPTY.nth(keyword("key"))

with pytest.raises(TypeError):
vec.EMPTY.nth(keyword("blah"), "default")


def test_peek():
assert None is vec.v().peek()

Expand Down
Loading