playwright-python
playwright-python copied to clipboard
[Feature] rename `TimeoutError` or make it extend the builtin `TimeoutError`
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")
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.
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?
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
Probably want something like this:
class TimeoutError(Error, __builtins__.TimeoutError):
pass
Implemented in #2297
@mxschmitt why was this reopened and the commit reverted?