mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Only convert to an indexable sequence if absolutely necessary
This commit is contained in:
committed by
Rodrigo Girão Serrão
parent
948cb6676f
commit
274bb634ca
@@ -1,7 +1,7 @@
|
|||||||
"""Provides collection-based utility code."""
|
"""Provides collection-based utility code."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Generic, TypeVar, Iterator, overload, Iterable
|
from typing import Generic, TypeVar, Iterator, overload, Iterable, Sequence
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ class ImmutableSequence(Generic[T]):
|
|||||||
Args:
|
Args:
|
||||||
wrap (Iterable[T]): The iterable value being wrapped.
|
wrap (Iterable[T]): The iterable value being wrapped.
|
||||||
"""
|
"""
|
||||||
self._wrap = tuple(wrap)
|
self._wrap = wrap if isinstance(wrap, Sequence) else tuple(wrap)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __getitem__(self, index: int) -> T:
|
def __getitem__(self, index: int) -> T:
|
||||||
|
|||||||
@@ -57,10 +57,11 @@ def test_get_item_from_immutable_sequence() -> None:
|
|||||||
assert wrap(range(10))[0] == 0
|
assert wrap(range(10))[0] == 0
|
||||||
assert wrap(range(10))[-1] == 9
|
assert wrap(range(10))[-1] == 9
|
||||||
|
|
||||||
|
|
||||||
def test_get_slice_from_immutable_sequence() -> None:
|
def test_get_slice_from_immutable_sequence() -> None:
|
||||||
"""It should be possible to get a slice from an immutable sequence."""
|
"""It should be possible to get a slice from an immutable sequence."""
|
||||||
assert list(wrap(range(10))[0:2]) == [0,1]
|
assert list(wrap(iter(range(10)))[0:2]) == [0,1]
|
||||||
assert list(wrap(range(10))[0:-1]) == [0,1,2,3,4,5,6,7,8]
|
assert list(wrap(iter(range(10)))[0:-1]) == [0,1,2,3,4,5,6,7,8]
|
||||||
|
|
||||||
|
|
||||||
def test_immutable_sequence_contains() -> None:
|
def test_immutable_sequence_contains() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user