sco1/flake8-annotations

ANN001 Missing type annotation for function argument if ANN101 is disabled

finswimmer opened this issue · 3 comments

Describe the bug
When using type comments and having ANN101disabled, I received a ANN001 Missing type annotation for function argument for method containing a self argument.

In contrast, using type hints no error is raised.

Is this intended?

To Reproduce

.flake8:

[flake8]
max-line-length = 88
ignore = E501, E203, W503, ANN101, ANN102

A.py:

class A:
    def __init__(self):  # type: () -> None
        self._var = 1

    @property
    def var(self):  # type: () -> int
        return self._var

    def sum(self, a):  # type: (int) -> int
        return self.var + a
$ flake8 A.py
A.py:9:19: ANN001 Missing type annotation for function argument 'a'

A2.py:

class A:
    def __init__(self) -> None:
        self._var = 1

    @property
    def var(self) -> int:
        return self._var

    def sum(self, a: int) -> int:
        return self.var + a
$ flake8 A2.py

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

$ flake8 --version
3.8.3 (flake8-annotations: 2.3.0, mccabe: 0.6.1, pycodestyle: 2.6.0, pyflakes: 2.2.0) CPython 3.8.5 on Linux

As well as your Python version:

$ python -V
Python 3.8.5
sco1 commented

Greetings, thanks for the report.

Yes, this is the expected behavior. Setting the ANN101 flag does not inject an annotation for self, but rather just ignores the code when yielding errors to flake8. For type comments, this means that # type: (int) -> int is interpreted as hinting self as int, rather than a, resulting in the ANN001 error being yielded.

This type of scenario is what we had in mind for the partial type comment caveat, which is what I was going to recommend (e.g. # type: (..., int) -> int), but this approach seems to clash with assumptions made by at least one linter (Pylance):

Parameter annotation count mismatch: expected 1 but received 2

and also makes mypy error:

error: Ellipses cannot accompany other argument types in function type signature

So... I don't think I'll recommend this, and the logic supporting this caveat should probably be revisited.

To the original problem, while it's the expected behavior I don't think it's the correct behavior; we'll have to do a bit of research into a more appropriate approach. I think prepending an annotation for self and cls for function type comments makes sense & should be painless into incorporate.

sco1 commented

This should be fixed on the dev branch now if you'd like to a look; the provided examples now appear to be behaving as expected:

$ flake8 --ignore=E501,E203,W503,ANN101,ANN102 ./A.py
$ flake8 --ignore=E501,E203,W503,ANN101,ANN102 ./A2.py

I'd like to get #94 fixed before making a new release but it may not be until early next week.

Well done 👍 Thanks a lot 😃