4
4
# This module is part of asyncpg and is released under
5
5
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
6
6
7
+ from __future__ import annotations
7
8
8
- import builtins
9
- import sys
10
9
import typing
11
10
12
- if sys .version_info >= (3 , 8 ):
13
- from typing import Literal , SupportsIndex
14
- else :
15
- from typing_extensions import Literal , SupportsIndex
11
+ if typing .TYPE_CHECKING :
12
+ import builtins
13
+ import sys
14
+
15
+ if sys .version_info < (3 , 11 ):
16
+ from typing_extensions import Self
17
+ else :
18
+ from typing import Self
16
19
17
20
18
21
__all__ = (
19
22
'BitString' , 'Point' , 'Path' , 'Polygon' ,
20
23
'Box' , 'Line' , 'LineSegment' , 'Circle' ,
21
24
)
22
25
23
- _BitString = typing .TypeVar ('_BitString' , bound = 'BitString' )
24
- _BitOrderType = Literal ['big' , 'little' ]
26
+ _BitOrderType = typing .Literal ['big' , 'little' ]
25
27
26
28
27
29
class BitString :
28
30
"""Immutable representation of PostgreSQL `bit` and `varbit` types."""
29
31
30
32
__slots__ = '_bytes' , '_bitlength'
31
33
34
+ _bytes : bytes
35
+ _bitlength : int
36
+
32
37
def __init__ (self ,
33
- bitstring : typing . Optional [ builtins .bytes ] = None ) -> None :
38
+ bitstring : builtins .bytes | None = None ) -> None :
34
39
if not bitstring :
35
40
self ._bytes = bytes ()
36
41
self ._bitlength = 0
@@ -67,9 +72,9 @@ def __init__(self,
67
72
self ._bitlength = bitlen
68
73
69
74
@classmethod
70
- def frombytes (cls : typing . Type [ _BitString ] ,
71
- bytes_ : typing . Optional [ builtins .bytes ] = None ,
72
- bitlength : typing . Optional [ int ] = None ) -> _BitString :
75
+ def frombytes (cls ,
76
+ bytes_ : builtins .bytes | None = None ,
77
+ bitlength : int | None = None ) -> Self :
73
78
if bitlength is None :
74
79
if bytes_ is None :
75
80
bytes_ = bytes ()
@@ -151,9 +156,9 @@ def to_int(self, bitorder: _BitOrderType = 'big',
151
156
return x
152
157
153
158
@classmethod
154
- def from_int (cls : typing . Type [ _BitString ] , x : int , length : int ,
159
+ def from_int (cls , x : int , length : int ,
155
160
bitorder : _BitOrderType = 'big' , * , signed : bool = False ) \
156
- -> _BitString :
161
+ -> Self :
157
162
"""Represent the Python int x as a BitString.
158
163
Acts similarly to int.to_bytes.
159
164
@@ -243,17 +248,23 @@ class Point(typing.Tuple[float, float]):
243
248
244
249
__slots__ = ()
245
250
246
- def __new__ (cls ,
247
- x : typing .Union [typing .SupportsFloat ,
248
- SupportsIndex ,
249
- typing .Text ,
250
- builtins .bytes ,
251
- builtins .bytearray ],
252
- y : typing .Union [typing .SupportsFloat ,
253
- SupportsIndex ,
254
- typing .Text ,
255
- builtins .bytes ,
256
- builtins .bytearray ]) -> 'Point' :
251
+ def __new__ (
252
+ cls ,
253
+ x : (
254
+ typing .SupportsFloat |
255
+ typing .SupportsIndex |
256
+ typing .Text |
257
+ builtins .bytes |
258
+ builtins .bytearray
259
+ ),
260
+ y : (
261
+ typing .SupportsFloat |
262
+ typing .SupportsIndex |
263
+ typing .Text |
264
+ builtins .bytes |
265
+ builtins .bytearray
266
+ )
267
+ ) -> Self :
257
268
return super ().__new__ (cls ,
258
269
typing .cast (typing .Any , (float (x ), float (y ))))
259
270
@@ -279,7 +290,7 @@ class Box(typing.Tuple[Point, Point]):
279
290
__slots__ = ()
280
291
281
292
def __new__ (cls , high : typing .Sequence [float ],
282
- low : typing .Sequence [float ]) -> 'Box' :
293
+ low : typing .Sequence [float ]) -> Self :
283
294
return super ().__new__ (cls ,
284
295
typing .cast (typing .Any , (Point (* high ),
285
296
Point (* low ))))
@@ -305,7 +316,7 @@ class Line(typing.Tuple[float, float, float]):
305
316
306
317
__slots__ = ()
307
318
308
- def __new__ (cls , A : float , B : float , C : float ) -> 'Line' :
319
+ def __new__ (cls , A : float , B : float , C : float ) -> Self :
309
320
return super ().__new__ (cls , typing .cast (typing .Any , (A , B , C )))
310
321
311
322
@property
@@ -327,7 +338,7 @@ class LineSegment(typing.Tuple[Point, Point]):
327
338
__slots__ = ()
328
339
329
340
def __new__ (cls , p1 : typing .Sequence [float ],
330
- p2 : typing .Sequence [float ]) -> 'LineSegment' :
341
+ p2 : typing .Sequence [float ]) -> Self :
331
342
return super ().__new__ (cls ,
332
343
typing .cast (typing .Any , (Point (* p1 ),
333
344
Point (* p2 ))))
@@ -388,8 +399,9 @@ def __getitem__(self, i: int) -> Point:
388
399
def __getitem__ (self , i : slice ) -> typing .Tuple [Point , ...]:
389
400
...
390
401
391
- def __getitem__ (self , i : typing .Union [int , slice ]) \
392
- -> typing .Union [Point , typing .Tuple [Point , ...]]:
402
+ def __getitem__ (
403
+ self , i : int | slice
404
+ ) -> Point | typing .Tuple [Point , ...]:
393
405
return self .points [i ]
394
406
395
407
def __contains__ (self , point : object ) -> bool :
@@ -411,7 +423,7 @@ class Circle(typing.Tuple[Point, float]):
411
423
412
424
__slots__ = ()
413
425
414
- def __new__ (cls , center : Point , radius : float ) -> 'Circle' :
426
+ def __new__ (cls , center : Point , radius : float ) -> Self :
415
427
return super ().__new__ (cls , typing .cast (typing .Any , (center , radius )))
416
428
417
429
@property
0 commit comments