Overwrite type hint of 3rd party library (for monkey patching)

119 views Asked by At

I am monkey-patching a class from a 3rd party library (playwright in my case) and also want to adjust the type hinting to include my adjustment. Is there a way somehow to overwrite or augment types from 3rd party libraries?

##
# Monkey-patching
##
from playwright.sync_api import Locator

def poll(self): # my custom function
  pass

# type error: poll is unknown
Locator.poll = poll

##
# Usage
##
def test_succeeds(page: Page):
  ...
  # type error: poll is unknown
  expect(page.locator('dl:has-text("Success")').poll()).to_be_visible()

0

There are 0 answers