Skip to content

[Refactor]: Improve type safety for browser preferences #289

@thalissonvs

Description

@thalissonvs

Currently, browser_preferences in ChromiumOptions is a generic dict without type safety, making it error-prone and difficult to maintain.

Problems:

No type validation: _browser_preferences: dict accepts any structure
Unsafe path access: _set_pref_path(path: list, value) - no type checking on paths or values
Silent failures: _get_pref_path() returns None without proper Optional typing
Generic exception: WrongPrefsDict doesn't indicate what's wrong
No autocomplete: IDE can't suggest available preference keys

Proposed Solution:

  1. Create TypedDict for preferences structure:
from typing import TypedDict, NotRequired

class DownloadPreferences(TypedDict, total=False):
    default_directory: str
    prompt_for_download: bool

class ProfilePreferences(TypedDict, total=False):
    password_manager_enabled: bool
    default_content_setting_values: NotRequired[dict[str, int]]

class BrowserPreferences(TypedDict, total=False):
    download: DownloadPreferences
    profile: ProfilePreferences
    intl: NotRequired[dict[str, str]]
    plugins: NotRequired[dict[str, bool]]
    credentials_enable_service: bool
  1. Update method signatures
  2. Add specific exceptions
  3. Add preference path validation

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions