{"record":{"id":"25808e12ec003827","repo":"TheAlgorithms/Python","slug":"input-must-be-a-string","errorCode":null,"errorMessage":"Input must be a string","messagePattern":"Input must be a string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"strings/count_vowels.py","lineNumber":25,"sourceCode":"\n    Examples:\n    >>> count_vowels(\"hello world\")\n    3\n    >>> count_vowels(\"HELLO WORLD\")\n    3\n    >>> count_vowels(\"123 hello world\")\n    3\n    >>> count_vowels(\"\")\n    0\n    >>> count_vowels(\"a quick brown fox\")\n    5\n    >>> count_vowels(\"the quick BROWN fox\")\n    5\n    >>> count_vowels(\"PYTHON\")\n    1\n    \"\"\"\n    if not isinstance(s, str):\n        raise TypeError(\"Input must be a string\")\n\n    vowels = \"aeiouAEIOU\"\n    return sum(1 for char in s if char in vowels)\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":7,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/strings/count_vowels.py#L7-L35","documentation":"Raised by count_vowels in strings/count_vowels.py when s is not a str. The function counts characters from the fixed vowel set 'aeiouAEIOU' by iterating the string, so any non-string (int, list, None) is rejected up front with TypeError. Unlike several neighbors in this repo, this one correctly uses TypeError for a type mismatch.","triggerScenarios":"count_vowels(123); count_vowels(['a', 'e']); count_vowels(None) when an optional text field was never provided.","commonSituations":"Optional fields defaulting to None; JSON numbers where text was expected; passing bytes instead of str (bytes is also rejected).","solutions":["Convert at the boundary: count_vowels(str(text)).","Handle None explicitly: count_vowels(text or '').","Decode bytes first: count_vowels(blob.decode('utf-8')) if handling raw byte input."],"exampleFix":"# before\ncount_vowels(payload.get('comment'))  # comment missing -> None\n\n# after\ncount_vowels(payload.get('comment') or '')","handlingStrategy":"type-guard","validationCode":"if not isinstance(s, str):\n    raise TypeError(f'expected str, got {type(s).__name__}')\ncount = count_vowels(s)","typeGuard":"def is_str(value) -> bool:\n    return isinstance(value, str)","tryCatchPattern":"try:\n    n = count_vowels(text)\nexcept TypeError:\n    n = 0 if text is None else count_vowels(str(text))","preventionTips":["Handle optional text fields with (text or '') before passing to string helpers.","Decode bytes to str at the I/O boundary.","count_vowels correctly raises TypeError, so a plain `except TypeError` is reliable here."],"tags":["strings","vowels","type-error","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}