[PYTHON] Increase source visibility with type hints

What is a type hint?

With type hints, you can define types for arguments and return values in your program and code them as if they were a statically typed language. Type hints are treated like meta information and do not affect the program being executed.

Basically, you don't write types in python. That's because python is a dynamically typed language. Dynamic typing means that python determines the type and selects the appropriate type when the program is executed. ⇔ Static typing: Determine the type at compile time. The target language is C or Java.

Purpose of using type hints

Improved visibility of source code

Although python also has the concept of types, you can write programs without being aware of types. However, if you write a program without being aware of the type, it is difficult to notice that the data structure is leaked or the processing content is incorrect. It is difficult to identify the cause even if an unexpected error occurs during execution. Even in reviews, it is difficult to convey the essence and purpose of the created program to a third party.

Using type hints makes the arguments of classes and functions clearer. It makes it easier to convey the intent of the program. The more source files there are, the more effective it is.

Improvement of development efficiency

When examining the program with help (), the given type hint can be displayed.

nohint.py



#There is a return value
def getword(name, age):
    return f"I{name}is.{age}歳is."

#No return value
def outputword(name, age):
    print(f"I{name}is.{age}歳is.")

if __name__ == "__main__":
    print(getword("Test Taro", 30))
    outputword("Test Hanako", 25)

$ python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import nohint
>>> help(nohint)
Help on module nohint:

NAME
    nohint - #There is a return value

FUNCTIONS
    getword(name, age)
        #There is a return value

    outputword(name, age)
        #No return value

FILE
    c:\users\xxx\documents\nohint.py

hintbase.py


from typing import NoReturn

#There is a return value
def getword(name : str, age : int) -> str:
    return f"I{name}is.{age}歳is."

#No return value
def outputword1(name : str, age : int):
    print(f"I{name}is.{age}歳is.")

#No return value
def outputword2(name : str, age : int) -> None:
    print(f"I{name}is.{age}歳is.")

#No return value
def outputword3(name : str, age : int) -> NoReturn:
    print(f"I{name}is.{age}歳is.")

if __name__ == "__main__":
    print(getword("Test Taro", 30))
    outputword1("Test Hanako", 23)
    outputword2("Test Manako", 24)
    outputword3("Test Yamako", 25)

$ python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hintbase
>>> help(hintbase)
Help on module hintbase:

NAME
    hintbase

FUNCTIONS
    getword(name: str, age: int) -> str
        #There is a return value

    outputword1(name: str, age: int)
        #No return value

    outputword2(name: str, age: int) -> None
        #No return value

    outputword3(name: str, age: int) -> NoReturn
        #No return value

DATA
    NoReturn = typing.NoReturn

FILE
    c:\users\xxx\documents\hintbase.py

To use type hints

Since typing is a module added in python3.5, it can be used from the beginning. typing official

How to write type hints

syntax

def function name(Argument name:Mold, Argument name:Mold, ...) ->Return type:

Write the type name with a colon after the argument name. If there are multiple arguments, separate them with commas. If there is no return value, use None or NoReturn. The -> return type can be omitted. If you want to use NoReturn, you need to import it from typing.

hint-base.py


from typing import NoReturn

#There is a return value
def getword(name : str, age : int) -> str:
    return f"I{name}is.{age}歳is."

#No return value
def outputword1(name : str, age : int):
    print(f"I{name}is.{age}歳is.")

#No return value
def outputword2(name : str, age : int) -> None:
    print(f"I{name}is.{age}歳is.")

#No return value
def outputword3(name : str, age : int) -> NoReturn:
    print(f"I{name}is.{age}歳is.")

if __name__ == "__main__":
    print(getword("Test Taro", 30))
    outputword1("Test Hanako", 23)
    outputword2("Test Manako", 24)
    outputword3("Test Yamako", 25)

$ python hint-base.py
I'm Test Taro. 30 years old.
I'm a test Hanako. I am 23 years old.
I'm a test manako. I am 24 years old.
I'm Test Yamako. I'm 25 years old.

For self-made class

In the method in the self-made class, the first argument is self, but self does not need a type hint. The same applies to class methods, and no type hint is required for the first argument cls.

hint-class.py


from typing import Type, NoReturn

class Words:

    #constructor
    def __init__(self, name : str, age : int):
        self.name = name
        self.age = age

    #There is a return value
    def getword(self) -> str:
        return f"I{self.name}is.{self.age}歳is."

    #No return value
    def setword(self, name : str, age : int) -> NoReturn:
        self.name = name
        self.age = age

if __name__ == "__main__":
    words = Words("Test Taro", 30)
    print(words.getword())
    words.setword("Test Hanako", 23)
    print(words.getword())

$ python hint-class.py
I'm Test Taro. 30 years old.
I'm a test Hanako. I am 23 years old.

Recommended Posts

Increase source visibility with type hints
Try type hints
I also wanted to check type hints with numpy
[Translation] PEP 0484 --Type Hints
Format C source with pycparser