Mypy: Keyword arguments of __call__ are not interpreted correctly

Created on 25 Apr 2017  路  4Comments  路  Source: python/mypy

I looks like mypy drops all keyword arguments of the __call__ function of a class and recognises only positional arguments.

Code example:

class Formatter:
    def __call__(
        self,
        message: str,
        *,  # same behaviour with and without 
        bold: bool = False,
    ) -> str:
        if bold:
            message = f"** {message} **"
        return message

formatter = Formatter()
print(formatter("test", bold=True))

Mypy error:
test.py:13: error: Unexpected keyword argument "bold" for "__call__" of "Formatter"

bug size-small

Most helpful comment

Yes, confirmed and seems specific to __call__.

All 4 comments

Yes, confirmed and seems specific to __call__.

Adding __call__ to MAGIC_METHODS_ALLOWING_KWARGS in sharedparse.py fixes the issue. I wonder why it is not there now, do older versions of Python not allow kwargs in __call__?

I assume it was just an oversight. Do the tests pass with that one change? Then it's worth adding a test specifically for __call__ with kwargs and push a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JukkaL picture JukkaL  路  30Comments

philthompson10 picture philthompson10  路  40Comments

timabbott picture timabbott  路  28Comments

dmoisset picture dmoisset  路  49Comments

tjltjl picture tjltjl  路  35Comments