sco1/flake8-annotations

False-positive for missing type annotation for "self" on nested classes

sumnerevans opened this issue · 3 comments

Describe the bug
In nested classes, methods which use self are flagged with ANN001 instead of ANN101. This causes problems when the user is attempting to ignore ANN101 but not ANN001 errors.

To Reproduce
Minimal code example to reproduce the behavior:

class Foo:
    class Bar:
        def __init__(self):
            pass

    def __init__(self):
        pass

Results in:

test.py:3:22: ANN001 Missing type annotation for function argument 'self'
test.py:6:18: ANN101 Missing type annotation for self in method

Version Information
Please provide the full output of flake8 --version

$ flake8 --version
3.7.9 (flake8-annotations: 2.0.0, flake8-comprehensions: 3.2.2, flake8-print: 3.1.4, flake8_pep3101: 1.2.1, import-order: 0.18.1, mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.8.1 on Linux

As well as your Python version:

$ python -V
Python 3.8.1
sco1 commented

Thanks, will take a look

sco1 commented

Similar to #69, the cause of this is simple with a seemingly non-simple fix.

The issue here is that the current handling of nesting inside classes is too simplistic. With non-class function nesting, ast.generic_visit() is sufficient to properly visit & parse all nested definitions, but since class node traversal requires a different constructor (to identify contained functions as class method), visiting of these nodes is being handled by the more generic function visitors so they don't get the appropriate class method flag. Broadening the generic walk just results in the methods getting parsed twice, once as a class method and once as a generic method.

I suspect the fix here, once we figure out how to do it, will also help solve #69.

Thanks for fixing this so quickly! Just checked on my projects and it's working as expected now.