Skip to content

Commit e24f292

Browse files
committed
Update flake8 (newer pyflakes handles overloads) and update variables
1 parent 9e6cf86 commit e24f292

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):
@@ -921,7 +922,7 @@ async def _copy_out(self, copy_stmt: str,
921922
# output is not a path-like object
922923
path = None
923924

924-
writer = None # type: typing.Optional[Writer]
925+
writer = None # type: typing.Optional[_Writer]
925926
opened_by_us = False
926927
run_in_executor = self._loop.run_in_executor
927928

@@ -1509,7 +1510,7 @@ def _set_proxy(self, proxy: typing.Optional['_pool.PoolConnectionProxy']) \
15091510
self._proxy = proxy
15101511

15111512
def _check_listeners(self,
1512-
listeners: 'typing._Collection[AnyCallable]',
1513+
listeners: 'typing._Collection[_AnyCallable]',
15131514
listener_type: str) -> None:
15141515
if listeners:
15151516
count = len(listeners)
@@ -1606,24 +1607,24 @@ async def reload_schema_state(self) -> None:
16061607
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
16071608
limit: int, timeout: typing.Optional[float],
16081609
return_status: typing_extensions.Literal[True]) \
1609-
-> RecordsExtraType:
1610+
-> _RecordsExtraType:
16101611
...
16111612

1612-
@typing.overload # noqa: F811
1613+
@typing.overload
16131614
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
16141615
limit: int, timeout: typing.Optional[float],
16151616
return_status: typing_extensions.Literal[False] = ...) \
1616-
-> RecordsType:
1617+
-> _RecordsType:
16171618
...
16181619

1619-
@typing.overload # noqa: F811
1620+
@typing.overload
16201621
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
16211622
limit: int, timeout: typing.Optional[float],
16221623
return_status: bool) \
1623-
-> typing.Union[RecordsExtraType, RecordsType]:
1624+
-> typing.Union[_RecordsExtraType, _RecordsType]:
16241625
...
16251626

1626-
async def _execute(self, query: str, args: typing.Sequence[typing.Any], # noqa: F811, E501
1627+
async def _execute(self, query: str, args: typing.Sequence[typing.Any],
16271628
limit: int, timeout: typing.Optional[float],
16281629
return_status: bool = False) -> typing.Any:
16291630
with self._stmt_exclusive_section:
@@ -1635,29 +1636,29 @@ async def _execute(self, query: str, args: typing.Sequence[typing.Any], # noqa:
16351636
async def __execute(self, query: str, args: typing.Sequence[typing.Any],
16361637
limit: int, timeout: typing.Optional[float],
16371638
return_status: typing_extensions.Literal[True]) \
1638-
-> typing.Tuple[RecordsExtraType,
1639+
-> typing.Tuple[_RecordsExtraType,
16391640
'_cprotocol.PreparedStatementState']:
16401641
...
16411642

1642-
@typing.overload # noqa: F811
1643+
@typing.overload
16431644
async def __execute(self, query: str, args: typing.Sequence[typing.Any],
16441645
limit: int, timeout: typing.Optional[float],
16451646
return_status: typing_extensions.Literal[
16461647
False] = ...) \
1647-
-> typing.Tuple[RecordsType, '_cprotocol.PreparedStatementState']:
1648+
-> typing.Tuple[_RecordsType, '_cprotocol.PreparedStatementState']:
16481649
...
16491650

1650-
@typing.overload # noqa: F811
1651+
@typing.overload
16511652
async def __execute(self, query: str, args: typing.Sequence[typing.Any],
16521653
limit: int, timeout: typing.Optional[float],
16531654
return_status: bool) \
1654-
-> typing.Union[typing.Tuple[RecordsExtraType,
1655+
-> typing.Union[typing.Tuple[_RecordsExtraType,
16551656
'_cprotocol.PreparedStatementState'],
1656-
typing.Tuple[RecordsType,
1657+
typing.Tuple[_RecordsType,
16571658
'_cprotocol.PreparedStatementState']]:
16581659
...
16591660

1660-
async def __execute(self, query: str, # noqa: F811
1661+
async def __execute(self, query: str,
16611662
args: typing.Sequence[typing.Any],
16621663
limit: int, timeout: typing.Optional[float],
16631664
return_status: bool = False) -> typing.Tuple[
@@ -1762,7 +1763,7 @@ async def connect(dsn: typing.Optional[str] = ..., *,
17621763
...
17631764

17641765

1765-
@typing.overload # noqa: F811
1766+
@typing.overload
17661767
async def connect(dsn: typing.Optional[str] = ..., *,
17671768
host: typing.Optional[connect_utils.HostType] = ...,
17681769
port: typing.Optional[connect_utils.PortType] = ...,
@@ -1777,13 +1778,13 @@ async def connect(dsn: typing.Optional[str] = ..., *,
17771778
max_cacheable_statement_size: int = ...,
17781779
command_timeout: typing.Optional[float] = ...,
17791780
ssl: typing.Optional[connect_utils.SSLType] = ...,
1780-
connection_class: typing.Type[C],
1781+
connection_class: typing.Type[_Connection],
17811782
server_settings: typing.Optional[
1782-
typing.Dict[str, str]] = ...) -> C:
1783+
typing.Dict[str, str]] = ...) -> _Connection:
17831784
...
17841785

17851786

1786-
async def connect(dsn: typing.Optional[str] = None, *, # noqa: F811
1787+
async def connect(dsn: typing.Optional[str] = None, *,
17871788
host: typing.Optional[connect_utils.HostType] = None,
17881789
port: typing.Optional[connect_utils.PortType] = None,
17891790
user: typing.Optional[str] = None,
@@ -1797,10 +1798,9 @@ async def connect(dsn: typing.Optional[str] = None, *, # noqa: F811
17971798
max_cacheable_statement_size: int = 1024 * 15,
17981799
command_timeout: typing.Optional[float] = None,
17991800
ssl: typing.Optional[connect_utils.SSLType] = None,
1800-
connection_class: typing.Type[C] = \
1801-
Connection, # type: ignore
1801+
connection_class: typing.Type[_Connection] = Connection, # type: ignore # noqa: E501
18021802
server_settings: typing.Optional[
1803-
typing.Dict[str, str]] = None) -> C:
1803+
typing.Dict[str, str]] = None) -> _Connection:
18041804
r"""A coroutine to establish a connection to a PostgreSQL server.
18051805
18061806
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)