{"record":{"id":"5ef8a4b532e4b7ea","repo":"TheAlgorithms/Python","slug":"plugboard-setting-isn-t-type-string-type-pbstrin","errorCode":null,"errorMessage":"Plugboard setting isn't type string ({type(pbstring)})","messagePattern":"Plugboard setting isn't type string \\((.+?)\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ciphers/enigma_machine2.py","lineNumber":136,"sourceCode":"    >>> _plugboard('PICTURES')\n    {'P': 'I', 'I': 'P', 'C': 'T', 'T': 'C', 'U': 'R', 'R': 'U', 'E': 'S', 'S': 'E'}\n    >>> _plugboard('POLAND')\n    {'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'}\n\n    In the code, ``pb`` stands for ``plugboard``\n\n    Pairs can be separated by spaces\n\n    :param pbstring: string containing plugboard setting for the Enigma machine\n    :return: dictionary containing converted pairs\n    \"\"\"\n\n    # tests the input string if it\n    # a) is type string\n    # b) has even length (so pairs can be made)\n    if not isinstance(pbstring, str):\n        msg = f\"Plugboard setting isn't type string ({type(pbstring)})\"\n        raise TypeError(msg)\n    elif len(pbstring) % 2 != 0:\n        msg = f\"Odd number of symbols ({len(pbstring)})\"\n        raise Exception(msg)\n    elif pbstring == \"\":\n        return {}\n\n    pbstring.replace(\" \", \"\")\n\n    # Checks if all characters are unique\n    tmppbl = set()\n    for i in pbstring:\n        if i not in abc:\n            msg = f\"'{i}' not in list of symbols\"\n            raise Exception(msg)\n        elif i in tmppbl:\n            msg = f\"Duplicate symbol ({i})\"\n            raise Exception(msg)\n        else:","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/enigma_machine2.py#L118-L154","documentation":"Raised by _plugboard() in ciphers/enigma_machine2.py when the plugboard setting is not a str. The plugboard is described as a string of letter pairs (e.g. 'PICTURES'), so passing a dict, list, bytes, or None triggers this TypeError.","triggerScenarios":"_plugboard({'P': 'O'}) with a dict; passing a list ['PI','CT','UR','ES']; passing None when no plugboard was configured; passing bytes.","commonSituations":"Naturally modelling the plugboard as a dict in calling code and passing it directly; optional settings defaulting to None; data arriving from JSON as a list of pairs.","solutions":["Pass a string of pairs: _plugboard('PICTURES') or the public API with pb='PICTURES'","Convert a dict of pairs back to a string: ''.join(k + v for k, v in pairs.items())","Use '' for 'no plugboard'"],"exampleFix":"# before\npb = {'P': 'O', 'L': 'A'}\n_plugboard(pb)\n\n# after\npb = 'POLA'\n_plugboard(pb)","handlingStrategy":"type-guard","validationCode":"if not isinstance(pb, str):\n    pb = ''.join(k + v for k, v in pb.items()) if isinstance(pb, dict) else str(pb)","typeGuard":"def is_plugboard_string(value) -> bool:\n    return isinstance(value, str)","tryCatchPattern":"try:\n    pbdict = _plugboard(pb)\nexcept TypeError as exc:\n    if \"isn't type string\" in str(exc):\n        pbdict = _plugboard('')  # no plugboard\n    else:\n        raise","preventionTips":["Pass plugboard settings as a plain string of pairs like 'POLA'","Use '' (not None) to mean 'no plugboard'","Convert dict/list representations to a string at your boundary"],"tags":["enigma","plugboard","type-error","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}