google / pytype

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

annotate_ast.annotate_source gets wrong line number for multi-line list in Python 3.7+ #818

Open maldil opened 3 years ago

maldil commented 3 years ago

I observed following behavior in annotate_ast.annotate_source

The AST annotator correctly annotate the type of _VERSION as str and type of REQUIRED_PACKAGES as List[nothing] in below example.

_VERSION = '1.12.0'
REQUIRED_PACKAGES = []

As the next step, I updated the code as below.

_VERSION = '1.12.0'
REQUIRED_PACKAGES = [
     'absl-py >= 0.1.6',        
     'numpy >= 1.16.0',         
 ]

It annotates type of _VERSION correctly but it does not annotate REQUIRED_PACKAGES. I checked the AnnotateAstVisitor to find a possible root cause for this behavior. I observed they visit Name nodes. Therefore I believe, it should visit the node REQUIRED_PACKAGES. Any help to debug this error is very much appreciated.

Pytype version : 2021.1.8 OS: MacOS

rchen152 commented 3 years ago

annotate_ast appears to correctly annotate REQUIRED_PACKAGES in Python 3.6 but fail to find an annotation in 3.7, which points to a bug in how we're handling bytecode/line number changes between the two versions. In particular, https://github.com/google/pytype/blob/7056acd733ea88eef084c90b6bb8c56d74c7096d/pytype/tools/traces/traces.py#L255-L258 looks extremely suspicious to me.