查看data-science-types个Package.
pip install data-science-types
MyPy现在可以使用Numpy、Pandas和Matplotlib存根.
# program.py
import numpy as np
import pandas as pd
arr1: np.ndarray[np.int64] = np.array([3, 7, 39, -3]) # OK
arr2: np.ndarray[np.int32] = np.array([3, 7, 39, -3]) # Type error
df: pd.DataFrame = pd.DataFrame({'col1': [1,2,3], 'col2': [4,5,6]}) # OK
df1: pd.DataFrame = pd.Series([1,2,3]) # error: Incompatible types in assignment (expression has type "Series[int]", variable has type "DataFrame")
像平常一样使用mypy.
$ mypy program.py
函数参数的用法
def f(df: pd.DataFrame):
return df.head()
if __name__ == "__main__":
x = pd.DataFrame({'col1': [1, 2, 3, 4, 5, 6]})
print(f(x))
$ mypy program.py
> Success: no issues found in 1 source file