google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.72k stars 274 forks source link

Empty list typed as List[nothing] in generated stub file #1252

Open cebtenzzre opened 2 years ago

cebtenzzre commented 2 years ago

Example Code

a.py

x = []

b.py

from a import x

Expected Behavior

pytype can generate a stub file for a.py that mypy is able to successfully read. It could represent x as e.g. List[object] or List[Any].

Actual Behavior

$ mkdir stubs
$ pytype-single a.py -o stubs/a.pyi
$ MYPYPATH=stubs mypy b.py
stubs/a.pyi:3: error: Name "nothing" is not defined
Found 1 error in 1 file (checked 1 source file)

This is the generated a.pyi:

from typing import List

x: List[nothing]

Software Versions

Python 3.10.2 pytype 2022.06.30 mypy 0.961

rchen152 commented 2 years ago

Changing List[nothing] to List[object] or List[Any] would almost certainly cause problems, since an empty list produces a different type when merged with another list than a non-empty one. But I can see how other type checkers not being able to use pytype-generated stubs is inconvenient; maybe we should add a mode in which pytype outputs stubs that only use features that all checkers support.

martindemello commented 2 years ago

the simplest fix might be to add nothing = Any at the top of the pyi file when run in compatibility mode