Why does Mypy return error when I call a fronzen dataclass property as a function?

Viewed 140

OkCase1, OkCase2, and var are no problem. But Mypy returned this message for self.func(input) in MypyErrorCase1

Invalid self argument "MypyErrorCase1" to attribute function "func" with type "Callable[[Input], Result]" "Result" not callable

The error is Mypy bug? (Should I create an issue on Mypy?) Or Should Mypy return an error for the calling frozen dataclass attribute as a function?

(In any case, the 2nd error message "Result" not callable is not suitable.)

from dataclasses import dataclass
from typing import Callable


class Input:
    pass


class Result:
    pass


FuncA = Callable[[Input], Result]


@dataclass(frozen=True)
class OkCase1:
    func: FuncA

    def call_func(self):
        return self.func()


@dataclass
class OkCase2:
    func: FuncA

    def call_func(self, input: Input):
        return self.func(input)


@dataclass(frozen=True)
class MypyErrorCase1:
    func: FuncA

    def call_func(self, input: Input):
        return self.func(input)


var: FuncA = lambda input: Result()  # No Error

# [{
#   "resource": "/Users/me/mypy_said_not_callable.py",
#   "owner": "python",
#   "code": "error",
#   "severity": 8,
#   "message": "Invalid self argument \"MypyErrorCase1\" to attribute function \"func\" with type \"Callable[[Input], Result]\"",
#   "source": "mypy",
#   "startLineNumber": 37,
#   "startColumn": 16,
#   "endLineNumber": 37,
#   "endColumn": 16
# }]

# [{
#   "resource": "/Users/me/mypy_said_not_callable.py",
#   "owner": "python",
#   "code": "error",
#   "severity": 8,
#   "message": "\"Result\" not callable",
#   "source": "mypy",
#   "startLineNumber": 37,
#   "startColumn": 16,
#   "endLineNumber": 37,
#   "endColumn": 16
# }]
$ python --version
Python 3.9.7

$ mypy --version
mypy 0.910

$ mypy example.py
example.py:37: error: Invalid self argument "MypyErrorCase1" to attribute function "func" with type "Callable[[Input], Result]"
example.py:37: error: "Result" not callable
Found 2 errors in 1 file (checked 1 source file)
0 Answers
Related