Skip to content
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

adding a warning about iteration over words #38585

Merged
merged 1 commit into from
Sep 15, 2024
Merged
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
33 changes: 22 additions & 11 deletions src/sage/combinat/words/words.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
rk = self.alphabet().rank
return rk(letter1)

def __eq__(self, other):
def __eq__(self, other) -> bool:
r"""
TESTS::

Expand All @@ -309,7 +309,7 @@
return self is other or (type(self) is type(other) and
self.alphabet() == other.alphabet())

def __ne__(self, other):
def __ne__(self, other) -> bool:
r"""
TESTS::

Expand Down Expand Up @@ -479,16 +479,17 @@
return self._element_classes['list'](self, data)

from sage.combinat.words.word_datatypes import (WordDatatype_str,
WordDatatype_list, WordDatatype_tuple)
WordDatatype_list,
WordDatatype_tuple)
if isinstance(data, WordDatatype_str):
return self._element_classes['str'](self, data._data)
if isinstance(data, WordDatatype_tuple):
return self._element_classes['tuple'](self, data._data)
if isinstance(data, WordDatatype_list):
return self._element_classes['list'](self, data._data)

from sage.combinat.words.word_infinite_datatypes import (WordDatatype_callable,
WordDatatype_iter)
from sage.combinat.words.word_infinite_datatypes import \
(WordDatatype_callable, WordDatatype_iter)
if isinstance(data, WordDatatype_callable):
length = data.length()
data = data._func
Expand Down Expand Up @@ -849,7 +850,7 @@
self._check(w)
return w

def _repr_(self):
def _repr_(self) -> str:
"""
EXAMPLES::

Expand All @@ -875,14 +876,14 @@
"""
try:
some_letters = list(self.alphabet().some_elements())
except Exception:
except (TypeError, ValueError, AttributeError, NotImplementedError):

Check warning on line 879 in src/sage/combinat/words/words.py

View check run for this annotation

Codecov / codecov/patch

src/sage/combinat/words/words.py#L879

Added line #L879 was not covered by tests
return self([])

if len(some_letters) == 1:
return self([some_letters[0]] * 3)
else:
a, b = some_letters[:2]
return self([b, a, b])

a, b = some_letters[:2]
return self([b, a, b])

def iterate_by_length(self, l=1):
r"""
Expand Down Expand Up @@ -912,10 +913,20 @@
Traceback (most recent call last):
...
TypeError: the parameter l (='a') must be an integer

TESTS::

sage: W = FiniteWords(NN)
sage: list(W.iterate_by_length(1))
Traceback (most recent call last):
...
NotImplementedError: cannot iterate over words for infinite alphabets
"""
if not isinstance(l, (int, Integer)):
raise TypeError("the parameter l (=%r) must be an integer" % l)
cls = self._element_classes['tuple']
if not self.alphabet().is_finite():
raise NotImplementedError("cannot iterate over words for infinite alphabets")
for w in itertools.product(self.alphabet(), repeat=l):
yield cls(self, w)

Expand Down Expand Up @@ -961,7 +972,7 @@
for l in itertools.count():
yield from self.iterate_by_length(l)

def __contains__(self, x):
def __contains__(self, x) -> bool:
"""
Test whether ``self`` contains ``x``.

Expand Down
Loading