{"record":{"id":"6b21b71e63d433da","repo":"TheAlgorithms/Python","slug":"invalid-k-value","errorCode":null,"errorMessage":"invalid k value","messagePattern":"invalid k value","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"computer_vision/harris_corner.py","lineNumber":21,"sourceCode":"\n\"\"\"\nHarris Corner Detector\nhttps://en.wikipedia.org/wiki/Harris_Corner_Detector\n\"\"\"\n\n\nclass HarrisCorner:\n    def __init__(self, k: float, window_size: int):\n        \"\"\"\n        k : is an empirically determined constant in [0.04,0.06]\n        window_size : neighbourhoods considered\n        \"\"\"\n\n        if k in (0.04, 0.06):\n            self.k = k\n            self.window_size = window_size\n        else:\n            raise ValueError(\"invalid k value\")\n\n    def __str__(self) -> str:\n        return str(self.k)\n\n    def detect(self, img_path: str) -> tuple[cv2.Mat, list[list[int]]]:\n        \"\"\"\n        Returns the image with corners identified\n        img_path  : path of the image\n        output : list of the corner positions, image\n        \"\"\"\n\n        img = cv2.imread(img_path, 0)\n        h, w = img.shape\n        corner_list: list[list[int]] = []\n        color_img = img.copy()\n        color_img = cv2.cvtColor(color_img, cv2.COLOR_GRAY2RGB)\n        dy, dx = np.gradient(img)\n        ixx = dx**2","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/computer_vision/harris_corner.py#L3-L39","documentation":"HarrisCorner.__init__ raises this ValueError when k is not one of exactly two floats: 0.04 or 0.06. The docstring says k is 'an empirically determined constant in [0.04,0.06]' (a range), but the implementation tests membership with `k in (0.04, 0.06)`, so any other value in the documented range — most notably 0.05 — is rejected. This is effectively a bug in the guard, not in the caller.","triggerScenarios":"HarrisCorner(0.05, 3); HarrisCorner(0.041, 5); HarrisCorner(0, 3); any k arrived at by tuning or random search inside [0.04, 0.06] other than the two endpoints.","commonSituations":"Following tutorials/textbooks that recommend k=0.05; hyperparameter sweeps; reading the docstring's [0.04,0.06] range and picking a midpoint. Only the literal values 0.04 and 0.06 pass.","solutions":["Pass exactly 0.04 or 0.06: HarrisCorner(0.04, window_size)","Or fix the guard to match the documented range: `if 0.04 <= k <= 0.06:` (this is the correct upstream fix)","If you must keep the strict check, snap your tuned k to the nearest allowed value before constructing"],"exampleFix":"# before\ncorner = HarrisCorner(0.05, 3)\n# ValueError: invalid k value\n\n# after (use an accepted literal)\ncorner = HarrisCorner(0.04, 3)\n\n# or fix the library guard\nif 0.04 <= k <= 0.06:","handlingStrategy":"validation","validationCode":"K_VALUES = (0.04, 0.06)\nk = 0.04 if k not in K_VALUES else k\nhc = HarrisCorner(k, window_size)","typeGuard":"def is_valid_k(k) -> bool:\n    return isinstance(k, float) and k in (0.04, 0.06)","tryCatchPattern":"try:\n    HarrisCorner(k, window_size)\nexcept ValueError as e:\n    if 'invalid k value' in str(e):\n        HarrisCorner(0.04, window_size)  # fall back to accepted literal\n    else:\n        raise","preventionTips":["Treat k as an enum of two values, not a continuous range, despite the docstring","Pin k=0.04 in configs so tuning tools never inject 0.05","Patch the guard to 0.04 <= k <= 0.06 if you fork the repo"],"tags":["computer-vision","harris-corner","validation","parameter-bug"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}