You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My main question here would be whether this fails too:
x=Foox()
Because if so, it disallows perfectly valid code:
x: Foo=ConcreteFoox()
and if it doesn't, it's a really special case that probably doesn't help much (in my opinion. Perhaps you've run into this and think it would be worth it, which is perfectly valid). Note that mypy has historically gone the all-the-way route with abstract base classes and this prompted #5374 (#5374 (comment) for reference).
fromtypingimportProtocol, TypeclassFoo(Protocol):
...
classConcreteFoo(Foo):
...
deffun(x: Type[Foo]) ->Foo:
returnx()
fun(Foo) # error: Argument of type "Type[Foo]" cannot be assigned to parameter "x" of type "Type[Foo]" in function "fun" "Foo" is not a concrete class type and cannot be assigned to typefun(ConcreteFoo) # fine
So basically Pyright seems to prohibit using a protocol class as a type (even as an argument to a function). This makes sense to me: I often want to pass in a concrete class as a parameter, but I don't I ever have had a use case for passing in an abstract class as a parameter.
fromtypingimportProtocolclassFoo(Protocol):
x: intdef__init__(self, x: int) ->None:
self.x=xclassConcreteFoo(Foo):
x: intConcreteFoo(x=1) # ConcreteFoo.__init__() takes exactly one argument (the instance to initialize)
Currently this fails at runtime with quite confusing behavior/errors because of the monkey patching that Protocol does to __init__.
My hope was that this check could be moved from runtime monkey patching to mypy.
Activity
A5rocks commentedon Mar 1, 2022
My main question here would be whether this fails too:
Because if so, it disallows perfectly valid code:
and if it doesn't, it's a really special case that probably doesn't help much (in my opinion. Perhaps you've run into this and think it would be worth it, which is perfectly valid). Note that mypy has historically gone the all-the-way route with abstract base classes and this prompted #5374 (#5374 (comment) for reference).
adriangb commentedon Mar 1, 2022
Interesting, that is a good point.
For your first example, Pyright flags it with the same error.
For your second example, Pyright allows it without errors.
But I don't think your second example is valid. It should be:
Which Pyright disallows with:
For the example presented in #5374 (comment):
So basically Pyright seems to prohibit using a protocol class as a type (even as an argument to a function). This makes sense to me: I often want to pass in a concrete class as a parameter, but I don't I ever have had a use case for passing in an abstract class as a parameter.
As additional context, the reason I brought this up is https://bugs.python.org/issue44807
Currently this fails at runtime with quite confusing behavior/errors because of the monkey patching that
Protocoldoes to__init__.My hope was that this check could be moved from runtime monkey patching to mypy.
Rahix commentedon Mar 17, 2022
There are usecases, see #4717.
hauntsaninja commentedon May 1, 2023
mypy complains about the original example. Looks like this got changed a while back, in 0.950