Skip to content

Commit 5334c21

Browse files
committed
Update flake8 (newer pyflakes handles overloads) and update variables
1 parent 444a6fc commit 5334c21

File tree

10 files changed

+82
-74
lines changed

10 files changed

+82
-74
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[flake8]
22
ignore = E402,E731,W504,E252
3-
exclude = .git,__pycache__,build,dist,.eggs,.github,.local
3+
exclude = .git,__pycache__,build,dist,.eggs,.github,.local,.venv*
44
per-file-ignores = *.pyi: F401, F403, F405, F811, E127, E128, E203, E266, E301, E302, E305, E501, E701, E704, E741, B303, W503, W504

asyncpg/compat.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,31 @@
1515
import typing_extensions
1616

1717

18-
T_co = typing.TypeVar('T_co', covariant=True)
19-
F = typing.TypeVar('F', bound=typing.Callable[..., typing.Any])
20-
F_35 = typing.TypeVar('F_35', bound=typing.Callable[
18+
_T_co = typing.TypeVar('_T_co', covariant=True)
19+
_F = typing.TypeVar('_F', bound=typing.Callable[..., typing.Any])
20+
_F_35 = typing.TypeVar('_F_35', bound=typing.Callable[
2121
...,
2222
typing.Coroutine[typing.Any, typing.Any, typing.Any]
2323
])
24+
2425
PY_36 = sys.version_info >= (3, 6)
2526
PY_37 = sys.version_info >= (3, 7)
2627
SYSTEM = platform.uname().system
2728

2829

2930
if sys.version_info < (3, 5, 2):
30-
def aiter_compat(func: F) -> F_35:
31+
def aiter_compat(func: _F) -> _F_35:
3132
@functools.wraps(func)
3233
async def wrapper(self: typing.Any) -> typing.Any:
3334
return func(self)
34-
return typing.cast(F_35, wrapper)
35+
return typing.cast(_F_35, wrapper)
3536
else:
36-
def aiter_compat(func: F) -> F: # type: ignore
37+
def aiter_compat(func: _F) -> _F: # type: ignore
3738
return func
3839

3940

40-
class PathLike(typing_extensions.Protocol[T_co]):
41-
def __fspath__(self) -> T_co:
41+
class PathLike(typing_extensions.Protocol[_T_co]):
42+
def __fspath__(self) -> _T_co:
4243
...
4344

4445

asyncpg/connect_utils.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
from . import exceptions
2828
from . import protocol
2929

30-
C = typing.TypeVar('C')
31-
P = typing.TypeVar('P', bound=asyncio.Protocol)
30+
_Connection = typing.TypeVar('_Connection')
31+
_Protocol = typing.TypeVar('_Protocol', bound=asyncio.Protocol)
3232

33-
_TPTupleType = typing.Tuple[asyncio.WriteTransport, P]
33+
_TPTupleType = typing.Tuple[asyncio.WriteTransport, _Protocol]
3434
AddrType = typing.Union[typing.Tuple[str, int], str]
3535
SSLType = typing.Union[ssl_module.SSLContext,
3636
typing_extensions.Literal[True]]
@@ -577,14 +577,15 @@ def connection_lost(self, exc: typing.Optional[Exception]) -> None:
577577
self.on_data.set_exception(exc)
578578

579579

580-
async def _create_ssl_connection(protocol_factory: typing.Callable[[], P],
580+
async def _create_ssl_connection(protocol_factory: typing.Callable[[],
581+
_Protocol],
581582
host: str, port: int, *,
582583
loop: asyncio.AbstractEventLoop,
583584
ssl_context: typing.Union[
584585
ssl_module.SSLContext,
585586
typing_extensions.Literal[True]],
586587
ssl_is_advisory: typing.Optional[bool] =
587-
False) -> _TPTupleType[P]:
588+
False) -> _TPTupleType[_Protocol]:
588589

589590
if ssl_context is True:
590591
ssl_context = ssl_module.create_default_context()
@@ -641,7 +642,7 @@ async def _create_ssl_connection(protocol_factory: typing.Callable[[], P],
641642

642643
try:
643644
return typing.cast(
644-
typing.Tuple[asyncio.WriteTransport, P],
645+
typing.Tuple[asyncio.WriteTransport, _Protocol],
645646
await conn_factory(sock=sock)
646647
)
647648
except (Exception, asyncio.CancelledError):
@@ -654,7 +655,8 @@ async def _connect_addr(*, addr: AddrType,
654655
timeout: float,
655656
params: _ConnectionParameters,
656657
config: _ClientConfiguration,
657-
connection_class: typing.Type[C]) -> C:
658+
connection_class: typing.Type[_Connection]) \
659+
-> _Connection:
658660
assert loop is not None
659661

660662
if timeout <= 0:
@@ -717,8 +719,8 @@ async def _connect_addr(*, addr: AddrType,
717719

718720
async def _connect(*, loop: typing.Optional[asyncio.AbstractEventLoop],
719721
timeout: float,
720-
connection_class: typing.Type[C],
721-
**kwargs: typing.Any) -> C:
722+
connection_class: typing.Type[_Connection],
723+
**kwargs: typing.Any) -> _Connection:
722724
if loop is None:
723725
loop = asyncio.get_event_loop()
724726

asyncpg/connection.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,24 @@
3535
from . import types
3636

3737

38-
C = typing.TypeVar('C', bound='Connection')
39-
Writer = typing.Callable[[bytes],
40-
typing.Coroutine[typing.Any, typing.Any, None]]
38+
_Connection = typing.TypeVar('_Connection', bound='Connection')
39+
_Writer = typing.Callable[[bytes],
40+
typing.Coroutine[typing.Any, typing.Any, None]]
41+
_RecordsType = typing.List['_cprotocol.Record']
42+
_RecordsExtraType = typing.Tuple[_RecordsType, bytes, bool]
43+
_AnyCallable = typing.Callable[..., typing.Any]
44+
4145
OutputType = typing.Union[typing.AnyStr,
4246
compat.PathLike[typing.AnyStr],
4347
typing.IO[typing.AnyStr],
44-
Writer]
48+
_Writer]
4549
SourceType = typing.Union[typing.AnyStr,
4650
compat.PathLike[typing.AnyStr],
4751
typing.IO[typing.AnyStr],
4852
typing.AsyncIterable[bytes]]
4953

5054
CopyFormat = typing_extensions.Literal['text', 'csv', 'binary']
5155
PasswordType = typing.Union[str, typing.Callable[[], str]]
52-
RecordsType = typing.List['_cprotocol.Record']
53-
RecordsExtraType = typing.Tuple[RecordsType, bytes, bool]
54-
AnyCallable = typing.Callable[..., typing.Any]
5556

5657

5758
class Listener(typing_extensions.Protocol):
@@ -890,7 +891,7 @@ async def _copy_out(self, copy_stmt: str,
890891
# output is not a path-like object
891892
path = None
892893

893-
writer = None # type: typing.Optional[Writer]
894+
writer = None # type: typing.Optional[_Writer]
894895
opened_by_us = False
895896
run_in_executor = self._loop.run_in_executor
896897

@@ -1458,7 +1459,7 @@ def _set_proxy(self, proxy: typing.Optional['_pool.PoolConnectionProxy']) \
14581459
self._proxy = proxy
14591460

14601461
def _check_listeners(self,
1461-
listeners: 'typing._Collection[AnyCallable]',
1462+
listeners: 'typing._Collection[_AnyCallable]',
14621463
listener_type: str) -> None:
14631464
if listeners:
14641465
count = len(listeners)
@@ -1555,24 +1556,24 @@ async def reload_schema_state(self) -> None:
15551556
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
15561557
limit: int, timeout: typing.Optional[float],
15571558
return_status: typing_extensions.Literal[True]) \
1558-
-> RecordsExtraType:
1559+
-> _RecordsExtraType:
15591560
...
15601561

1561-
@typing.overload # noqa: F811
1562+
@typing.overload
15621563
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
15631564
limit: int, timeout: typing.Optional[float],
15641565
return_status: typing_extensions.Literal[False] = ...) \
1565-
-> RecordsType:
1566+
-> _RecordsType:
15661567
...
15671568

1568-
@typing.overload # noqa: F811
1569+
@typing.overload
15691570
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
15701571
limit: int, timeout: typing.Optional[float],
15711572
return_status: bool) \
1572-
-> typing.Union[RecordsExtraType, RecordsType]:
1573+
-> typing.Union[_RecordsExtraType, _RecordsType]:
15731574
...
15741575

1575-
async def _execute(self, query: str, args: typing.Sequence[typing.Any], # noqa: F811, E501
1576+
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
15761577
limit: int, timeout: typing.Optional[float],
15771578
return_status: bool = False) -> typing.Any:
15781579
with self._stmt_exclusive_section:
@@ -1584,29 +1585,29 @@ async def _execute(self, query: str, args: typing.Sequence[typing.Any], # noqa:
15841585
async def __execute(self, query: str, args: typing.Sequence[typing.Any],
15851586
limit: int, timeout: typing.Optional[float],
15861587
return_status: typing_extensions.Literal[True]) \
1587-
-> typing.Tuple[RecordsExtraType,
1588+
-> typing.Tuple[_RecordsExtraType,
15881589
'_cprotocol.PreparedStatementState']:
15891590
...
15901591

1591-
@typing.overload # noqa: F811
1592+
@typing.overload
15921593
async def __execute(self, query: str, args: typing.Sequence[typing.Any],
15931594
limit: int, timeout: typing.Optional[float],
15941595
return_status: typing_extensions.Literal[
15951596
False] = ...) \
1596-
-> typing.Tuple[RecordsType, '_cprotocol.PreparedStatementState']:
1597+
-> typing.Tuple[_RecordsType, '_cprotocol.PreparedStatementState']:
15971598
...
15981599

1599-
@typing.overload # noqa: F811
1600+
@typing.overload
16001601
async def __execute(self, query: str, args: typing.Sequence[typing.Any],
16011602
limit: int, timeout: typing.Optional[float],
16021603
return_status: bool) \
1603-
-> typing.Union[typing.Tuple[RecordsExtraType,
1604+
-> typing.Union[typing.Tuple[_RecordsExtraType,
16041605
'_cprotocol.PreparedStatementState'],
1605-
typing.Tuple[RecordsType,
1606+
typing.Tuple[_RecordsType,
16061607
'_cprotocol.PreparedStatementState']]:
16071608
...
16081609

1609-
async def __execute(self, query: str, # noqa: F811
1610+
async def __execute(self, query: str,
16101611
args: typing.Sequence[typing.Any],
16111612
limit: int, timeout: typing.Optional[float],
16121613
return_status: bool = False) -> typing.Tuple[
@@ -1711,7 +1712,7 @@ async def connect(dsn: typing.Optional[str] = ..., *,
17111712
...
17121713

17131714

1714-
@typing.overload # noqa: F811
1715+
@typing.overload
17151716
async def connect(dsn: typing.Optional[str] = ..., *,
17161717
host: typing.Optional[connect_utils.HostType] = ...,
17171718
port: typing.Optional[connect_utils.PortType] = ...,
@@ -1726,13 +1727,13 @@ async def connect(dsn: typing.Optional[str] = ..., *,
17261727
max_cacheable_statement_size: int = ...,
17271728
command_timeout: typing.Optional[float] = ...,
17281729
ssl: typing.Optional[connect_utils.SSLType] = ...,
1729-
connection_class: typing.Type[C],
1730+
connection_class: typing.Type[_Connection],
17301731
server_settings: typing.Optional[
1731-
typing.Dict[str, str]] = ...) -> C:
1732+
typing.Dict[str, str]] = ...) -> _Connection:
17321733
...
17331734

17341735

1735-
async def connect(dsn: typing.Optional[str] = None, *, # noqa: F811
1736+
async def connect(dsn: typing.Optional[str] = None, *,
17361737
host: typing.Optional[connect_utils.HostType] = None,
17371738
port: typing.Optional[connect_utils.PortType] = None,
17381739
user: typing.Optional[str] = None,
@@ -1746,10 +1747,9 @@ async def connect(dsn: typing.Optional[str] = None, *, # noqa: F811
17461747
max_cacheable_statement_size: int = 1024 * 15,
17471748
command_timeout: typing.Optional[float] = None,
17481749
ssl: typing.Optional[connect_utils.SSLType] = None,
1749-
connection_class: typing.Type[C] = \
1750-
Connection, # type: ignore
1750+
connection_class: typing.Type[_Connection] = Connection, # type: ignore # noqa: E501
17511751
server_settings: typing.Optional[
1752-
typing.Dict[str, str]] = None) -> C:
1752+
typing.Dict[str, str]] = None) -> _Connection:
17531753
r"""A coroutine to establish a connection to a PostgreSQL server.
17541754
17551755
The connection parameters may be specified either as a connection

asyncpg/connresource.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
from . import connection as _connection
1717

1818

19-
CR = typing.TypeVar('CR', bound='ConnectionResource')
20-
F = typing.TypeVar('F', bound=typing.Callable[..., typing.Any])
19+
_ConnectionResource = typing.TypeVar('_ConnectionResource',
20+
bound='ConnectionResource')
21+
_Callable = typing.TypeVar('_Callable', bound=typing.Callable[..., typing.Any])
2122

2223

23-
def guarded(meth: F) -> F:
24+
def guarded(meth: _Callable) -> _Callable:
2425
"""A decorator to add a sanity check to ConnectionResource methods."""
2526

2627
@functools.wraps(meth)
27-
def _check(self: CR,
28+
def _check(self: _ConnectionResource,
2829
*args: typing.Any,
2930
**kwargs: typing.Any) -> typing.Any:
3031
self._check_conn_validity(meth.__name__)
3132
return meth(self, *args, **kwargs)
3233

33-
return typing.cast(F, _check)
34+
return typing.cast(_Callable, _check)
3435

3536

3637
class ConnectionResource:

asyncpg/cursor.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
from . import connection as _connection
1919

2020

21-
C = typing.TypeVar('C', bound='Cursor[typing.Any]')
22-
CI = typing.TypeVar('CI', bound='CursorIterator[typing.Any]')
23-
R = typing.TypeVar('R', bound='_cprotocol.Record')
21+
_Cursor = typing.TypeVar('_Cursor', bound='Cursor[typing.Any]')
22+
_CursorIterator = typing.TypeVar('_CursorIterator',
23+
bound='CursorIterator[typing.Any]')
24+
_Record = typing.TypeVar('_Record', bound='_cprotocol.Record')
2425

2526

2627
class CursorFactory(connresource.ConnectionResource):
@@ -72,7 +73,7 @@ def __del__(self) -> None:
7273
self._connection._maybe_gc_stmt(self._state)
7374

7475

75-
class BaseCursor(connresource.ConnectionResource, typing.Generic[R]):
76+
class BaseCursor(connresource.ConnectionResource, typing.Generic[_Record]):
7677

7778
__slots__ = ('_state', '_args', '_portal_name', '_exhausted', '_query')
7879

@@ -174,7 +175,7 @@ def __del__(self) -> None:
174175
self._connection._maybe_gc_stmt(self._state)
175176

176177

177-
class CursorIterator(BaseCursor[R]):
178+
class CursorIterator(BaseCursor[_Record]):
178179

179180
__slots__ = ('_buffer', '_prefetch', '_timeout')
180181

@@ -189,17 +190,17 @@ def __init__(self, connection: '_connection.Connection', query: str,
189190
raise exceptions.InterfaceError(
190191
'prefetch argument must be greater than zero')
191192

192-
self._buffer = collections.deque() # type: typing.Deque[R]
193+
self._buffer = collections.deque() # type: typing.Deque[_Record]
193194
self._prefetch = prefetch
194195
self._timeout = timeout
195196

196197
@compat.aiter_compat
197198
@connresource.guarded
198-
def __aiter__(self: CI) -> CI:
199+
def __aiter__(self: _CursorIterator) -> _CursorIterator:
199200
return self
200201

201202
@connresource.guarded
202-
async def __anext__(self) -> R:
203+
async def __anext__(self) -> _Record:
203204
if self._state is None:
204205
self._state = await self._connection._get_statement(
205206
self._query, self._timeout, named=True)
@@ -219,12 +220,12 @@ async def __anext__(self) -> R:
219220
raise StopAsyncIteration
220221

221222

222-
class Cursor(BaseCursor[R]):
223+
class Cursor(BaseCursor[_Record]):
223224
"""An open *portal* into the results of a query."""
224225

225226
__slots__ = ()
226227

227-
async def _init(self: C, timeout: typing.Optional[float]) -> C:
228+
async def _init(self: _Cursor, timeout: typing.Optional[float]) -> _Cursor:
228229
if self._state is None:
229230
self._state = await self._connection._get_statement(
230231
self._query, timeout, named=True)
@@ -235,7 +236,8 @@ async def _init(self: C, timeout: typing.Optional[float]) -> C:
235236

236237
@connresource.guarded
237238
async def fetch(self, n: int, *,
238-
timeout: typing.Optional[float] = None) -> typing.List[R]:
239+
timeout: typing.Optional[float] = None) \
240+
-> typing.List[_Record]:
239241
r"""Return the next *n* rows as a list of :class:`Record` objects.
240242
241243
:param float timeout: Optional timeout value in seconds.
@@ -247,7 +249,7 @@ async def fetch(self, n: int, *,
247249
raise exceptions.InterfaceError('n must be greater than zero')
248250
if self._exhausted:
249251
return []
250-
recs = await self._exec(n, timeout) # type: typing.List[R]
252+
recs = await self._exec(n, timeout) # type: typing.List[_Record]
251253
if len(recs) < n:
252254
self._exhausted = True
253255
return recs
@@ -256,7 +258,7 @@ async def fetch(self, n: int, *,
256258
async def fetchrow(
257259
self, *,
258260
timeout: typing.Optional[float] = None
259-
) -> typing.Optional[R]:
261+
) -> typing.Optional[_Record]:
260262
r"""Return the next row.
261263
262264
:param float timeout: Optional timeout value in seconds.
@@ -266,7 +268,7 @@ async def fetchrow(
266268
self._check_ready()
267269
if self._exhausted:
268270
return None
269-
recs = await self._exec(1, timeout) # type: typing.List[R]
271+
recs = await self._exec(1, timeout) # type: typing.List[_Record]
270272
if len(recs) < 1:
271273
self._exhausted = True
272274
return None

0 commit comments

Comments
 (0)