Giter Site home page Giter Site logo

Comments (5)

danphenderson avatar danphenderson commented on May 25, 2024 1

I am digging into this issue some more to try and figure out if there is a difference between None and False.

It appears that chromium_sandbox will be passed to the launch and lanuch_persistent_context methods of BrowserType as the chromiumSandbox parameter, which is declared as a bool but defaults to None.

The more I look, I see None being declared as the default when the parameter/attribute isn't declared as optional, a different but related issue to the one at hand (e.g. all the methods in BrowserType).

@jamesbraza your report appears to just be the tip of the iceberg.

from playwright-python.

danphenderson avatar danphenderson commented on May 25, 2024 1

Great, I agree!

Regarding the other issue at hand, observe the Browser method:

class Browser(ChannelOwner):
    ...
    async def new_context(
        self,
        viewport: ViewportSize = None,
        screen: ViewportSize = None,
        noViewport: bool = None,
        ignoreHTTPSErrors: bool = None,
        javaScriptEnabled: bool = None,
        bypassCSP: bool = None,
        userAgent: str = None,
        locale: str = None,
        timezoneId: str = None,
        geolocation: Geolocation = None,
        permissions: Sequence[str] = None,
        extraHTTPHeaders: Dict[str, str] = None,
        offline: bool = None,
        httpCredentials: HttpCredentials = None,
        deviceScaleFactor: float = None,
        isMobile: bool = None,
        hasTouch: bool = None,
        colorScheme: ColorScheme = None,
        reducedMotion: ReducedMotion = None,
        forcedColors: ForcedColors = None,
        acceptDownloads: bool = None,
        defaultBrowserType: str = None,
        proxy: ProxySettings = None,
        recordHarPath: Union[Path, str] = None,
        recordHarOmitContent: bool = None,
        recordVideoDir: Union[Path, str] = None,
        recordVideoSize: ViewportSize = None,
        storageState: Union[StorageState, str, Path] = None,
        baseURL: str = None,
        strictSelectors: bool = None,
        serviceWorkers: ServiceWorkersPolicy = None,
        recordHarUrlFilter: Union[Pattern[str], str] = None,
        recordHarMode: HarMode = None,
        recordHarContent: HarContentPolicy = None,
    ) -> BrowserContext:
    ...

The type-hints for the new_context parameters are incorrect, namely, None is being used as the default despite the parameter not being declared as optional. This occurs in a lot of other areas in the code base. I realize Python typing is very optional, but I figure that if we use type hints we might as well use them properly.

An update that is compliant with Python 3.9- may look like this:

class Browser(ChannelOwner):
    ...
    async def new_context(
        self,
        viewport: Optional[ViewportSize] = None,
        screen: Optional[ViewportSize] = None,
        noViewport: Optional[bool] = None,
        ignoreHTTPSErrors: Optional[bool] = None,
        javaScriptEnabled: Optional[bool] = None,
        bypassCSP: Optional[bool] = None,
        userAgent: Optional[str] = None,
        locale: Optional[str] = None,
        timezoneId: Optional[str] = None,
        geolocation: Optional[Geolocation] = None,
        permissions: Optional[Sequence[str]] = None,
        extraHTTPHeaders: Optional[Dict[str, str]] = None,
        offline: Optional[bool] = None,
        httpCredentials: Optional[HttpCredentials] = None,
        deviceScaleFactor: Optional[float] = None,
        isMobile: Optional[bool] = None,
        hasTouch: Optional[bool] = None,
        colorScheme: Optional[ColorScheme] = None,
        reducedMotion: Optional[ReducedMotion] = None,
        forcedColors: Optional[ForcedColors] = None,
        acceptDownloads: Optional[bool] = None,
        defaultBrowserType: Optional[str] = None,
        proxy: Optional[ProxySettings] = None,
        recordHarPath: Union[Path, str, None] = None,
        recordHarOmitContent: Optional[bool] = None,
        recordVideoDir: Union[Path, str, None] = None,
        recordVideoSize: Optional[ViewportSize] = None,
        storageState: Union[StorageState, str, Path, None] = None,
        baseURL: Optional[str] = None,
        strictSelectors: Optional[bool] = None,
        serviceWorkers: Optional[ServiceWorkersPolicy] = None,
        recordHarUrlFilter: Union[Pattern[str], str, None] = None,
        recordHarMode: Optional[HarMode] = None,
        recordHarContent: Optional[HarContentPolicy] = None,
    ) -> BrowserContext:
    ...

from playwright-python.

mxschmitt avatar mxschmitt commented on May 25, 2024

All parameters of any Playwright API have None as a default. This is more an implementation detail which we internally use to decide if a user has passed a parameter over or not. Internally in our code we check if it was provided or not (!== null/undefined and then use it / use our default).

None means for all our methods that the default will be used / treated like nothing was specified.

We can maybe get rid of all the = None everywhere.

from playwright-python.

danphenderson avatar danphenderson commented on May 25, 2024

Using None that way is fine, but the parameter type hints should be either Union[SomeType, None], Optional[SomeType], or SomeType | None (preferred, but may cause some issues in Python 3.9)

from playwright-python.

jamesbraza avatar jamesbraza commented on May 25, 2024

I am also okay with defaulting to None, I just request the docstring state:

  1. The correct default of None
  2. That None is equivalent to False, within the implementation

So it's changing from:

Enable Chromium sandboxing. Defaults to false.

To:

Enable Chromium sandboxing. Defaults to None, which is equivalent to False.

from playwright-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.