DEV Community

Cover image for Get rid of guessing the function name supported with type hints in Python 3.6 +
cuongld2
cuongld2

Posted on

Get rid of guessing the function name supported with type hints in Python 3.6 +

Alt Text

In the past, I used to work with strong-typed languages such as Java so I get a lot of suggestions when working with object / classes.

But when I moved to Python (our team use Python for automation testing), I completely feel lost when I need to know which method does the type/class support.

Recently, Iโ€™ve got more time to dig in Python, I got to know Python optional type hints.

So what is optional type hint?

1.Keep in mind that it is optional:

If the variable is not declared with type, thatโ€™s totally fine.

2.How to do the type hint:

For example, you create a method that tell Selenium to click on a button.

If you do it without type hint, it will go like this:

def do_not_seed_action(self, driver):
    do_not_seed_btn = self.downloads_elem.find_do_not_seed_button(driver)
    do_not_seed_btn.click()

When you write the code, you might not know what can this instance of class/object can do

Should it click() or click_on() or click_in()?
Alt Text

Instead, you can write code with type hint for getting more support from IDE:

from selenium.webdriver.remote.webelement import WebElement
do_not_seed_btn: WebElement = self.downloads_elem.find_do_not_seed_button(driver)
do_not_seed_btn.click()

And you wonโ€™t need to guess what is the right word anymore:
Alt Text

Tadaaa~~~

Happy coding ~~~

Top comments (0)