Skip to content
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

Example in PEP 544 seems to be wrong #1231

Closed
MaxG87 opened this issue Nov 11, 2019 · 3 comments
Closed

Example in PEP 544 seems to be wrong #1231

MaxG87 opened this issue Nov 11, 2019 · 3 comments

Comments

@MaxG87
Copy link

MaxG87 commented Nov 11, 2019

I am on Python 3.8 and use MyPy 0.740

I consider the following example from PEP 544:

from typing import Any, Protocol

class ProtoA(Protocol):
    def meth(self, x: int) -> int: ...
class ProtoB(Protocol):
    def meth(self, obj: Any, x: int) -> int: ...

class C:
    def meth(self, x: int) -> int: ...

a: ProtoA = C  # Type check error, signatures don't match!
b: ProtoB = C  # OK

In the final two lines a class is assigned to a instance. MyPy thus reports

/tmp/protocol-test.py:19: error: Incompatible types in assignment (expression has type "Type[C]", variable has type "ProtoA")
/tmp/protocol-test.py:20: error: Incompatible types in assignment (expression has type "Type[C]", variable has type "ProtoB")
Found 2 errors in 1 file (checked 1 source file)

If I fix that (i.e. adding the missing '()'), than it seems as if the assignment to b should cause a type error, not the assignment to a. Mypy`s report is consistent to my presumption.

I hope it is ok to ask this, since I am here anyways: What is the difference between ... and pass? RTFM with link would be fine.

@ilevkivskyi
Copy link
Member

This is just a mypy bug (or rather a missing feature), it doesn't support class objects and modules as implementations of protocols, see python/mypy#4536

@MaxG87
Copy link
Author

MaxG87 commented Nov 11, 2019

I am sorry to insist, but it still holds that the signatures for ProtoA and C match while these for ProtoB and C are in conflict. Thus, at least the comments must be swapped.

@ilevkivskyi
Copy link
Member

You can just try examples in REPL:

a: ProtoA = C  # You say this should be accepted
a.meth(42)  # Oops TypeError here

You can also read some tutorials about how bound and unbound methods work in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants