pytype icon indicating copy to clipboard operation
pytype copied to clipboard

pytype does not recognize partial(attr.ib, ...)

Open pepoluan opened this issue 4 years ago • 2 comments

So I have a class defined like this:

import attr
from functools import partial

@attr.s
class AuthResult:
    """
    Contains the result of authentication, to be returned to the smtp_AUTH method.
    All initialization arguments _must_ be keyworded!
    """
    _kwattr = partial(attr.ib, kw_only=True)

    success: bool = _kwattr()
    handled: bool = _kwattr(default=True)
    message: Optional[str] = _kwattr(default=None)
    auth_data: Optional[Any] = _kwattr(default=None, repr=lambda x: "...")

PyCharm recognizes this no problem, and when I type AuthResult( in PyCharm's Python console, it provides the expected "intellisense" pop up:

image

But pytype exploded with errors:

[4/36] check aiosmtpd.smtp
FAILED: /home/pepoluan/projects/aiosmtpd/.pytype/pyi/aiosmtpd/smtp.pyi
/home/pepoluan/projects/aiosmtpd/.tox/static/bin/python -m pytype.single --disable not-supported-yet --imports_info /home/pepoluan/projects/aiosmtpd/.pytype/imports/aiosmtpd.smtp.imports --module-name aiosmtpd.smtp -V 3.8 -o /home/pepoluan/projects/aiosmtpd/.pytype/pyi/aiosmtpd/smtp.pyi --analyze-annotated --nofail --quick /home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1048, in _authenticate: Invalid keyword arguments (auth_data, handled, success) to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, auth_data, handled, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1050, in _authenticate: Invalid keyword arguments (handled, success) to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, handled, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1078, in auth_PLAIN: Invalid keyword argument success to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1084, in auth_PLAIN: Invalid keyword arguments (handled, success) to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, handled, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1092, in auth_PLAIN: Invalid keyword arguments (handled, success) to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, handled, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1104, in auth_LOGIN: Invalid keyword argument success to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1111, in auth_LOGIN: Invalid keyword arguments (handled, success) to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, handled, success)
File "/home/pepoluan/projects/aiosmtpd/aiosmtpd/smtp.py", line 1117, in auth_LOGIN: Invalid keyword argument success to function AuthResult.__init__ [wrong-keyword-args]
         Expected: (self)
  Actually passed: (self, success)

This is the impacted file: https://github.com/pepoluan/aiosmtpd/blob/31f6b73b1eab10ec35a20bd90ef393005bd97467/aiosmtpd/smtp.py

If you check into those files, the error should not happen because they do call init using keywords:

Line 1048:

                return AuthResult(success=True, handled=True, auth_data=auth_data)

Line 1050:

                return AuthResult(success=False, handled=False)

... and so on.

Others do not complain:

  • flake8 does not complain
  • Running tests does not complain

pepoluan avatar Mar 06 '21 18:03 pepoluan

Update:

I tried moving _kwattr out of the class, so it looks like this:

import attr
from functools import partial

_kwattr = partial(attr.ib, kw_only=True)

@attr.s
class AuthResult:
    """
    Contains the result of authentication, to be returned to the smtp_AUTH method.
    All initialization arguments _must_ be keyworded!
    """
    success: bool = _kwattr()
    handled: bool = _kwattr(default=True)
    message: Optional[str] = _kwattr(default=None)
    auth_data: Optional[Any] = _kwattr(default=None, repr=lambda x: "...")

Again, no issues with flake8 or pytest, but pytype still barfed with the same bunch of [wrong-keyword-args] errors.

pepoluan avatar Mar 06 '21 18:03 pepoluan

Thanks for the report. I reproduced the error with:

import attr
from functools import partial

@attr.s
class AuthResult:
    _kwattr = partial(attr.ib, kw_only=True)
    success: bool = _kwattr()

x = AuthResult(success=False)  # incorrect [wrong-keyword-args] error

rchen152 avatar Mar 11 '21 01:03 rchen152