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 }}
This repository was archived by the owner on Sep 3, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 3, 2026. It is now read-only.
pytype hangs on alias from another module inside class #612
I believe we currently only allow aliases inside a class to another member of the same class, due to some parser limitations. But the parser usually prints a sensible error message instead of hanging forever, so at the least that should be fixed.
I discovered this limitation while working on python/typeshed#4275. One temporary solution would be if pytype would just treating those aliases as Any. This would allow type stubs with those aliases to work at least. But I am not sure how complex this would be to implement. This discussion is probably out of scope for this issue, though.
Not sure if pytype has more permissive rules around Type, but I don't think that would fix all issues in pytest_mock. MockFixture.mock_open is a function, for example (an alias of unittest.mock.mock_open), and I don't think you're allowed to Type[] a function.
When trying to resolve this aliasing, pytype gets stuck into this infinite while loop in pytype/pyi/parser.py:
while isinstance(val, pytd.Alias):
if isinstance(val.type, pytd.NamedType):
_, _, base_name = val.type.name.rpartition(".")
if base_name in vals_dict:
val = vals_dict[base_name]
continue
raise ParseError(
"Illegal value for alias %r. Value must be an attribute "
"on the same class." % val.name)
I haven't tried to figure out what this loop is supposed to do yet so I don't know how to fix this.
Being able to do this kind of aliasing would allow me to get rid of some copy/pasta in stub files that I'm currently working on.
Activity
rchen152 commentedon Jun 26, 2020
I believe we currently only allow aliases inside a class to another member of the same class, due to some parser limitations. But the parser usually prints a sensible error message instead of hanging forever, so at the least that should be fixed.
srittau commentedon Jun 26, 2020
I discovered this limitation while working on python/typeshed#4275. One temporary solution would be if pytype would just treating those aliases as
Any. This would allow type stubs with those aliases to work at least. But I am not sure how complex this would be to implement. This discussion is probably out of scope for this issue, though.rchen152 commentedon Jun 26, 2020
Rewriting
as
seems to work. (which leads to the question: could pytype just automatically do this rewrite behind the scenes?)
JelleZijlstra commentedon Jun 28, 2020
Not sure if pytype has more permissive rules around
Type, but I don't think that would fix all issues in pytest_mock.MockFixture.mock_openis a function, for example (an alias ofunittest.mock.mock_open), and I don't think you're allowed toType[]a function.Akuli commentedon Jul 24, 2020
I ran into possibly the same problem with a class from the same module. Minimal repro (.pyi file)
When trying to resolve this aliasing, pytype gets stuck into this infinite while loop in
pytype/pyi/parser.py:I haven't tried to figure out what this loop is supposed to do yet so I don't know how to fix this.
Being able to do this kind of aliasing would allow me to get rid of some copy/pasta in stub files that I'm currently working on.
rchen152 commentedon Jul 24, 2020
Thanks for tracking that down! I think I'll try just putting in a type of
Anywhen a loop is detected - beats hanging forever.Akuli commentedon Jul 24, 2020
What is currently the preferred way to say "
Bar.methhas the same arguments and return types asFoo.meth"? This doesn't seem to work:Edit: If the
Anyworkaround will be released in a few days then nevermind.pyi parser: allow aliases inside a class to values outside the class.
rchen152 commentedon Jul 24, 2020
pytype 2020.07.24 contains a workaround that allows arbitrary aliases as class attributes; let me know if something isn't working.
update pytype to get workaround for google/pytype#612