From cb6b8694901b3ad2abc860ae27096d915cff2773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Thu, 29 Aug 2024 16:50:03 +0200 Subject: [PATCH] adding a warning about iteration over words --- src/sage/combinat/words/words.py | 33 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/sage/combinat/words/words.py b/src/sage/combinat/words/words.py index c230c90057d..f33c868d636 100644 --- a/src/sage/combinat/words/words.py +++ b/src/sage/combinat/words/words.py @@ -293,7 +293,7 @@ def _sortkey_letters(self, letter1): rk = self.alphabet().rank return rk(letter1) - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" TESTS:: @@ -309,7 +309,7 @@ def __eq__(self, other): 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:: @@ -479,7 +479,8 @@ def _word_from_word(self, data): 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): @@ -487,8 +488,8 @@ def _word_from_word(self, 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 @@ -849,7 +850,7 @@ def __call__(self, data=None, length=None, datatype=None, caching=True, check=Tr self._check(w) return w - def _repr_(self): + def _repr_(self) -> str: """ EXAMPLES:: @@ -875,14 +876,14 @@ def _an_element_(self): """ try: some_letters = list(self.alphabet().some_elements()) - except Exception: + except (TypeError, ValueError, AttributeError, NotImplementedError): 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""" @@ -912,10 +913,20 @@ def iterate_by_length(self, l=1): 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) @@ -961,7 +972,7 @@ def __iter__(self): 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``.