<jupyter_start><jupyter_text>mypy IntroductionThis notebook shows the main components of the mypy package.  For more information, visit mypy's official home page (http://mypy-lang.org/) and the mypy documentation (https://mypy.readthedocs.io/en/stable/).mypy introduces static typing to Python. I.e., you can define types for variables and function outputs, as if Python does not use dynamic typing as default. Usually, mypy is bundled with common Python distribution, such as Anaconda (https://www.continuum.io/anaconda). If it is not bundled with your distribution and you use pip, you can install mypy with the command "pip install mypy".After you installed mypy, you can load it into your script in the following way:<jupyter_code>from typing import *<jupyter_output><empty_output><jupyter_text>1. Usage with variables<jupyter_code># Integers
a:int = 1
a
# Floats
b:float = 1.0
b
# Booleans
c:bool = True
c
# Strings
d:str = "A"
d
# Tuples
e:Tuple[str, int] = ("A", 1)
e
# Lists
f:List[int] = [1,2,3,4]
f
# List of lists
g:List[List[int]] = [[1,2,3]]
g
# Dictionaries
h:Dict[str, int] = {}
h["A"] = 2
h<jupyter_output><empty_output><jupyter_text>2. Usage with definitions<jupyter_code># Method without return
def method(argument: int) -> None:
    print("A"*argument)
method(4)
# Function which an integer and returns a string
def function(argument: int) -> str:
    return "A"*argument
print(function(4))
# Function returning via yield using Iterator[type]
def yield_function(argument:int) -> Iterator[str]:
    for _ in range(argument):
        yield "A"

temp:Iterator[str] = yield_function(4)
string:str = ""
for char in temp:
    string += char
print(string)<jupyter_output>AAAA<jupyter_text>3. Type checking and linting with mypyAs you may note, no error is raised by default if the type declaration is not correct:<jupyter_code>integer:int = "STRING"
integer<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10953133228014744, 'fraction_numerical': 0.009478672985781991, 'mean_word_length': 5.270627062706271, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '414433', 'license': 'MIT', 'n_tokens_mistral': 643, 'n_tokens_neox': 623, 'n_words': 217, 'path': 'python_module_tutorials/mypy.ipynb', 'repo_name': 'Paulocracy/python_tutorials', 'size': '1899'}