{"record":{"id":"932d79f90e197185","repo":"donnemartin/interactive-coding-challenges","slug":"num-pairs-cannot-be-none","errorCode":null,"errorMessage":"num_pairs cannot be None","messagePattern":"num_pairs cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"recursion_dynamic/n_pairs_parentheses/n_pairs_parentheses_solution.ipynb","lineNumber":108,"sourceCode":"  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Parentheses(object):\\n\",\n    \"\\n\",\n    \"    def find_pair(self, num_pairs):\\n\",\n    \"        if num_pairs is None:\\n\",\n    \"            raise TypeError('num_pairs cannot be None')\\n\",\n    \"        if num_pairs < 0:\\n\",\n    \"            raise ValueError('num_pairs cannot be < 0')\\n\",\n    \"        if not num_pairs:\\n\",\n    \"            return []\\n\",\n    \"        results = []\\n\",\n    \"        curr_results = []\\n\",\n    \"        self._find_pair(num_pairs, num_pairs, curr_results, results)\\n\",\n    \"        return results\\n\",\n    \"\\n\",\n    \"    def _find_pair(self, nleft, nright, curr_results, results):\\n\",\n    \"        if nleft == 0 and nright == 0:\\n\",\n    \"            results.append(''.join(curr_results))\\n\",\n    \"        else:\\n\",\n    \"            if nleft >= 0:\\n\",\n    \"                self._find_pair(nleft-1, nright, curr_results+['('], results)\\n\",\n    \"            if nright > nleft:\\n\",\n    \"                self._find_pair(nleft, nright-1, curr_results+[')'], results)\"\n   ]","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/recursion_dynamic/n_pairs_parentheses/n_pairs_parentheses_solution.ipynb#L90-L126","documentation":"Raised by Parentheses.find_pair when num_pairs is None. It is the first of three guards (None, negative, zero), because the recursive generator assumes an integer count; None is rejected with a TypeError before the num_pairs < 0 comparison would fail with a TypeError anyway.","triggerScenarios":"Calling find_pair(None). num_pairs = 0 returns [] and a negative value raises ValueError instead — the TypeError is specific to None.","commonSituations":"num_pairs coming from an unset request parameter or CLI option defaulting to None; a parsed value that failed int() conversion; optional argument never supplied.","solutions":["Pass an explicit integer, e.g. find_pair(3)","Convert and default: num_pairs = int(num_pairs or 0)","Validate request params before calling"],"exampleFix":"// before\npairs = p.find_pair(request.args.get('n'))\n// after\npairs = p.find_pair(int(request.args.get('n', 0)))","handlingStrategy":"validation","validationCode":"num_pairs = int(num_pairs) if num_pairs is not None else 0\np.find_pair(num_pairs)","typeGuard":"def is_nonneg_int(x):\n    return isinstance(x, int) and x >= 0","tryCatchPattern":"try:\n    p.find_pair(n)\nexcept TypeError:\n    n = 0\n    p.find_pair(n)","preventionTips":["Convert request params to int with defaults","Never pass optional params straight through as None"],"tags":["python","recursion","backtracking","parentheses","input-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}