-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent Treatment of Class Constructor by Protocol #10482
Comments
Hm, this is because in typeshed TBH I am not sure which one is better. |
Typeshed is technically right there: |
Problem is not much in what it takes, but in what it returns, it should be |
I see, opened python/typeshed#9215 to change it. Let's see if there's any negative effects. |
Btw funny thing is that we have exactly a test like the original repro, but it passes, because we use test fixtures for most protocol tests, not full typeshed stubs. |
@JelleZijlstra Hm, it looks like it didn't work (see |
The return type being a protocol isn't important. I think that we should compare the signature of Another example: from typing import Protocol
class P(Protocol):
def __call__(self, x: int) -> C: ...
class C:
def __init__(self, x: str) -> None: ...
def f(x: str) -> C:
return C(x)
x: P = C # no error
y: P = f # yes error |
In the code below we define one protocol which, when called, returns another. In effect we're describing a "factory". However, while MyPy is able to recognize incorrect protocol implementations when given a function, it fails to do the same for class constructors.
Playground link: https://mypy-play.net/?mypy=latest&python=3.9&gist=3b9250b17032b659fc36746e659bcdb5
We would expect MyPy to give an error in both cases given they result in the same type, but it only complains when given a standard function.
The text was updated successfully, but these errors were encountered: