2Tests the handling of modules as the implementation of a protocol.
5# Specification: https://typing.readthedocs.io/en/latest/spec/protocol.html#modules-as-implementations-of-protocols
7# > A module object is accepted where a protocol is expected if the public
8# > interface of the given module is compatible with the expected protocol.
10import _protocols_modules1
11import _protocols_modules2
12from typing import Protocol
15class Options1(Protocol):
21class Options2(Protocol):
25op1: Options1 = _protocols_modules1 # OK
Unexpected error
[invalid-assignment] Object of type `<module '_protocols_modules1'>` is not assignable to `Options1`
26op2: Options2 = _protocols_modules1 # E
[invalid-assignment] Object of type `<module '_protocols_modules1'>` is not assignable to `Options2`
29class Reporter1(Protocol):
30 def on_error(self, x: int) -> None:
33 def on_success(self) -> None:
37class Reporter2(Protocol):
38 def on_error(self, x: int) -> int:
42class Reporter3(Protocol):
43 def not_implemented(self, x: int) -> int:
47rp1: Reporter1 = _protocols_modules2 # OK
Unexpected error
[invalid-assignment] Object of type `<module '_protocols_modules2'>` is not assignable to `Reporter1`
48rp2: Reporter2 = _protocols_modules2 # E
[invalid-assignment] Object of type `<module '_protocols_modules2'>` is not assignable to `Reporter2`
49rp3: Reporter3 = _protocols_modules2 # E
[invalid-assignment] Object of type `<module '_protocols_modules2'>` is not assignable to `Reporter3`