Question:
Steps to setup typing annotations on my script: Python

I want to make Playwright's browser and context the return of the following code, but I can not get it to work:

import asyncio

from playwright.async_api import async_playwright


async def run_browser() : 

    p = await async_playwright().start()

    browser = await p.chromium.launch(headless=False)

    context = await browser.new_context(java_script_enabled=True,locale='pt-br')

    return context, p


What would I have to set on the -> type?


Solution:

This code for Playwright 1.40.0 works with Pyright 1.1.343 on Python 3.10.12:


import asyncio

from playwright.async_api import async_playwright, BrowserContext, Playwright



async def run_browser() -> tuple[BrowserContext, Playwright]:

    p = await async_playwright().start()

    browser = await p.chromium.launch(headless=False)

    context = await browser.new_context(java_script_enabled=True, locale="pt-br")

    return context, p


See also >Automatically generating Python type annotations?.



Suggested blogs:

>How to Build a Python GUI Application With WX Python

>How to Build a RESTful API Using Node Express and MongoDB- Part 1

>How to build an Electron application from scratch?

>How to build interactive_graphviz in Python

>How to change the project in GCP using CLI command

>How to create a line animation in Python?

>Design a basic Mobile App With the Kivy Python Framework

>How React hooks Completely replace redux?

>How to access the state using Redux RTK in the React Native app?

>How to create a One Page App with Angular.js, Node.js and MongoDB?

>How to Create an array based on two other arrays in Php

>How To Create Nested Table Structure In Angular?


Nisha Patel

Nisha Patel

Submit
0 Answers