Skip to content

Commit 2e85a70

Browse files
authored
psycopg2: make Range generic (#11435)
And some other small fixes
1 parent 498ab71 commit 2e85a70

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
psycopg2.pool.AbstractConnectionPool.(closeall|getconn|putconn)
2-
psycopg2.(extras|_range).RangeAdapter.name
32
psycopg2.(_psycopg|extensions).connection.async # async is a reserved keyword

stubs/psycopg2/psycopg2/_range.pyi

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
from _typeshed import Incomplete
2-
from typing import Any, overload
1+
import datetime as dt
2+
from _typeshed import SupportsAllComparisons
3+
from typing import Any, Generic, TypeVar, overload
34
from typing_extensions import Self
45

5-
from psycopg2._psycopg import connection, cursor
6+
from psycopg2._psycopg import _type, connection, cursor
67

7-
class Range:
8+
_T_co = TypeVar("_T_co", covariant=True)
9+
10+
class Range(Generic[_T_co]):
811
def __init__(
9-
self, lower: Incomplete | None = None, upper: Incomplete | None = None, bounds: str = "[)", empty: bool = False
12+
self, lower: _T_co | None = None, upper: _T_co | None = None, bounds: str = "[)", empty: bool = False
1013
) -> None: ...
1114
@property
12-
def lower(self): ...
15+
def lower(self) -> _T_co | None: ...
1316
@property
14-
def upper(self): ...
17+
def upper(self) -> _T_co | None: ...
1518
@property
1619
def isempty(self) -> bool: ...
1720
@property
@@ -22,51 +25,53 @@ class Range:
2225
def lower_inc(self) -> bool: ...
2326
@property
2427
def upper_inc(self) -> bool: ...
25-
def __contains__(self, x) -> bool: ...
28+
def __contains__(self, x: SupportsAllComparisons) -> bool: ...
2629
def __bool__(self) -> bool: ...
2730
def __eq__(self, other: object) -> bool: ...
2831
def __ne__(self, other: object) -> bool: ...
2932
def __hash__(self) -> int: ...
30-
def __lt__(self, other: Range) -> bool: ...
31-
def __le__(self, other: Range) -> bool: ...
32-
def __gt__(self, other: Range) -> bool: ...
33-
def __ge__(self, other: Range) -> bool: ...
33+
def __lt__(self, other: Range[_T_co]) -> bool: ...
34+
def __le__(self, other: Range[_T_co]) -> bool: ...
35+
def __gt__(self, other: Range[_T_co]) -> bool: ...
36+
def __ge__(self, other: Range[_T_co]) -> bool: ...
3437

3538
def register_range(
36-
pgrange: str, pyrange: str | type[Range], conn_or_curs: connection | cursor, globally: bool = False
39+
pgrange: str, pyrange: str | type[Range[Any]], conn_or_curs: connection | cursor, globally: bool = False
3740
) -> RangeCaster: ...
3841

3942
class RangeAdapter:
40-
name: str # this is None here but MUST be str in subclasses
41-
adapted: Range
42-
def __init__(self, adapted: Range) -> None: ...
43+
name: str | None = None
44+
adapted: Range[Any]
45+
def __init__(self, adapted: Range[Any]) -> None: ...
4346
def __conform__(self, proto) -> Self | None: ...
4447
def prepare(self, conn: connection | None) -> None: ...
4548
def getquoted(self) -> bytes: ...
4649

4750
class RangeCaster:
4851
adapter: type[RangeAdapter]
49-
range: type[Range]
50-
subtype_oid: Any
51-
typecaster: Any
52-
array_typecaster: Any
52+
range: type[Range[Any]]
53+
subtype_oid: int
54+
typecaster: _type
55+
array_typecaster: _type | None
5356
def __init__(
5457
self,
5558
pgrange: str | type[RangeAdapter],
56-
pyrange: str | type[Range],
59+
pyrange: str | type[Range[Any]],
5760
oid: int,
5861
subtype_oid: int,
5962
array_oid: int | None = None,
6063
) -> None: ...
6164
@overload
6265
def parse(self, s: None, cur: cursor | None = None) -> None: ...
6366
@overload
64-
def parse(self, s: str, cur: cursor | None = None) -> Range: ...
67+
def parse(self, s: str, cur: cursor | None = None) -> Range[Any]: ...
68+
@overload
69+
def parse(self, s: str | None, cur: cursor | None = None) -> Range[Any] | None: ...
6570

66-
class NumericRange(Range): ...
67-
class DateRange(Range): ...
68-
class DateTimeRange(Range): ...
69-
class DateTimeTZRange(Range): ...
71+
class NumericRange(Range[float]): ...
72+
class DateRange(Range[dt.date]): ...
73+
class DateTimeRange(Range[dt.datetime]): ...
74+
class DateTimeTZRange(Range[dt.datetime]): ...
7075

7176
class NumberRangeAdapter(RangeAdapter):
7277
def getquoted(self) -> bytes: ...

0 commit comments

Comments
 (0)