pytype icon indicating copy to clipboard operation
pytype copied to clipboard

A function reading a global variable and a loop causes an error.

Open relent95 opened this issue 6 years ago • 2 comments

When the following code is processed,

x = 1
def test():
	print(x)

i = 0
while i < 3:
	i += 1

the following error occurs.

pytype-single --imports_info /d/test_pytype/.pytype/imports/test_loop.imports --module-name test_loop -V 3.7 -o /d/test_pytype/.pytype/pyi/test_loop.pyi --analyze-annotated --nofail --quick /d/test_pytype/test_loop.py
ERROR:pytype.analyze:No visible options for i
File "/d/test_pytype/test_loop.py", line 3, in test: Name 'x' is not defined [name-error]

I facing many similar errors in the real world code base I currently working on.

relent95 avatar Nov 08 '19 07:11 relent95

O_o That's a weird one. I'll dig into a bit more when I have time.

rchen152 avatar Nov 08 '19 21:11 rchen152

Ok, I figured out what's happening. pytype is looking at i = 0; while i < 3 and deciding that the while loop can't terminate, so it thinks that definitions from import time are unreachable.

The easiest way I can think of to fix this is to discard the concrete value of i when doing the comparison.

rchen152 avatar Dec 06 '19 21:12 rchen152