File size: 2,638 Bytes
d8a7ddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Tests type variable variance inference for generic protocols.
"""

# Specification: https://typing.readthedocs.io/en/latest/spec/protocol.html#generic-protocols

from typing import Protocol, TypeVar
from typing_extensions import ParamSpec

T1 = TypeVar("T1")
T2 = TypeVar("T2", bound=int)
T3 = TypeVar("T3", bytes, str)
T1_co = TypeVar("T1_co", covariant=True)
T1_contra = TypeVar("T1_contra", contravariant=True)
P = ParamSpec("P", contravariant=True)
R = TypeVar("R", covariant=True)

# > Type checkers will warn if the inferred variance is different from the
# > declared variance.


class AnotherBox(Protocol[T1]):  # E: T should be covariant
    def content(self) -> T1:
        ...


class Protocol1(Protocol[T1, T2, T3]):  # OK
    def m1(self, p0: T1, p1: T2, p2: T3) -> T1 | T2:
        ...

    def m2(self) -> T1:
        ...

    def m3(self) -> T2:
        ...

    def m4(self) -> T3:
        ...


class Protocol2(Protocol[T1, T2, T3]):  # E: T3 should be contravariant
    def m1(self, p0: T1, p1: T2, p2: T3) -> T1:
        ...

    def m2(self) -> T1:
        ...

    def m3(self) -> T2:
        ...


class Protocol3(Protocol[T1_co]):
    def m1(self) -> None:
        pass


class Protocol4(Protocol[T1]):  # E: T1 should be contravariant
    def m1(self, p0: T1) -> None:
        ...


class Protocol5(Protocol[T1_co]):  # E[covariant_in_input+]
    def m1(self, p0: T1_co) -> None:  # E[covariant_in_input+]
        ...


class Protocol6(Protocol[T1]):  # E: T1 should be covariant
    def m1(self) -> T1:
        ...


class Protocol7(Protocol[T1_contra]):  # E[contravariant_in_output+]
    def m1(self) -> T1_contra:  # E[contravariant_in_output+]
        ...


class Protocol8(Protocol[T1]):  # OK
    def m1(self) -> T1:
        ...

    def m2(self, p1: T1) -> None:
        pass


class Callback(Protocol[P, R]):  # OK
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
        ...


class Protocol9(Protocol[T1_co]):  # OK
    @property
    def prop1(self) -> T1_co:
        ...


class Protocol10(Protocol[T1_co]):  # OK
    def m1(self) -> type[T1_co]:
        ...


class Protocol11(Protocol[T1]):  # OK
    x: T1 | None


class Protocol12(Protocol[T1]):  # E: T1 should be covariant
    # __init__ method is exempt from variance calculations.
    def __init__(self, x: T1) -> None:
        ...


class Protocol13(Protocol[T1_contra]):  # OK
    def m1(self: "Protocol13[T1_contra]", x: T1_contra) -> None:
        ...

    @classmethod
    def m2(cls: "type[Protocol13[T1_contra]]") -> None:
        ...


class Protocol14(Protocol[T1]):  # OK
    def m1(self) -> list[T1]:
        ...