1 of 10

pytype

A static type analyzer for Python code

https://google.github.io/pytype

2 of 10

When I usually talk to folks about pytype, they want to know how it compares to mypy.

What do you think are the main advantages of Pytype over Mypy?

How do pytype and mypy compare?

3 of 10

mypy

pytype

  • Gradual typing: code explicitly opts-in to analysis by adding annotations.
  • Strict: disallows operations that change types.
  • Inference: analyzes all of your code, inferring types for unannotated parts.
  • Lenient: allows all operations that succeed at runtime and don’t contradict annotations.

4 of 10

Gradual typing and inference

def f():

return "PyCon"

def g():

return f() + 2019

5 of 10

Gradual typing and inference: mypy

def f() -> str:

return "PyCon"

def g() -> int:

return f() + 2019

mypy: 4: error: Unsupported operand types for +

6 of 10

Gradual typing and inference: pytype

def f():

return "PyCon"

def g():

return f() + 2019

pytype: line 4, in g: unsupported operand type(s) for +

7 of 10

Strictness

from typing import List

def get_list() -> List[str]:

lst = ["PyCon"]

lst.append(2019)

return lst

8 of 10

Strictness: mypy

from typing import List

def get_list() -> List[str]:

lst = ["PyCon"] # List[str]

lst.append(2019) # List[str].append(int)

return lst

mypy: 4: error: Argument 1 to "append" of "list" has incompatible type

9 of 10

Strictness: pytype

from typing import List

def get_list() -> List[str]:

lst = ["PyCon"] # List[str]

lst.append(2019) # List[Union[str, int]]

return lst # List[Union[str, int]] != List[str]

pytype: line 5, in get_list: bad option in return type

10 of 10

pytype

A static type analyzer for Python code

https://google.github.io/pytype

Optional static typing for Python

mypy-lang.org