Skip to content

convert_string_to_list silently drops negative integers and non-digit entries #3818

Description

@emilycartr

backend/utils/str_utils.py:27-39:

def convert_string_to_list(items_str: Optional[str]) -> List[int]:
    """Convert comma-separated string to list of integers for processing"""
    if not items_str or items_str.strip() == "":
        return []
    return [int(item.strip()) for item in items_str.split(",") if item.strip().isdigit()]

Two related bugs in one line:

  1. str.isdigit() returns False for "-1", "+1", and " 3 " (after strip() only stripped outer whitespace — fine — but for any digit-with-sign it's False). The companion convert_list_to_string (line 12) happily emits a negative integer; round-tripping a list containing -1 therefore drops it silently.
  2. Non-digit garbage like "a,1,b,2" is silently filtered to [1, 2]. There's no warning, no exception, and no log line — the caller has no way to distinguish "no values supplied" from "your input was malformed".

Repro

>>> convert_string_to_list(convert_list_to_string([1, -2, 3]))
[1, 3]
>>> convert_string_to_list("a, 1, b, 2")
[1, 2]   # silently dropped 'a' and 'b'

Suggested fix

def convert_string_to_list(items_str: Optional[str]) -> List[int]:
    if not items_str or not items_str.strip():
        return []
    out = []
    for raw in items_str.split(","):
        item = raw.strip()
        if not item:
            continue
        try:
            out.append(int(item))
        except ValueError:
            logger.warning("convert_string_to_list: dropping non-integer entry %r", item)
    return out

That preserves negatives, surfaces malformed input via logs, and keeps the same return shape.

Category: A (logic/correctness). Severity: Low–Medium depending on where the round-trip is used in the DB layer.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions