Skip to content

Commit 3cad045

Browse files
authored
Improve typing (#25)
1 parent ea27e3f commit 3cad045

File tree

2 files changed

+50
-31
lines changed

2 files changed

+50
-31
lines changed

pgproto.pyi

+7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import uuid
44

55
class CodecContext:
66
def get_text_codec(self) -> codecs.CodecInfo: ...
7+
def get_json_decoder(self) -> object: ...
8+
def get_json_encoder(self) -> object: ...
79

10+
@typing.final
811
class ReadBuffer: ...
12+
13+
@typing.final
914
class WriteBuffer: ...
15+
1016
class BufferError(Exception): ...
1117

18+
@typing.final
1219
class UUID(uuid.UUID):
1320
def __init__(self, inp: typing.AnyStr) -> None: ...

types.py

+43-31
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,38 @@
44
# This module is part of asyncpg and is released under
55
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
66

7+
from __future__ import annotations
78

8-
import builtins
9-
import sys
109
import typing
1110

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
1619

1720

1821
__all__ = (
1922
'BitString', 'Point', 'Path', 'Polygon',
2023
'Box', 'Line', 'LineSegment', 'Circle',
2124
)
2225

23-
_BitString = typing.TypeVar('_BitString', bound='BitString')
24-
_BitOrderType = Literal['big', 'little']
26+
_BitOrderType = typing.Literal['big', 'little']
2527

2628

2729
class BitString:
2830
"""Immutable representation of PostgreSQL `bit` and `varbit` types."""
2931

3032
__slots__ = '_bytes', '_bitlength'
3133

34+
_bytes: bytes
35+
_bitlength: int
36+
3237
def __init__(self,
33-
bitstring: typing.Optional[builtins.bytes] = None) -> None:
38+
bitstring: builtins.bytes | None = None) -> None:
3439
if not bitstring:
3540
self._bytes = bytes()
3641
self._bitlength = 0
@@ -67,9 +72,9 @@ def __init__(self,
6772
self._bitlength = bitlen
6873

6974
@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:
7378
if bitlength is None:
7479
if bytes_ is None:
7580
bytes_ = bytes()
@@ -151,9 +156,9 @@ def to_int(self, bitorder: _BitOrderType = 'big',
151156
return x
152157

153158
@classmethod
154-
def from_int(cls: typing.Type[_BitString], x: int, length: int,
159+
def from_int(cls, x: int, length: int,
155160
bitorder: _BitOrderType = 'big', *, signed: bool = False) \
156-
-> _BitString:
161+
-> Self:
157162
"""Represent the Python int x as a BitString.
158163
Acts similarly to int.to_bytes.
159164
@@ -243,17 +248,23 @@ class Point(typing.Tuple[float, float]):
243248

244249
__slots__ = ()
245250

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:
257268
return super().__new__(cls,
258269
typing.cast(typing.Any, (float(x), float(y))))
259270

@@ -279,7 +290,7 @@ class Box(typing.Tuple[Point, Point]):
279290
__slots__ = ()
280291

281292
def __new__(cls, high: typing.Sequence[float],
282-
low: typing.Sequence[float]) -> 'Box':
293+
low: typing.Sequence[float]) -> Self:
283294
return super().__new__(cls,
284295
typing.cast(typing.Any, (Point(*high),
285296
Point(*low))))
@@ -305,7 +316,7 @@ class Line(typing.Tuple[float, float, float]):
305316

306317
__slots__ = ()
307318

308-
def __new__(cls, A: float, B: float, C: float) -> 'Line':
319+
def __new__(cls, A: float, B: float, C: float) -> Self:
309320
return super().__new__(cls, typing.cast(typing.Any, (A, B, C)))
310321

311322
@property
@@ -327,7 +338,7 @@ class LineSegment(typing.Tuple[Point, Point]):
327338
__slots__ = ()
328339

329340
def __new__(cls, p1: typing.Sequence[float],
330-
p2: typing.Sequence[float]) -> 'LineSegment':
341+
p2: typing.Sequence[float]) -> Self:
331342
return super().__new__(cls,
332343
typing.cast(typing.Any, (Point(*p1),
333344
Point(*p2))))
@@ -388,8 +399,9 @@ def __getitem__(self, i: int) -> Point:
388399
def __getitem__(self, i: slice) -> typing.Tuple[Point, ...]:
389400
...
390401

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, ...]:
393405
return self.points[i]
394406

395407
def __contains__(self, point: object) -> bool:
@@ -411,7 +423,7 @@ class Circle(typing.Tuple[Point, float]):
411423

412424
__slots__ = ()
413425

414-
def __new__(cls, center: Point, radius: float) -> 'Circle':
426+
def __new__(cls, center: Point, radius: float) -> Self:
415427
return super().__new__(cls, typing.cast(typing.Any, (center, radius)))
416428

417429
@property

0 commit comments

Comments
 (0)