Git Product home page Git Product logo

Comments (2)

vxgmichel avatar vxgmichel commented on May 28, 2024

To add a bit more information, the same example works fine if the I type is included within the P param spec (see that in the playground).

However, being able to extract a type from the param spec as seen in the first code sample is really useful. In particular, the type of the first argument might appear in other methods of the protocol, for instance:

# 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:
        ...

    def prepare(self, *args: P.args, **kwargs: P.kwargs) -> Callable[[I], O]:
        ...

from mypy.

vxgmichel avatar vxgmichel commented on May 28, 2024

I just came up with a simpler example that can reproduce the regression, without Protocol nor decorators:

from typing import (
    Generic,
    TypeVar,
    Callable,
    Concatenate,
    ParamSpec,
    reveal_type,
)
from dataclasses import dataclass


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


@dataclass
class Convertor(Generic[I, P, O]):
    convert: Callable[Concatenate[I, P], O]


def as_list(source: X, repeat: int = 1) -> list[X]:
    return [source] * repeat


as_list_convertor = Convertor(convert=as_list)


if __name__ == "__main__":
    reveal_type(as_list_convertor.convert)
    result = as_list_convertor.convert(1, repeat=3)
    reveal_type(result)
    assert result == [1, 1, 1]

You can see in the playground that it passes with mypy 1.6 but starts failing with mypy 1.7.

from mypy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.