TheAlgorithms/Python · error · TypeError

The parameter s type must be str.

Error message

The parameter s type must be str.

What it means

Raised by all_rotations() in data_compression/burrows_wheeler.py when the input parameter s is not a Python str. The function builds every rotation of a string (s[i:] + s[:i] for each i), which only works on str objects. It validates with isinstance(s, str) before doing any work and rejects everything else, including bytes and lists.

Source

Thrown at data_compression/burrows_wheeler.py:49

    >>> all_rotations("^BANANA|") # doctest: +NORMALIZE_WHITESPACE
    ['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA',
    'A|^BANAN', '|^BANANA']
    >>> all_rotations("a_asa_da_casa") # doctest: +NORMALIZE_WHITESPACE
    ['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a',
    'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d',
    '_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca',
    'aa_asa_da_cas']
    >>> all_rotations("panamabanana") # doctest: +NORMALIZE_WHITESPACE
    ['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan',
    'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab',
    'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan']
    >>> all_rotations(5)
    Traceback (most recent call last):
        ...
    TypeError: The parameter s type must be str.
    """
    if not isinstance(s, str):
        raise TypeError("The parameter s type must be str.")

    return [s[i:] + s[:i] for i in range(len(s))]


def bwt_transform(s: str) -> BWTTransformDict:
    """
    :param s: The string that will be used at bwt algorithm
    :return: the string composed of the last char of each row of the ordered
    rotations and the index of the original string at ordered rotations list
    :raises TypeError: If the s parameter type is not str
    :raises ValueError: If the s parameter is empty
    Examples:

    >>> bwt_transform("^BANANA")
    {'bwt_string': 'BNN^AAA', 'idx_original_string': 6}
    >>> bwt_transform("a_asa_da_casa")
    {'bwt_string': 'aaaadss_c__aa', 'idx_original_string': 3}
    >>> bwt_transform("panamabanana")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert the value to str before calling: all_rotations(str(value)).
  2. If reading from files/network, decode bytes first: all_rotations(raw.decode('utf-8')).
  3. Add an isinstance check at the call site to fail early with a clearer message for your own layer.

Example fix

# before
all_rotations(5)  # TypeError

# after
all_rotations(str(5))  # ['5']
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(s, str):
    s = str(s)
rotations = all_rotations(s)

Type guard

def is_str(value: object) -> bool:
    return isinstance(value, str)

Prevention

When it happens

Trigger: Calling all_rotations(5), all_rotations(['a','b']), all_rotations(b'abc'), or passing a value read from a non-string source (e.g. an int loop counter or a deserialized JSON number) instead of a str.

Common situations: Feeding data parsed from JSON or user input where a number slips through, passing bytes from file/network reads without decoding, or test code that passes integers as shown in the doctest all_rotations(5).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/086a7c487467767a. Report an issue: GitHub.