SeleniumHQ/selenium · error · TypeError

{self.name} must be of type {self.expected_type}

Error message

{self.name} must be of type {self.expected_type}

What it means

Safari Options uses OptionDescriptor instances for safari-specific capabilities. __set__ enforces that the assigned value matches expected_type; a mismatch raises TypeError naming the capability and the expected type.

Source

Thrown at py/selenium/webdriver/safari/options.py:54

    Example:
        `self.automatic_inspection` = True
        (`__set__` method sets/updates the value of the key `safari:automaticInspection` in `_caps`
            dictionary in `Options` class)
    """

    def __init__(self, name, expected_type):
        self.name = name
        self.expected_type = expected_type

    def __get__(self, obj, cls):
        if self.name == "Safari Technology Preview":
            return obj._caps.get("browserName") == self.name
        return obj._caps.get(self.name)

    def __set__(self, obj, value):
        if not isinstance(value, self.expected_type):
            raise TypeError(f"{self.name} must be of type {self.expected_type}")
        if self.name == "Safari Technology Preview":
            obj._caps["browserName"] = self.name if value else "safari"
        else:
            obj._caps[self.name] = value


class Options(ArgOptions):
    # @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
    AUTOMATIC_INSPECTION = "safari:automaticInspection"
    AUTOMATIC_PROFILING = "safari:automaticProfiling"
    SAFARI_TECH_PREVIEW = "Safari Technology Preview"

    # creating descriptor objects
    automatic_inspection = _SafariOptionsDescriptor(AUTOMATIC_INSPECTION, bool)
    """Whether to enable automatic inspection."""

    automatic_profiling = _SafariOptionsDescriptor(AUTOMATIC_PROFILING, bool)
    """Whether to enable automatic profiling."""

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Coerce the value to the descriptor's expected_type before assignment.
  2. Inspect the Options class constants (AUTOMATIC_INSPECTION, AUTOMATIC_PROFILING, etc.) to learn which options exist and their types.
  3. Load config with proper typing so json booleans stay bool, not str.

Example fix

# before
opts = Options()
opts["safari:automaticInspection"] = "true"   # str, not bool

# after
opts = Options()
opts["safari:automaticInspection"] = True
Defensive patterns

Strategy: type-guard

Type guard

def coerce_bool(v):
    if isinstance(v, bool):
        return v
    if isinstance(v, str) and v.lower() in ("true", "false"):
        return v.lower() == "true"
    raise TypeError(f"expected bool, got {type(v)}")

Prevention

When it happens

Trigger: Setting a safari option (e.g. safari:automaticInspection / safari:automaticProfiling which expect bool) with the wrong Python type - a string, None, or int where bool is required.

Common situations: Loading capabilities from JSON/YAML where booleans arrive as strings, or assuming these accept truthy values.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/67e621e3e14b43b6. Report an issue: GitHub.