{"record":{"id":"2dea7d567506b4bd","repo":"SeleniumHQ/selenium","slug":"self-name-should-be-of-type-self-expected-type","errorCode":null,"errorMessage":"{self.name} should be of type {self.expected_type.__name__}","messagePattern":"(.+?) should be of type (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/common/print_page_options.py","lineNumber":133,"sourceCode":"    def __set__(self, obj, value) -> None:\n        if value not in self.ORIENTATION_VALUES:\n            raise ValueError(f\"Orientation value must be one of {self.ORIENTATION_VALUES}\")\n        obj._print_options[self.name] = value\n\n\nclass _ValidateTypeDescriptor:\n    \"\"\"Base Class Descriptor which validates type of any subclass attribute.\"\"\"\n\n    def __init__(self, name, expected_type: type):\n        self.name = name\n        self.expected_type = expected_type\n\n    def __get__(self, obj, cls):\n        return obj._print_options.get(self.name, None)\n\n    def __set__(self, obj, value) -> None:\n        if not isinstance(value, self.expected_type):\n            raise ValueError(f\"{self.name} should be of type {self.expected_type.__name__}\")\n        obj._print_options[self.name] = value\n\n\nclass _ValidateBackGround(_ValidateTypeDescriptor):\n    \"\"\"Expected type of background attribute.\"\"\"\n\n    def __init__(self, name):\n        super().__init__(name, bool)\n\n\nclass _ValidateShrinkToFit(_ValidateTypeDescriptor):\n    \"\"\"Expected type of shrink to fit attribute.\"\"\"\n\n    def __init__(self, name):\n        super().__init__(name, bool)\n\n\nclass _ValidatePageRanges(_ValidateTypeDescriptor):","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/common/print_page_options.py#L115-L151","documentation":"The _ValidateTypeDescriptor validates that the assigned value is an instance of the descriptor's expected_type. For PrintOptions, background and shrink_to_fit require bool, and page_ranges requires list. A wrong type raises ValueError naming the property and the expected type.","triggerScenarios":"Setting print_options.background = 'true' (string instead of bool), print_options.background = 1 (int), print_options.shrink_to_fit = 'yes', or print_options.page_ranges = '1-3' (string instead of list).","commonSituations":"Loading config from JSON/env where booleans arrive as strings. Passing a single page-range string instead of a list. Treating Python truthiness as a bool substitute.","solutions":["Pass a real bool for background/shrink_to_fit and a list for page_ranges.","Convert string config values: background = (val == 'true') or bool(val).","Wrap single ranges in a list: page_ranges = ['1-3']."],"exampleFix":"# before\nprint_options.background = 'true'      # str, not bool -> ValueError\nprint_options.page_ranges = '1-3'      # str, not list -> ValueError\n\n# after\nprint_options.background = True\nprint_options.page_ranges = ['1-3']","handlingStrategy":"type-guard","validationCode":"if 'background' in cfg:\n    assert isinstance(cfg['background'], bool), 'background must be bool'\nif 'pageRanges' in cfg:\n    assert isinstance(cfg['pageRanges'], list), 'pageRanges must be list'","typeGuard":"def is_bool(v) -> bool:\n    return isinstance(v, bool)\ndef is_str_list(v) -> bool:\n    return isinstance(v, list) and all(isinstance(x, str) for x in v)","tryCatchPattern":"try:\n    print_options.background = bg\nexcept ValueError:\n    print_options.background = bool(bg) if not isinstance(bg, bool) else bg","preventionTips":["Convert string 'true'/'false' to real bools at the config boundary.","Wrap single page-range strings in a list."],"tags":["print","type-validation","background","page-ranges"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}