google / pytype

A static type analyzer for Python code

Home Page:https://google.github.io/pytype

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type annotation for pandas

gretadolcetti opened this issue · comments

I am using pytype to get type annotations for a given Python program.

For example for program test.py

a = 1
b = 1.0

after executing pytype test.py and merge-pyi test.py .pytype/pyi/test.pyi I obtain

a: int = 1
b: float = 1.0

which is the desired output.

The problem is that I need annotation for types coming from external libraries, specifically pandas.

So that for the program

import pandas as pd

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

df = pd.DataFrame(data)

print(df)

I would like to obtain

import pandas as pd
from typing import Any, Dict, List

data: Dict[str, List[int]] = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

df: pd.DataFrame = pd.DataFrame(data)

print(df)

but I obtain

import pandas as pd
from typing import Any, Dict, List

data: Dict[str, List[int]] = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

df: Any = pd.DataFrame(data)

print(df)

What can I do?

I've seen that pytype gets stubs from typeshed and pandas is not there, but I see that the annotation for external libraries is ": Any" also for libraries that are present in typeshed.

Stubs for pandas exist in pandas-stubs, how could I integrate them and instruct pytype to use them?

pytype can't use the pandas stubs yet because of #151 =(
(Inside Google, pytype is integrated into the build system, so we don't have a need for stub packages, which means that it's hard to find the resources to support them, sadly.)