pytype icon indicating copy to clipboard operation
pytype copied to clipboard

Classes defined inside a function aren't recognised as valid type annotations

Open AdamHillier opened this issue 5 years ago • 3 comments

The following code triggers a Pytype error Invalid type annotation 'A' [invalid-annotation]. Name 'A' is not defined:

def some_function():
    class A:
        pass

    class B:
        a: A = A()

This behaviour is incorrect as A is clearly defined and in-scope for use in the type annotation.

If instead the two classes are defined at the module level, outside of the body of a function, there is (correctly) no error raised:

class A:
    pass

class B:
    a: A = A()

This issue exists in pytype==2020.06.26 on MacOS, with Python 3.7.

AdamHillier avatar Jul 10 '20 13:07 AdamHillier

I just checked and this issue is not present in pytype==2020.4.22 but is present in pytype==2020.5.13 (and all later releases), so it must have been introduced between the two.

AdamHillier avatar Jul 10 '20 13:07 AdamHillier

The bug appears to have been introduced by https://github.com/google/pytype/commit/b57cc280959d570ec9607ae029597793fff703cf.

rchen152 avatar Jul 14 '20 01:07 rchen152

Unassigning myself since I'm not actively working on this.

rchen152 avatar Jan 20 '21 01:01 rchen152