playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

[Feature] rename `TimeoutError` or make it extend the builtin `TimeoutError`

Open DetachHead opened this issue 3 years ago • 8 comments

TimeoutError shadows a builtin exception with the same name, which leads to confusion

try:
    page.click("foo")
except TimeoutError: # builtins.TimeoutError
    # this doesn't run because it raises a playwright.sync_api.TimeoutError
    print("failed to click element")

DetachHead avatar Aug 15 '22 06:08 DetachHead

This can be super sus if the import (from playwright.sync_api import TimeoutError) gets removed, your code will silently change behavior with no warning.

KotlinIsland avatar Aug 15 '22 06:08 KotlinIsland

You can rename it within your own project like: from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError.

While this doesn't address the shadowing and omission concern, Playwright cannot break its API by renaming this completely.

Perhaps doing:

from playwright.sync_api import sync_playwright, TimeoutError
class PlaywrightTimeoutError(TimeoutError):
   pass

would be a backwards compatible compromise. (The examples and your code could then be changed to look for PlaywrightTimeoutError.) Would this work for you? If so, we could discuss it further?

rwoll avatar Aug 15 '22 18:08 rwoll

perhaps simply making playwright.sync_api.TimeoutError extend both Error and the builtin TimeoutError would solve the problem?

_BuiltinTimeoutError = TimeoutError


class TimeoutError(Error, _BuiltinTimeoutError):
    ...

that way there's no backward incompatible change and code that catches builtins.TimeoutError will still work

DetachHead avatar Aug 15 '22 23:08 DetachHead

Probably want something like this:

class TimeoutError(Error, __builtins__.TimeoutError):
    pass

KotlinIsland avatar Aug 16 '22 23:08 KotlinIsland

Implemented in #2297

mxschmitt avatar Feb 14 '24 09:02 mxschmitt

@mxschmitt why was this reopened and the commit reverted?

danphenderson avatar Feb 21 '24 16:02 danphenderson