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

Image for post
Image for post
Woman walking beside body of water toward snow covered mountain during daytime — Unsplash

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)

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

Image for post
Image for post
Should it click() or click_on() or click_in()?

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)

And you won’t need to guess what is the right word anymore:


Happy coding ~~~

Written by

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store