mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Regression since mypy 1.7 with functions that return a generic protocol

Open vxgmichel opened this issue 1 year ago • 0 comments

Bug Report

The following code used to pass with mypy 1.6.1

from typing import (
    Protocol,
    TypeVar,
    Callable,
    Concatenate,
    ParamSpec,
    reveal_type,
)


X = TypeVar("X")
Y = TypeVar("Y")
P = ParamSpec("P")
I = TypeVar("I", contravariant=True)
O = TypeVar("O", covariant=True)


# A callable converting I to O with parameters P
ConvertorCallable = Callable[Concatenate[I, P], O]


# An object that can convert I to O with parameters P
class ConvertorProtocol(Protocol[I, P, O]):
    def convert(self, __source: I, *args: P.args, **kwargs: P.kwargs) -> O:
        ...


# Decorator to convert a callable to a convertor
def convertor(
    func: ConvertorCallable[X, P, Y],
) -> ConvertorProtocol[X, P, Y]:
    class Convertor:
        def convert(self, source: X, /, *args: P.args, **kwargs: P.kwargs) -> Y:
            return func(source, *args, **kwargs)

    return Convertor()


# A convertor that converts X to a list of X
@convertor
def as_list(source: X, repeat: int = 1) -> list[X]:
    return [source] * repeat


if __name__ == "__main__":
    result = as_list.convert(1, repeat=3)
    reveal_type(result)
    print(result)

However, it no longer passes with mypy 1.7.0 and later:

error: Argument 1 to "convert" of "ConvertorProtocol" has incompatible type "int"; expected Never  [arg-type]

Try it in the playground.

Note that this sample works in the pyright playground.

vxgmichel avatar Apr 29 '24 18:04 vxgmichel