You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Named tuples in import cycles still fail sometimes #4442
It looks like issue #3054 was not completely fixed. Namely, in some scenarios named tuples
in import cycles result in Invalid type. For example, checking this file
importposix
results in
typeshed/stdlib/3/os/__init__.pyi:331: error: Invalid type "posix.uname_result"
typeshed/stdlib/3/os/__init__.pyi:599: error: Invalid type "posix.times_result"
typeshed/stdlib/3/os/__init__.pyi:605: error: Invalid type "posix.waitid_result"
typeshed/stdlib/3/os/__init__.pyi:622: error: Invalid type "posix.sched_param"
typeshed/stdlib/3/os/__init__.pyi:624: error: Invalid type "posix.sched_param"
typeshed/stdlib/3/os/__init__.pyi:625: error: Invalid type "posix.sched_param"
Note that the above "invalid" types are all named tuples and there is an import cycle os <-> posix. Interestingly, import os and from os import * both typecheck cleanly.
I haven't yet tried to find a simple repro (not involving typeshed).
Mypy executes from os import stat_result, but then proceeds to import the rest of os/__init__.pyi, resulting in an error when uname_result is undefined.
@ilevkivskyi Can you come up with a self-contained example that reproduces the problem? I'd like to know if #4695 fixed this issue, and if not, I'll look at fixing this.
@JukkaL Unfortunately, I didn't yet find a simple repro, but there is a "dirty" way of testing it by just using the offending typeshed commit 81751306883. I just tried it, and the error is still present, it was not fixed by #4695.
Activity
ilevkivskyi commentedon Jan 9, 2018
Somehow this breaks typshed build since recently, so I raised priority to high.
move os.stat_result into posix stub
move os.stat_result into posix stub
move os.stat_result into posix stub (#1818)
elliott-beach commentedon Jan 10, 2018
I deleted code from the typestub until I got, in
posix.pyi,and in
os/__init__.pyi,so there is something fishy with the way circular imports are resolved, but it doesn't seem to me to involve NamedTuple specifically.
elliott-beach commentedon Jan 10, 2018
Actually, I think this is just a case of the bad circular import discussed in https://stackoverflow.com/a/40094439/5749914 (posix.py is Y.py, os/__init__.py is X.py)
Mypy executes
from os import stat_result, but then proceeds to import the rest ofos/__init__.pyi, resulting in an error whenuname_resultis undefined.gvanrossum commentedon Jan 10, 2018
elliott-beach commentedon Jan 10, 2018
Yes, but if it wouldn't work by python's runtime, I figure there is no reason to expect it to work for mypy, at least in this case IMO.
elliott-beach commentedon Jan 11, 2018
@ilevkivskyi I think this ought to be closed because the behavior is reasonable, what do you think?
JukkaL commentedon Mar 8, 2018
@ilevkivskyi Can you come up with a self-contained example that reproduces the problem? I'd like to know if #4695 fixed this issue, and if not, I'll look at fixing this.
ilevkivskyi commentedon Mar 9, 2018
@JukkaL Unfortunately, I didn't yet find a simple repro, but there is a "dirty" way of testing it by just using the offending typeshed commit
81751306883. I just tried it, and the error is still present, it was not fixed by #4695.ilevkivskyi commentedon Mar 9, 2018
@JukkaL
OK, I will spend some more time now to find a simple repro for you.
ilevkivskyi commentedon Mar 9, 2018
So, here is a simplest repro I can find:
Then one needs to run
mypy b.pyand get:Note that error disappears if
tpis a normal class.Also note that the same error also appears if one or both of
aandbare packages and use named tuples in__init__.py.Update imported Vars in the third pass (#5635)