Skip to content
This repository was archived by the owner on Sep 3, 2026. It is now read-only.
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

Closed

Description

@srittau

Please consider the following stub file:

import unittest.mock

class MockFixture:
    Mock = unittest.mock.Mock

When checking this with pytype (2020.06.01), pytype hangs indefinitely, while consuming 100% CPU time.

Activity

  1. self-assigned this
    on Jun 26, 2020
  2. rchen152 commented on Jun 26, 2020

    @rchen152
    Contributor

    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.

  3. srittau commented on Jun 26, 2020

    @srittau
    ContributorAuthor

    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.

  4. rchen152 commented on Jun 26, 2020

    @rchen152
    Contributor

    Rewriting

    class Foo:
      Alias = SomeType
    

    as

    from typing import Type
    class Foo:
      Alias: Type[SomeType]
    

    seems to work. (which leads to the question: could pytype just automatically do this rewrite behind the scenes?)

  5. JelleZijlstra commented on Jun 28, 2020

    @JelleZijlstra
    Contributor

    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.

  6. Akuli commented on Jul 24, 2020

    @Akuli

    I ran into possibly the same problem with a class from the same module. Minimal repro (.pyi file)

    class Foo:
        def meth(self) -> None: ...
    
    class Bar:
        meth = Foo.meth

    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.

  7. rchen152 commented on Jul 24, 2020

    @rchen152
    Contributor

    Thanks for tracking that down! I think I'll try just putting in a type of Any when a loop is detected - beats hanging forever.

  8. Akuli commented on Jul 24, 2020

    @Akuli

    What is currently the preferred way to say "Bar.meth has the same arguments and return types as Foo.meth"? This doesn't seem to work:

    class Foo:
        def meth(self) -> None: ...
    
    class Bar:
        meth: Type[Foo.meth]
    

    Edit: If the Any workaround will be released in a few days then nevermind.

  9. rchen152 commented on Jul 24, 2020

    @rchen152
    Contributor

    pytype 2020.07.24 contains a workaround that allows arbitrary aliases as class attributes; let me know if something isn't working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions