Skip to content

Commit 8ac22c1

Browse files
committed
Typing improvements and updates for code in master
1 parent f091228 commit 8ac22c1

File tree

9 files changed

+46
-33
lines changed

9 files changed

+46
-33
lines changed

asyncpg/cluster.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class _ConnectionSpec(typing_extensions.TypedDict):
3636
port: str
3737

3838

39-
_system = platform.uname().system
39+
_system: typing_extensions.Final = platform.uname().system
4040

4141
if _system == 'Windows':
4242
def platform_exe(name: str) -> str:

asyncpg/compat.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
import platform
1111
import sys
1212
import typing
13+
import typing_extensions
1314

1415

1516
_T = typing.TypeVar('_T')
16-
PY_36 = sys.version_info >= (3, 6)
17-
PY_37 = sys.version_info >= (3, 7)
18-
SYSTEM = platform.uname().system
17+
PY_36: typing_extensions.Final = sys.version_info >= (3, 6)
18+
PY_37: typing_extensions.Final = sys.version_info >= (3, 7)
19+
SYSTEM: typing_extensions.Final = platform.uname().system
1920

2021

2122
if SYSTEM == 'Windows':

asyncpg/connect_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class _ClientConfiguration(typing.NamedTuple):
5656
max_cacheable_statement_size: int
5757

5858

59-
_system = platform.uname().system
59+
_system: typing_extensions.Final = platform.uname().system
6060

6161

6262
if _system == 'Windows':

asyncpg/connection.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ def get_settings(self) -> '_cprotocol.ConnectionSettings':
320320
return self._protocol.get_settings()
321321

322322
def transaction(self, *,
323-
isolation: transaction.IsolationLevels = 'read_committed',
323+
isolation: typing.Optional[
324+
transaction.IsolationLevels
325+
] = None,
324326
readonly: bool = False,
325327
deferrable: bool = False) -> transaction.Transaction:
326328
"""Create a :class:`~transaction.Transaction` object.
@@ -1749,7 +1751,7 @@ def _maybe_gc_stmt(
17491751
if (
17501752
stmt.refs == 0
17511753
and not self._stmt_cache.has(
1752-
(stmt.query, stmt.record_class, bool(stmt.ignore_custom_codec))
1754+
(stmt.query, stmt.record_class, stmt.ignore_custom_codec)
17531755
)
17541756
):
17551757
# If low-level `stmt` isn't referenced from any high-level
@@ -2736,14 +2738,17 @@ async def connect(dsn: typing.Optional[str] = None, *,
27362738
)
27372739

27382740

2741+
_StatementCacheKey = typing.Tuple[str, typing.Type[_Record], bool]
2742+
2743+
27392744
class _StatementCacheEntry:
27402745

27412746
__slots__ = ('_query', '_statement', '_cache', '_cleanup_cb')
27422747

27432748
def __init__(
27442749
self,
27452750
cache: '_StatementCache',
2746-
query: typing.Tuple[str, typing.Type[_Record], bool],
2751+
query: _StatementCacheKey[_Record],
27472752
statement: '_cprotocol.PreparedStatementState[_Record]'
27482753
) -> None:
27492754
self._cache = cache
@@ -2783,7 +2788,7 @@ def __init__(self, *, loop: asyncio.AbstractEventLoop,
27832788
# entries dict, whereas the unused one will group in the
27842789
# beginning of it.
27852790
self._entries: collections.OrderedDict[
2786-
typing.Tuple[str, typing.Type['_cprotocol.Record'], bool],
2791+
_StatementCacheKey['_cprotocol.Record'],
27872792
_StatementCacheEntry
27882793
] = collections.OrderedDict()
27892794

@@ -2811,7 +2816,7 @@ def set_max_lifetime(self, new_lifetime: float) -> None:
28112816

28122817
def get(
28132818
self,
2814-
query: typing.Tuple[str, typing.Type[_Record], bool],
2819+
query: _StatementCacheKey[_Record],
28152820
*,
28162821
promote: bool = True
28172822
) -> typing.Optional['_cprotocol.PreparedStatementState[_Record]']:
@@ -2837,12 +2842,12 @@ def get(
28372842

28382843
return entry._statement
28392844

2840-
def has(self, query: typing.Tuple[str, typing.Type[_Record], bool]) -> bool:
2845+
def has(self, query: _StatementCacheKey[_Record]) -> bool:
28412846
return self.get(query, promote=False) is not None
28422847

28432848
def put(
28442849
self,
2845-
query: typing.Tuple[str, typing.Type[_Record], bool],
2850+
query: _StatementCacheKey[_Record],
28462851
statement: '_cprotocol.PreparedStatementState[_Record]'
28472852
) -> None:
28482853
if not self._max_size:
@@ -2884,7 +2889,7 @@ def _set_entry_timeout(self, entry: _StatementCacheEntry) -> None:
28842889

28852890
def _new_entry(
28862891
self,
2887-
query: typing.Tuple[str, typing.Type[_Record], bool],
2892+
query: _StatementCacheKey[_Record],
28882893
statement: '_cprotocol.PreparedStatementState[_Record]'
28892894
) -> _StatementCacheEntry:
28902895
entry = _StatementCacheEntry(self, query, statement)

asyncpg/cursor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __await__(self) -> typing.Generator[
109109
self._query,
110110
self._state,
111111
self._args,
112-
self._record_class
112+
self._record_class,
113113
)
114114
return cursor._init(self._timeout).__await__()
115115

asyncpg/introspection.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77

88
import typing
9+
import typing_extensions
910

1011
if typing.TYPE_CHECKING:
1112
from . import protocol
1213

1314

14-
_TYPEINFO = '''\
15+
_TYPEINFO: typing_extensions.Final = '''\
1516
(
1617
SELECT
1718
t.oid AS oid,
@@ -102,7 +103,7 @@
102103
'''
103104

104105

105-
INTRO_LOOKUP_TYPES = '''\
106+
INTRO_LOOKUP_TYPES: typing_extensions.Final = '''\
106107
WITH RECURSIVE typeinfo_tree(
107108
oid, ns, name, kind, basetype, has_bin_io, elemtype, elemdelim,
108109
range_subtype, elem_has_bin_io, attrtypoids, attrnames, depth)
@@ -140,7 +141,7 @@
140141
'''.format(typeinfo=_TYPEINFO)
141142

142143

143-
TYPE_BY_NAME = '''\
144+
TYPE_BY_NAME: typing_extensions.Final = '''\
144145
SELECT
145146
t.oid,
146147
t.typelem AS elemtype,
@@ -153,7 +154,7 @@
153154
'''
154155

155156

156-
TYPE_BY_OID = '''\
157+
TYPE_BY_OID: typing_extensions.Final = '''\
157158
SELECT
158159
t.oid,
159160
t.typelem AS elemtype,
@@ -166,7 +167,7 @@
166167

167168

168169
# 'b' for a base type, 'd' for a domain, 'e' for enum.
169-
SCALAR_TYPE_KINDS = (b'b', b'd', b'e')
170+
SCALAR_TYPE_KINDS: typing_extensions.Final = (b'b', b'd', b'e')
170171

171172

172173
def is_scalar_type(typeinfo: 'protocol.Record') -> bool:

asyncpg/protocol/protocol.pyi

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ from typing import (
1818
Union,
1919
overload,
2020
)
21-
from typing_extensions import Protocol as _TEProtocol, Literal
21+
from typing_extensions import Protocol as _TEProtocol, Literal, Final
2222

2323
import asyncpg.pgproto.pgproto
2424

@@ -33,9 +33,9 @@ _OtherRecord = TypeVar('_OtherRecord', bound=Record)
3333
_PreparedStatementState = TypeVar('_PreparedStatementState',
3434
bound=PreparedStatementState[Any])
3535

36-
BUILTIN_TYPE_NAME_MAP: Dict[str, int]
37-
BUILTIN_TYPE_OID_MAP: Dict[int, str]
38-
NO_TIMEOUT: _NoTimeoutType
36+
BUILTIN_TYPE_NAME_MAP: Final[Dict[str, int]]
37+
BUILTIN_TYPE_OID_MAP: Final[Dict[int, str]]
38+
NO_TIMEOUT: Final[_NoTimeoutType]
3939

4040
def hashlib_md5(*args: Any, **kwargs: Any) -> Any: ...
4141

@@ -186,7 +186,7 @@ class ConnectionSettings(asyncpg.pgproto.pgproto.CodecContext):
186186
format: Any,
187187
) -> Any: ...
188188
def clear_type_cache(self) -> None: ...
189-
def get_data_codec(self, oid: int, format: Any = ...) -> Any: ...
189+
def get_data_codec(self, oid: int, format: Any = ..., ignore_custom_codec: bool = ...) -> Any: ...
190190
def get_text_codec(self) -> CodecInfo: ...
191191
def register_data_types(self, types: Iterable[Any]) -> None: ...
192192
def remove_python_codec(
@@ -247,15 +247,15 @@ class PreparedStatementState(Generic[_Record]):
247247
query: str = ...
248248
refs: int = ...
249249
record_class: _TypingType[_Record] = ...
250-
ignore_custom_codec: int = ...
250+
ignore_custom_codec: bool = ...
251251
__pyx_vtable__: Any = ...
252252
def __init__(
253253
self,
254254
name: str,
255255
query: str,
256256
protocol: BaseProtocol[Any],
257257
record_class: _TypingType[_Record],
258-
ignore_custom_codec: int,
258+
ignore_custom_codec: bool,
259259
) -> None: ...
260260
def _get_parameters(self) -> Tuple[Type, ...]: ...
261261
def _get_attributes(self) -> Tuple[Attribute, ...]: ...

asyncpg/transaction.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ class TransactionState(enum.Enum):
2828
IsolationLevels = typing_extensions.Literal['read_committed',
2929
'serializable',
3030
'repeatable_read']
31-
ISOLATION_LEVELS: typing.Set[IsolationLevels] = {'read_committed',
32-
'serializable',
33-
'repeatable_read'}
34-
ISOLATION_LEVELS_BY_VALUE: typing.Dict[str, IsolationLevels] = {
31+
ISOLATION_LEVELS: typing_extensions.Final[
32+
typing.Set[IsolationLevels]
33+
] = {
34+
'read_committed',
35+
'serializable',
36+
'repeatable_read'
37+
}
38+
ISOLATION_LEVELS_BY_VALUE: typing_extensions.Final[
39+
typing.Dict[str, IsolationLevels]
40+
] = {
3541
'read committed': 'read_committed',
3642
'serializable': 'serializable',
3743
'repeatable read': 'repeatable_read',
@@ -50,7 +56,7 @@ class Transaction(connresource.ConnectionResource):
5056
'_state', '_nested', '_id', '_managed')
5157

5258
def __init__(self, connection: '_connection.Connection[typing.Any]',
53-
isolation: IsolationLevels,
59+
isolation: typing.Optional[IsolationLevels],
5460
readonly: bool, deferrable: bool) -> None:
5561
super().__init__(connection)
5662

@@ -249,7 +255,7 @@ def __repr__(self) -> str:
249255
attrs = []
250256
attrs.append('state:{}'.format(self._state.name.lower()))
251257

252-
attrs.append(self._isolation)
258+
attrs.append(str(self._isolation))
253259
if self._readonly:
254260
attrs.append('readonly')
255261
if self._deferrable:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'pycodestyle~=2.6.0',
3636
'flake8~=3.8.2',
3737
'uvloop~=0.14.0;platform_system!="Windows"',
38-
'mypy>=0.780'
38+
'mypy>=0.790'
3939
]
4040

4141
# Dependencies required to build documentation.

0 commit comments

Comments
 (0)