11import operator
22import re
33from functools import wraps
4- from typing import Any , Callable , Optional , Pattern , Sequence , Tuple , Union
4+ from typing import Any , Callable , Optional , Pattern , Sequence , Tuple , Type , TypeVar , Union
55
66from magic_filter .exceptions import RejectOperations , SwitchModeToAll , SwitchModeToAny
77from magic_filter .operations import (
1616)
1717from magic_filter .operations .function import ImportantFunctionOperation
1818
19+ MagicT = TypeVar ("MagicT" , bound = "MagicFilter" )
20+
1921
2022class MagicFilter :
2123 __slots__ = ("_operations" ,)
@@ -32,16 +34,16 @@ def wrapper(value: Any) -> Any:
3234 return wrapper
3335
3436 @classmethod
35- def _new (cls , operations : Tuple [BaseOperation , ...]) -> "MagicFilter" :
37+ def _new (cls : Type [ MagicT ] , operations : Tuple [BaseOperation , ...]) -> MagicT :
3638 return cls (operations = operations )
3739
38- def _extend (self , operation : BaseOperation ) -> "MagicFilter" :
40+ def _extend (self : MagicT , operation : BaseOperation ) -> MagicT :
3941 return self ._new (self ._operations + (operation ,))
4042
41- def _replace_last (self , operation : BaseOperation ) -> "MagicFilter" :
43+ def _replace_last (self : MagicT , operation : BaseOperation ) -> MagicT :
4244 return self ._new (self ._operations [:- 1 ] + (operation ,))
4345
44- def _exclude_last (self ) -> "MagicFilter" :
46+ def _exclude_last (self : MagicT ) -> MagicT :
4547 return self ._new (self ._operations [:- 1 ])
4648
4749 def _resolve (self , value : Any , operations : Optional [Tuple [BaseOperation , ...]] = None ) -> Any :
@@ -69,36 +71,36 @@ def _resolve(self, value: Any, operations: Optional[Tuple[BaseOperation, ...]] =
6971 rejected = False
7072 return value
7173
72- def resolve (self , value : Any ) -> Any :
74+ def resolve (self : MagicT , value : Any ) -> Any :
7375 return self ._resolve (value = value )
7476
75- def __getattr__ (self , item : Any ) -> "MagicFilter" :
77+ def __getattr__ (self : MagicT , item : Any ) -> MagicT :
7678 return self ._extend (GetAttributeOperation (name = item ))
7779
7880 attr_ = __getattr__
7981
80- def __getitem__ (self , item : Any ) -> "MagicFilter" :
82+ def __getitem__ (self : MagicT , item : Any ) -> MagicT :
8183 return self ._extend (GetItemOperation (key = item ))
8284
83- def __eq__ (self , other : Any ) -> "MagicFilter" : # type: ignore
85+ def __eq__ (self : MagicT , other : Any ) -> MagicT : # type: ignore
8486 return self ._extend (ComparatorOperation (right = other , comparator = operator .eq ))
8587
86- def __ne__ (self , other : Any ) -> "MagicFilter" : # type: ignore
88+ def __ne__ (self : MagicT , other : Any ) -> MagicT : # type: ignore
8789 return self ._extend (ComparatorOperation (right = other , comparator = operator .ne ))
8890
89- def __lt__ (self , other : Any ) -> "MagicFilter" :
91+ def __lt__ (self : MagicT , other : Any ) -> MagicT :
9092 return self ._extend (ComparatorOperation (right = other , comparator = operator .lt ))
9193
92- def __gt__ (self , other : Any ) -> "MagicFilter" :
94+ def __gt__ (self : MagicT , other : Any ) -> MagicT :
9395 return self ._extend (ComparatorOperation (right = other , comparator = operator .gt ))
9496
95- def __le__ (self , other : Any ) -> "MagicFilter" :
97+ def __le__ (self : MagicT , other : Any ) -> MagicT :
9698 return self ._extend (ComparatorOperation (right = other , comparator = operator .le ))
9799
98- def __ge__ (self , other : Any ) -> "MagicFilter" :
100+ def __ge__ (self : MagicT , other : Any ) -> MagicT :
99101 return self ._extend (ComparatorOperation (right = other , comparator = operator .ge ))
100102
101- def __invert__ (self ) -> "MagicFilter" :
103+ def __invert__ (self : MagicT ) -> MagicT :
102104 if (
103105 self ._operations
104106 and isinstance (self ._operations [- 1 ], ImportantFunctionOperation )
@@ -107,116 +109,116 @@ def __invert__(self) -> "MagicFilter":
107109 return self ._exclude_last ()
108110 return self ._extend (ImportantFunctionOperation (function = operator .not_ ))
109111
110- def __call__ (self , * args : Any , ** kwargs : Any ) -> "MagicFilter" :
112+ def __call__ (self : MagicT , * args : Any , ** kwargs : Any ) -> MagicT :
111113 return self ._extend (CallOperation (args = args , kwargs = kwargs ))
112114
113- def __and__ (self , other : Any ) -> "MagicFilter" :
115+ def __and__ (self : MagicT , other : Any ) -> MagicT :
114116 if isinstance (other , MagicFilter ):
115117 return self ._extend (CombinationOperation .and_op (right = other ))
116118 return self ._extend (CombinationOperation (right = other , combinator = operator .and_ ))
117119
118- def __rand__ (self , other : Any ) -> "MagicFilter" :
120+ def __rand__ (self : MagicT , other : Any ) -> MagicT :
119121 return self ._extend (RCombinationOperation (left = other , combinator = operator .and_ ))
120122
121- def __or__ (self , other : Any ) -> "MagicFilter" :
123+ def __or__ (self : MagicT , other : Any ) -> MagicT :
122124 if isinstance (other , MagicFilter ):
123125 return self ._extend (CombinationOperation .or_op (right = other ))
124126 return self ._extend (CombinationOperation (right = other , combinator = operator .or_ ))
125127
126- def __ror__ (self , other : Any ) -> "MagicFilter" :
128+ def __ror__ (self : MagicT , other : Any ) -> MagicT :
127129 return self ._extend (RCombinationOperation (left = other , combinator = operator .or_ ))
128130
129- def __xor__ (self , other : Any ) -> "MagicFilter" :
131+ def __xor__ (self : MagicT , other : Any ) -> MagicT :
130132 return self ._extend (CombinationOperation (right = other , combinator = operator .xor ))
131133
132- def __rxor__ (self , other : Any ) -> "MagicFilter" :
134+ def __rxor__ (self : MagicT , other : Any ) -> MagicT :
133135 return self ._extend (RCombinationOperation (left = other , combinator = operator .xor ))
134136
135- def __rshift__ (self , other : Any ) -> "MagicFilter" :
137+ def __rshift__ (self : MagicT , other : Any ) -> MagicT :
136138 return self ._extend (CombinationOperation (right = other , combinator = operator .rshift ))
137139
138- def __rrshift__ (self , other : Any ) -> "MagicFilter" :
140+ def __rrshift__ (self : MagicT , other : Any ) -> MagicT :
139141 return self ._extend (RCombinationOperation (left = other , combinator = operator .rshift ))
140142
141- def __lshift__ (self , other : Any ) -> "MagicFilter" :
143+ def __lshift__ (self : MagicT , other : Any ) -> MagicT :
142144 return self ._extend (CombinationOperation (right = other , combinator = operator .lshift ))
143145
144- def __rlshift__ (self , other : Any ) -> "MagicFilter" :
146+ def __rlshift__ (self : MagicT , other : Any ) -> MagicT :
145147 return self ._extend (RCombinationOperation (left = other , combinator = operator .lshift ))
146148
147- def __add__ (self , other : Any ) -> "MagicFilter" :
149+ def __add__ (self : MagicT , other : Any ) -> MagicT :
148150 return self ._extend (CombinationOperation (right = other , combinator = operator .add ))
149151
150- def __radd__ (self , other : Any ) -> "MagicFilter" :
152+ def __radd__ (self : MagicT , other : Any ) -> MagicT :
151153 return self ._extend (RCombinationOperation (left = other , combinator = operator .add ))
152154
153- def __sub__ (self , other : Any ) -> "MagicFilter" :
155+ def __sub__ (self : MagicT , other : Any ) -> MagicT :
154156 return self ._extend (CombinationOperation (right = other , combinator = operator .sub ))
155157
156- def __rsub__ (self , other : Any ) -> "MagicFilter" :
158+ def __rsub__ (self : MagicT , other : Any ) -> MagicT :
157159 return self ._extend (RCombinationOperation (left = other , combinator = operator .sub ))
158160
159- def __mul__ (self , other : Any ) -> "MagicFilter" :
161+ def __mul__ (self : MagicT , other : Any ) -> MagicT :
160162 return self ._extend (CombinationOperation (right = other , combinator = operator .mul ))
161163
162- def __rmul__ (self , other : Any ) -> "MagicFilter" :
164+ def __rmul__ (self : MagicT , other : Any ) -> MagicT :
163165 return self ._extend (RCombinationOperation (left = other , combinator = operator .mul ))
164166
165- def __truediv__ (self , other : Any ) -> "MagicFilter" :
167+ def __truediv__ (self : MagicT , other : Any ) -> MagicT :
166168 return self ._extend (CombinationOperation (right = other , combinator = operator .truediv ))
167169
168- def __rtruediv__ (self , other : Any ) -> "MagicFilter" :
170+ def __rtruediv__ (self : MagicT , other : Any ) -> MagicT :
169171 return self ._extend (RCombinationOperation (left = other , combinator = operator .truediv ))
170172
171- def __floordiv__ (self , other : Any ) -> "MagicFilter" :
173+ def __floordiv__ (self : MagicT , other : Any ) -> MagicT :
172174 return self ._extend (CombinationOperation (right = other , combinator = operator .floordiv ))
173175
174- def __rfloordiv__ (self , other : Any ) -> "MagicFilter" :
176+ def __rfloordiv__ (self : MagicT , other : Any ) -> MagicT :
175177 return self ._extend (RCombinationOperation (left = other , combinator = operator .floordiv ))
176178
177- def __mod__ (self , other : Any ) -> "MagicFilter" :
179+ def __mod__ (self : MagicT , other : Any ) -> MagicT :
178180 return self ._extend (CombinationOperation (right = other , combinator = operator .mod ))
179181
180- def __rmod__ (self , other : Any ) -> "MagicFilter" :
182+ def __rmod__ (self : MagicT , other : Any ) -> MagicT :
181183 return self ._extend (RCombinationOperation (left = other , combinator = operator .mod ))
182184
183- def __matmul__ (self , other : Any ) -> "MagicFilter" :
185+ def __matmul__ (self : MagicT , other : Any ) -> MagicT :
184186 return self ._extend (CombinationOperation (right = other , combinator = operator .matmul ))
185187
186- def __rmatmul__ (self , other : Any ) -> "MagicFilter" :
188+ def __rmatmul__ (self : MagicT , other : Any ) -> MagicT :
187189 return self ._extend (RCombinationOperation (left = other , combinator = operator .matmul ))
188190
189- def __pow__ (self , other : Any ) -> "MagicFilter" :
191+ def __pow__ (self : MagicT , other : Any ) -> MagicT :
190192 return self ._extend (CombinationOperation (right = other , combinator = operator .pow ))
191193
192- def __rpow__ (self , other : Any ) -> "MagicFilter" :
194+ def __rpow__ (self : MagicT , other : Any ) -> MagicT :
193195 return self ._extend (RCombinationOperation (left = other , combinator = operator .pow ))
194196
195- def __pos__ (self ) -> "MagicFilter" :
197+ def __pos__ (self : MagicT ) -> MagicT :
196198 return self ._extend (FunctionOperation (function = operator .pos ))
197199
198- def __neg__ (self ) -> "MagicFilter" :
200+ def __neg__ (self : MagicT ) -> MagicT :
199201 return self ._extend (FunctionOperation (function = operator .neg ))
200202
201- def is_ (self , value : Any ) -> "MagicFilter" :
203+ def is_ (self : MagicT , value : Any ) -> MagicT :
202204 return self ._extend (CombinationOperation (right = value , combinator = operator .is_ ))
203205
204- def is_not (self , value : Any ) -> "MagicFilter" :
206+ def is_not (self : MagicT , value : Any ) -> MagicT :
205207 return self ._extend (CombinationOperation (right = value , combinator = operator .is_not ))
206208
207- def in_ (self , iterable : Sequence [Any ]) -> "MagicFilter" :
209+ def in_ (self : MagicT , iterable : Sequence [Any ]) -> MagicT :
208210 return self ._extend (FunctionOperation .in_op (iterable ))
209211
210- def contains (self , value : Any ) -> "MagicFilter" :
212+ def contains (self : MagicT , value : Any ) -> MagicT :
211213 return self ._extend (FunctionOperation .contains_op (value ))
212214
213- def len (self ) -> "MagicFilter" :
215+ def len (self : MagicT ) -> MagicT :
214216 return self ._extend (FunctionOperation (len ))
215217
216- def regexp (self , pattern : Union [str , Pattern [str ]]) -> "MagicFilter" :
218+ def regexp (self : MagicT , pattern : Union [str , Pattern [str ]]) -> MagicT :
217219 if isinstance (pattern , str ):
218220 pattern = re .compile (pattern )
219221 return self ._extend (FunctionOperation (pattern .match ))
220222
221- def func (self , func : Callable [[Any ], Any ]) -> "MagicFilter" :
223+ def func (self : MagicT , func : Callable [[Any ], Any ]) -> MagicT :
222224 return self ._extend (FunctionOperation (func ))
0 commit comments