← Back to index

protocols_modules.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 3
FP: 2
FN: 0
Optional: 0 / 0
1"""
2Tests the handling of modules as the implementation of a protocol.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/protocol.html#modules-as-implementations-of-protocols
6
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.
9
10import _protocols_modules1
11import _protocols_modules2
12from typing import Protocol
15class Options1(Protocol):
16 timeout: int
17 one_flag: bool
18 other_flag: bool
21class Options2(Protocol):
22 timeout: str
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:
31 ...
33 def on_success(self) -> None:
34 ...
37class Reporter2(Protocol):
38 def on_error(self, x: int) -> int:
39 ...
42class Reporter3(Protocol):
43 def not_implemented(self, x: int) -> int:
44 ...
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`