12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import functools
16
- from typing import Any , Dict , Iterable , List , Optional , Tuple , Set , TypeVar , TYPE_CHECKING , Union
17
-
18
15
import abc
16
+ import functools
17
+ from typing import Any , Dict , Iterable , List , Optional , Tuple , Set , TYPE_CHECKING , Union
18
+ from typing_extensions import Self
19
19
20
20
import numpy as np
21
21
24
24
if TYPE_CHECKING :
25
25
import cirq
26
26
27
- TSelf = TypeVar ('TSelf' , bound = '_BaseGridQid' )
28
-
29
27
30
28
@functools .total_ordering
31
29
class _BaseGridQid (ops .Qid ):
@@ -69,13 +67,13 @@ def neighbors(self, qids: Optional[Iterable[ops.Qid]] = None) -> Set['_BaseGridQ
69
67
return neighbors
70
68
71
69
@abc .abstractmethod
72
- def _with_row_col (self : TSelf , row : int , col : int ) -> TSelf :
70
+ def _with_row_col (self , row : int , col : int ) -> Self :
73
71
"""Returns a qid with the same type but a different coordinate."""
74
72
75
73
def __complex__ (self ) -> complex :
76
74
return self .col + 1j * self .row
77
75
78
- def __add__ (self : TSelf , other : Union [Tuple [int , int ], TSelf ]) -> 'TSelf' :
76
+ def __add__ (self , other : Union [Tuple [int , int ], Self ]) -> Self :
79
77
if isinstance (other , _BaseGridQid ):
80
78
if self .dimension != other .dimension :
81
79
raise TypeError (
@@ -94,7 +92,7 @@ def __add__(self: TSelf, other: Union[Tuple[int, int], TSelf]) -> 'TSelf':
94
92
)
95
93
return self ._with_row_col (row = self .row + other [0 ], col = self .col + other [1 ])
96
94
97
- def __sub__ (self : TSelf , other : Union [Tuple [int , int ], TSelf ]) -> 'TSelf' :
95
+ def __sub__ (self , other : Union [Tuple [int , int ], Self ]) -> Self :
98
96
if isinstance (other , _BaseGridQid ):
99
97
if self .dimension != other .dimension :
100
98
raise TypeError (
@@ -113,13 +111,13 @@ def __sub__(self: TSelf, other: Union[Tuple[int, int], TSelf]) -> 'TSelf':
113
111
)
114
112
return self ._with_row_col (row = self .row - other [0 ], col = self .col - other [1 ])
115
113
116
- def __radd__ (self : TSelf , other : Tuple [int , int ]) -> 'TSelf' :
114
+ def __radd__ (self , other : Tuple [int , int ]) -> Self :
117
115
return self + other
118
116
119
- def __rsub__ (self : TSelf , other : Tuple [int , int ]) -> 'TSelf' :
117
+ def __rsub__ (self , other : Tuple [int , int ]) -> Self :
120
118
return - self + other
121
119
122
- def __neg__ (self : TSelf ) -> 'TSelf' :
120
+ def __neg__ (self ) -> Self :
123
121
return self ._with_row_col (row = - self .row , col = - self .col )
124
122
125
123
0 commit comments