{"record":{"id":"30004b8bae5f2681","repo":"TheAlgorithms/Python","slug":"hand-should-be-of-type-str-hand-r","errorCode":null,"errorMessage":"Hand should be of type 'str': {hand!r}","messagePattern":"Hand should be of type 'str': (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_054/sol1.py","lineNumber":124,"sourceCode":"        \"\"\"\n        Initialize hand.\n        Hand should of type str and should contain only five cards each\n        separated by a space.\n\n        The cards should be of the following format:\n        [card value][card suit]\n\n        The first character is the value of the card:\n        2, 3, 4, 5, 6, 7, 8, 9, T(en), J(ack), Q(ueen), K(ing), A(ce)\n\n        The second character represents the suit:\n        S(pades), H(earts), D(iamonds), C(lubs)\n\n        For example: \"6S 4C KC AS TH\"\n        \"\"\"\n        if not isinstance(hand, str):\n            msg = f\"Hand should be of type 'str': {hand!r}\"\n            raise TypeError(msg)\n        # split removes duplicate whitespaces so no need of strip\n        if len(hand.split(\" \")) != 5:\n            msg = f\"Hand should contain only 5 cards: {hand!r}\"\n            raise ValueError(msg)\n        self._hand = hand\n        self._first_pair = 0\n        self._second_pair = 0\n        self._card_values, self._card_suit = self._internal_state()\n        self._hand_type = self._get_hand_type()\n        self._high_card = self._card_values[0]\n\n    @property\n    def hand(self):\n        \"\"\"Returns the self hand\"\"\"\n        return self._hand\n\n    def compare_with(self, other: PokerHand) -> str:\n        \"\"\"","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_054/sol1.py#L106-L142","documentation":"Raised by the PokerHand constructor in project_euler/problem_054/sol1.py when the hand argument is not a str. Hands are expected as a single string of five space-separated cards such as '6S 4C KC AS TH'; passing a list of cards, a tuple, or None triggers this TypeError before any parsing happens.","triggerScenarios":"PokerHand(['6S','4C','KC','AS','TH']) (list instead of string), PokerHand(None), PokerHand(b'6S 4C') (bytes), or PokerHand(123). Any non-str type, even if it contains valid card text.","commonSituations":"Reading hands from CSV/pandas where a row splits into a list of fields; passing bytes from file reads without decode; refactoring from list-based to string-based API and missing a call site.","solutions":["Pass one space-separated string: PokerHand('6S 4C KC AS TH').","Join list/tuple inputs: PokerHand(' '.join(cards)).","Decode bytes before constructing: PokerHand(raw.decode()).","Add a type guard or assert at the boundary where hands enter your pipeline."],"exampleFix":"# before\nhand = ['6S', '4C', 'KC', 'AS', 'TH']\np = PokerHand(hand)  # TypeError\n\n# after\np = PokerHand(' '.join(hand))","handlingStrategy":"type-guard","validationCode":"if isinstance(cards, (list, tuple)):\n    hand = ' '.join(cards)\nelif isinstance(hand_input, bytes):\n    hand = hand_input.decode()\nelse:\n    hand = hand_input\nassert isinstance(hand, str)\nPokerHand(hand)","typeGuard":"def is_hand_string(value) -> bool:\n    return isinstance(value, str)","tryCatchPattern":"try:\n    poker_hand = PokerHand(raw)\nexcept TypeError:\n    poker_hand = PokerHand(' '.join(raw))  # retry with normalized input\nexcept ValueError:\n    logger.error(\"malformed hand: %r\", raw)\n    raise","preventionTips":["Keep hands as one space-separated string end-to-end in your pipeline.","Join list-of-cards and decode bytes at the data-ingestion boundary.","Add a type check in your loader so bad rows are logged with line numbers."],"tags":["project-euler","poker","typeerror","type-check"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}