Repository: apify/crawlee-python
File: src/crawlee/playwright_crawler/_playwright_crawler.py (Python)

Code before:
    def __init__(
        self,
        browser_pool: BrowserPool | None = None,
        browser_type: BrowserType | None = None,
        browser_options: Mapping[str, Any] | None = None,
        page_options: Mapping[str, Any] | None = None,
        headless: bool | None = None,
        **kwargs: Unpack[BasicCrawlerOptions[PlaywrightCrawlingContext]],
    ) -> None:
        """A default constructor.

        Args:
            browser_pool: A `BrowserPool` instance to be used for launching the browsers and getting pages.
            browser_type: The type of browser to launch ('chromium', 'firefox', or 'webkit').
                This option should not be used if `browser_pool` is provided.
            browser_options: Keyword arguments to pass to the browser launch method.
                This option should not be used if `browser_pool` is provided.
            page_options: Keyword arguments to pass to the new page method.
                This option should not be used if `browser_pool` is provided.
            headless: Whether to run the browser in headless mode.
                This option should not be used if `browser_pool` is provided.
            kwargs: Additional keyword arguments to pass to the underlying `BasicCrawler`.
        """
        if browser_pool:
            # Raise an exception if browser_pool is provided together with headless or browser_type arguments.
            if (
                headless is not None
                or browser_type is not None
                or browser_options is not None
                or page_options is not None
            ):
                raise ValueError(
                    'You cannot provide `headless`, `browser_type`, `browser_options` or `page_options` '
                    'arguments when `browser_pool` is provided.'
                )

        # If browser_pool is not provided, create a new instance of BrowserPool with specified arguments.
        else:
            browser_pool = BrowserPool.with_default_plugin(
                headless=headless,
                browser_type=browser_type,
                browser_options=browser_options,
                page_options=page_options,
            )

        self._browser_pool = browser_pool

        # Compose the context pipeline with the Playwright-specific context enhancer.
        kwargs['_context_pipeline'] = (
            ContextPipeline().compose(self._open_page).compose(self._navigate).compose(self._handle_blocked_request)
        )

Review comment (suggestion):
With many conditions like this you could consider using **if any**(headless, browser_type, browser_options, page_options)

Code after:
    def __init__(
        self,
        browser_pool: BrowserPool | None = None,
        browser_type: BrowserType | None = None,
        browser_options: Mapping[str, Any] | None = None,
        page_options: Mapping[str, Any] | None = None,
        headless: bool | None = None,
        **kwargs: Unpack[BasicCrawlerOptions[PlaywrightCrawlingContext]],
    ) -> None:
        """A default constructor.

        Args:
            browser_pool: A `BrowserPool` instance to be used for launching the browsers and getting pages.
            browser_type: The type of browser to launch ('chromium', 'firefox', or 'webkit').
                This option should not be used if `browser_pool` is provided.
            browser_options: Keyword arguments to pass to the browser launch method.
                This option should not be used if `browser_pool` is provided.
            page_options: Keyword arguments to pass to the new page method.
                This option should not be used if `browser_pool` is provided.
            headless: Whether to run the browser in headless mode.
                This option should not be used if `browser_pool` is provided.
            kwargs: Additional keyword arguments to pass to the underlying `BasicCrawler`.
        """
        if browser_pool:
            # Raise an exception if browser_pool is provided together with other browser-related arguments.
            if any(param is not None for param in (headless, browser_type, browser_options, page_options)):
                raise ValueError(
                    'You cannot provide `headless`, `browser_type`, `browser_options` or `page_options` '
                    'arguments when `browser_pool` is provided.'
                )

        # If browser_pool is not provided, create a new instance of BrowserPool with specified arguments.
        else:
            browser_pool = BrowserPool.with_default_plugin(
                headless=headless,
                browser_type=browser_type,
                browser_options=browser_options,
                page_options=page_options,
            )

        self._browser_pool = browser_pool

        # Compose the context pipeline with the Playwright-specific context enhancer.
        kwargs['_context_pipeline'] = (
            ContextPipeline().compose(self._open_page).compose(self._navigate).compose(self._handle_blocked_request)
        )
        kwargs['_additional_context_managers'] = [self._browser_pool]
        kwargs.setdefault('_logger', logging.getLogger(__name__))
        self._pre_navigation_hooks: list[Callable[[PlaywrightPreNavigationContext], Awaitable[None]]] = []

        super().__init__(**kwargs)