Canute S
Canute S

Reputation: 394

Shorter type hints for long types

(Excuse the bad title, maybe someone can suggest a better one)

Just getting into typehinting/typechecking and wanted to add on for a function to which I'm passing a selenium webdriver object which shows:

In:  type(driver)
Out: selenium.webdriver.chrome.webdriver.WebDriver

Is there a way it can be made shorter without typing the whole thing? (no pun intended)

I found about about aliases and wanted to know if it is safe to use something like:

WebDriver = selenium.webdriver.chrome.webdriver.WebDriver
WebElement = selenium.webdriver.remote.webelement.WebElement

# Function definition follows:
def get_object(driver: WebDriver, ....) -> WebElement:
    ...

Two questions remain:

Upvotes: 1

Views: 751

Answers (1)

Wes Hardaker
Wes Hardaker

Reputation: 22262

The pythononic-way to do this is changing the imports line:

from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement

# Function definition follows:
def get_object(driver: WebDriver, ....) -> WebElement:

Upvotes: 4

Related Questions