TheAlgorithms/Python · error · TypeError

The parameter bwt_string type must be str.

Error message

The parameter bwt_string type must be str.

What it means

Raised by reverse_bwt() in data_compression/burrows_wheeler.py when bwt_string is not a str. The inverse transform repeatedly prepends characters and sorts, which requires str input. This is the first validation in the function, checked before emptiness and index validation.

Solutions

  1. Decode back to str before inverting: reverse_bwt(data.decode('utf-8'), idx) when data came back as bytes.
  2. Pass the exact values returned by bwt_transform() without intermediate conversions.
  3. Guard for None explicitly before calling.

Example fix

# before
reverse_bwt(payload['bwt'], payload['idx'])  # bwt is None/bytes

# after
bwt = payload['bwt'] or ''
if isinstance(bwt, bytes):
    bwt = bwt.decode('utf-8')
reverse_bwt(bwt, payload['idx'])
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(bwt_string, bytes):
    bwt_string = bwt_string.decode('utf-8')
if not isinstance(bwt_string, str):
    raise TypeError('bwt_string must be str')
plain = reverse_bwt(bwt_string, idx)

Type guard

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

Prevention

When it happens

Trigger: Calling reverse_bwt(None, 0), reverse_bwt(123, 0), reverse_bwt(['m','n','p'], 0), or passing bytes produced by a transport layer instead of the str returned by bwt_transform().

Common situations: Round-tripping a BWT result through a medium that changes its type (encoded to bytes for network/file storage, then not decoded), or deserializing from JSON where the field is missing and defaults to None.

Related errors


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

Appendix: source

Thrown at data_compression/burrows_wheeler.py:140

        ...
    TypeError: The parameter idx_original_string type must be int or passive
    of cast to int.
    >>> reverse_bwt("mnpbnnaaaaaa", -1)
    Traceback (most recent call last):
        ...
    ValueError: The parameter idx_original_string must not be lower than 0.
    >>> reverse_bwt("mnpbnnaaaaaa", 12) # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: The parameter idx_original_string must be lower than
    len(bwt_string).
    >>> reverse_bwt("mnpbnnaaaaaa", 11.0)
    'panamabanana'
    >>> reverse_bwt("mnpbnnaaaaaa", 11.4)
    'panamabanana'
    """
    if not isinstance(bwt_string, str):
        raise TypeError("The parameter bwt_string type must be str.")
    if not bwt_string:
        raise ValueError("The parameter bwt_string must not be empty.")
    try:
        idx_original_string = int(idx_original_string)
    except ValueError:
        raise TypeError(
            "The parameter idx_original_string type must be int or passive"
            " of cast to int."
        )
    if idx_original_string < 0:
        raise ValueError("The parameter idx_original_string must not be lower than 0.")
    if idx_original_string >= len(bwt_string):
        raise ValueError(
            "The parameter idx_original_string must be lower than len(bwt_string)."
        )

    ordered_rotations = [""] * len(bwt_string)
    for _ in range(len(bwt_string)):

View on GitHub (pinned to f5988cc097)