
Am 20.05.19 um 16:30 schrieb Guido van Rossum:
I'm planning to accept the following PEPs related to the type system later this week:
PEP 544: Protocols PEP 586: Literal Types PEP 591: Final qualifier and @final decorator PEP 589: TypedDict
All of these have been proposed for and discussed before.
I look forward to these PEPs becoming part of the standard Python type system. One thing is a bit unclear to me in PEP 544, though: Can explicit protocols be used to mark classes that implement some of the protocol members dynamically. For example: ------------------------ from typing import Protocol, Any class Foo(Protocol): def bar(self) -> str: ... class ExplicitFoo(Foo): pass ExplicitFoo.bar = lambda self: "some-value" # type: ignore class ImplicitFoo: pass ImplicitFoo.bar = lambda self: "some-value" # type: ignore x1: Foo = ExplicitFoo() x2: Foo = ImplicitFoo() ------------------------ The text in the section "Explicitly declaring implementation" is a bit ambiguous about it, saying "So while it's possible to subclass a protocol explicitly, it's not necessary to do so for the sake of type-checking." In the case of ExplicitFoo, explicitly deriving from the protocol seems to be necessary. The section "Subtyping relationships with other types" actually says that ExplicitFoo is not a subtype of Foo: "A concrete type X is a subtype of protocol P if and only if X implements all protocol members of P with compatible types. ..." This contradicts the "Rejected ideas" section which says "Explicit subclassing makes it possible to force a class to be considered a subtype of a protocol ..." as well as mypy's implementation of protocols, which accepts the second to last line in the example above (but of course not the last one). Whatever the intention of the PEP, I think it would be good to clarify that. (Personally I think the latter interpretation is more useful for examples like above, or protocols implemented using __getattr__.) - Sebastian