{"record":{"id":"a86f6b5bd1df9d64","repo":"donnemartin/interactive-coding-challenges","slug":"val-cannot-be-none-a86f6b","errorCode":null,"errorMessage":"val cannot be None","messagePattern":"val cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"online_judges/math_ops/math_ops_solution.ipynb","lineNumber":110,"sourceCode":"    \"\\n\",\n    \"\\n\",\n    \"class Solution(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, upper_limit=100):\\n\",\n    \"        self.max = None\\n\",\n    \"        self.min = None\\n\",\n    \"        # Mean\\n\",\n    \"        self.num_items = 0\\n\",\n    \"        self.running_sum = 0\\n\",\n    \"        self.mean = None\\n\",\n    \"        # Mode\\n\",\n    \"        self.array = [0] * (upper_limit+1)\\n\",\n    \"        self.mode_ocurrences = 0\\n\",\n    \"        self.mode = None\\n\",\n    \"\\n\",\n    \"    def insert(self, val):\\n\",\n    \"        if val is None:\\n\",\n    \"            raise TypeError('val cannot be None')\\n\",\n    \"        if self.max is None or val > self.max:\\n\",\n    \"            self.max = val\\n\",\n    \"        if self.min is None or val < self.min:\\n\",\n    \"            self.min = val\\n\",\n    \"        # Calculate the mean\\n\",\n    \"        self.num_items += 1\\n\",\n    \"        self.running_sum += val\\n\",\n    \"        self.mean = self.running_sum / self.num_items\\n\",\n    \"        # Calculate the mode\\n\",\n    \"        self.array[val] += 1\\n\",\n    \"        if self.array[val] > self.mode_ocurrences:\\n\",\n    \"            self.mode_ocurrences = self.array[val]\\n\",\n    \"            self.mode = val\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/math_ops/math_ops_solution.ipynb#L92-L128","documentation":"Raised by MathOps.insert in the online_judges variant when val is None (note this variant also has a typo'd attribute mode_ocurrences). The structure maintains running mean/min/max/mode counts that assume numeric input, so None is rejected before any accumulator is touched.","triggerScenarios":"Calling insert(None) — inserting values from a stream/dataset with missing entries (JSON null, CSV blank, failed parse).","commonSituations":"Streaming ingest of dirty data; batch tests; wrappers forwarding optional values unchanged.","solutions":["Filter or replace None values before inserting","Fix parsing to skip missing entries","Guard at the call site: if val is not None: ops.insert(val)"],"exampleFix":"# before\nfor val in stream:\n    ops.insert(val)  # stream may yield None\n\n# after\nfor val in stream:\n    if val is not None:\n        ops.insert(val)","handlingStrategy":"validation","validationCode":"for v in values:\n    if v is None: continue\n    ops.insert(v)","typeGuard":"def is_number(v): return isinstance(v, (int, float))","tryCatchPattern":"try:\n    ops.insert(v)\nexcept TypeError as e:\n    logger.warning('skipping invalid value: %s', e)","preventionTips":["Sanitize streams before ingest","Skip or impute missing values explicitly"],"tags":["python","input-validation","none-check","statistics"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}