Skip to content

Commit da85005

Browse files
committed
Update to 3.6 type annotations and remove 3.5 compatibility
1 parent b9e0ef9 commit da85005

14 files changed

+112
-191
lines changed

.travis.yml

+1-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: generic
33
env:
44
global:
55
- PYMODULE=asyncpg
6-
- RELEASE_PYTHON_VERSIONS="3.5 3.6 3.7 3.8"
6+
- RELEASE_PYTHON_VERSIONS="3.6 3.7 3.8"
77

88
- S3_UPLOAD_USERNAME=oss-ci-bot
99
- S3_UPLOAD_BUCKET=magicstack-oss-releases
@@ -93,20 +93,6 @@ jobs:
9393

9494
# Do a full test run on the latest supported version of PostgreSQL
9595
# on each supported version of Python.
96-
- name: "Test py 3.5"
97-
os: linux
98-
dist: focal
99-
language: python
100-
python: "3.5"
101-
env: BUILD=tests PGVERSION=12
102-
addons:
103-
apt:
104-
sources:
105-
- sourceline: 'deb https://apt.postgresql.org/pub/repos/apt/ focal-pgdg main'
106-
key_url: 'https://www.postgresql.org/media/keys/ACCC4CF8.asc'
107-
packages:
108-
- postgresql-12
109-
11096
- name: "Test py 3.6"
11197
os: linux
11298
dist: focal
@@ -195,10 +181,6 @@ jobs:
195181
addons:
196182
postgresql: "12"
197183

198-
- name: "OSX py 3.5"
199-
os: osx
200-
env: BUILD=tests,wheels PYTHON_VERSION=3.5.9 PGVERSION=12
201-
202184
- name: "OSX py 3.6"
203185
os: osx
204186
env: BUILD=tests,wheels PYTHON_VERSION=3.6.10 PGVERSION=12

asyncpg/cluster.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
from . import types
3232

3333

34-
_ConnectionSpec = typing_extensions.TypedDict('_ConnectionSpec', {
35-
'host': str,
36-
'port': str
37-
})
34+
class _ConnectionSpec(typing_extensions.TypedDict):
35+
host: str
36+
port: str
3837

3938

4039
_system = platform.uname().system
@@ -53,7 +52,7 @@ def find_available_port(port_range: typing.Tuple[int, int] = (49152, 65535),
5352
max_tries: int = 1000) -> typing.Optional[int]:
5453
low, high = port_range
5554

56-
port = low # type: typing.Optional[int]
55+
port: typing.Optional[int] = low
5756
try_no = 0
5857

5958
while try_no < max_tries:
@@ -85,11 +84,11 @@ def __init__(self, data_dir: str, *,
8584
self._data_dir = data_dir
8685
self._pg_config_path = pg_config_path
8786
self._pg_bin_dir = os.environ.get('PGINSTALLATION')
88-
self._pg_ctl = None # type: typing.Optional[str]
89-
self._daemon_pid = None # type: typing.Optional[int]
90-
self._daemon_process = None # type: typing.Optional[subprocess.Popen[bytes]] # noqa: E501
91-
self._connection_addr = None # type: typing.Optional[_ConnectionSpec]
92-
self._connection_spec_override = None # type: typing.Optional[_ConnectionSpec] # noqa: E501
87+
self._pg_ctl: typing.Optional[str] = None
88+
self._daemon_pid: typing.Optional[int] = None
89+
self._daemon_process: typing.Optional[subprocess.Popen[bytes]] = None
90+
self._connection_addr: typing.Optional[_ConnectionSpec] = None
91+
self._connection_spec_override: typing.Optional[_ConnectionSpec] = None
9392

9493
def get_pg_version(self) -> 'types.ServerVersion':
9594
return self._pg_version
@@ -132,7 +131,7 @@ def get_status(self) -> str:
132131
async def connect(self,
133132
loop: typing.Optional[asyncio.AbstractEventLoop] = None,
134133
**kwargs: typing.Any) -> 'connection.Connection':
135-
conn_info = self.get_connection_spec() # type: typing.Optional[typing.Any] # noqa: E501
134+
conn_info: typing.Optional[typing.Any] = self.get_connection_spec()
136135
conn_info.update(kwargs) # type: ignore[union-attr]
137136
return await asyncpg.connect(loop=loop, **conn_info) # type: ignore[misc] # noqa: E501
138137

@@ -215,7 +214,7 @@ def start(self, wait: int = 60, *,
215214
# is not permitted and there is no easy way to drop
216215
# privileges.
217216
if os.getenv('ASYNCPG_DEBUG_SERVER'):
218-
stdout = sys.stdout # type: typing.Union[int, typing.TextIO]
217+
stdout: typing.Union[int, typing.TextIO] = sys.stdout
219218
else:
220219
stdout = subprocess.DEVNULL
221220

asyncpg/compat.py

-61
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,17 @@
66

77

88
import asyncio
9-
import functools
10-
import os
119
import pathlib
1210
import platform
1311
import sys
1412
import typing
15-
import typing_extensions
1613

1714

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[
21-
...,
22-
typing.Coroutine[typing.Any, typing.Any, typing.Any]
23-
])
24-
2515
PY_36 = sys.version_info >= (3, 6)
2616
PY_37 = sys.version_info >= (3, 7)
2717
SYSTEM = platform.uname().system
2818

2919

30-
if sys.version_info < (3, 5, 2):
31-
def aiter_compat(func: _F) -> _F_35:
32-
@functools.wraps(func)
33-
async def wrapper(self: typing.Any) -> typing.Any:
34-
return func(self)
35-
return typing.cast(_F_35, wrapper)
36-
else:
37-
def aiter_compat(func: _F) -> _F: # type: ignore[misc]
38-
return func
39-
40-
41-
class PathLike(typing_extensions.Protocol[_T_co]):
42-
def __fspath__(self) -> _T_co:
43-
...
44-
45-
46-
if sys.version_info >= (3, 6):
47-
fspath = os.fspath
48-
else:
49-
@typing.overload
50-
def fspath(path: str) -> str:
51-
...
52-
53-
@typing.overload
54-
def fspath(path: bytes) -> bytes:
55-
...
56-
57-
@typing.overload
58-
def fspath(path: PathLike[typing.AnyStr]) -> typing.AnyStr:
59-
...
60-
61-
def fspath(path: typing.Any) -> typing.Any:
62-
fsp = getattr(path, '__fspath__', None)
63-
if fsp is not None and callable(fsp):
64-
path = fsp()
65-
if not isinstance(path, (str, bytes)):
66-
raise TypeError(
67-
'expected {}() to return str or bytes, not {}'.format(
68-
fsp.__qualname__, type(path).__name__
69-
))
70-
return path
71-
elif isinstance(path, (str, bytes)):
72-
return path
73-
else:
74-
raise TypeError(
75-
'expected str, bytes or path-like object, not {}'.format(
76-
type(path).__name__
77-
)
78-
)
79-
80-
8120
if SYSTEM == 'Windows':
8221
import ctypes.wintypes
8322

asyncpg/connect_utils.py

+24-28
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,22 @@
3737
HostType = typing.Union[typing.List[str], str]
3838
PortType = typing.Union[typing.List[int], int]
3939

40-
_ConnectionParameters = typing.NamedTuple(
41-
'ConnectionParameters',
42-
[
43-
('user', str),
44-
('password', typing.Optional[str]),
45-
('database', str),
46-
('ssl', typing.Optional[SSLType]),
47-
('ssl_is_advisory', typing.Optional[bool]),
48-
('connect_timeout', typing.Optional[float]),
49-
('server_settings', typing.Optional[typing.Dict[str, str]])
50-
])
51-
52-
53-
_ClientConfiguration = typing.NamedTuple(
54-
'ConnectionConfiguration',
55-
[
56-
('command_timeout', typing.Optional[float]),
57-
('statement_cache_size', int),
58-
('max_cached_statement_lifetime', int),
59-
('max_cacheable_statement_size', int),
60-
])
40+
41+
class _ConnectionParameters(typing.NamedTuple):
42+
user: str
43+
password: typing.Optional[str]
44+
database: str
45+
ssl: typing.Optional[SSLType]
46+
ssl_is_advisory: typing.Optional[bool]
47+
connect_timeout: typing.Optional[float]
48+
server_settings: typing.Optional[typing.Dict[str, str]]
49+
50+
51+
class _ClientConfiguration(typing.NamedTuple):
52+
command_timeout: typing.Optional[float]
53+
statement_cache_size: int
54+
max_cached_statement_lifetime: int
55+
max_cacheable_statement_size: int
6156

6257

6358
_system = platform.uname().system
@@ -175,15 +170,16 @@ def _parse_hostlist(hostlist: str,
175170
else:
176171
hostspecs = [hostlist]
177172

178-
hosts = [] # type: typing.List[str]
179-
hostlist_ports = [] # type: typing.List[int]
180-
ports = None # type: typing.Optional[typing.List[int]]
173+
hosts: typing.List[str] = []
174+
hostlist_ports: typing.List[int] = []
175+
ports: typing.Optional[typing.List[int]] = None
181176

182177
if not port:
183178
portspec = os.environ.get('PGPORT')
184179
if portspec:
185180
if ',' in portspec:
186-
temp_port = [int(p) for p in portspec.split(',')] # type: typing.Union[typing.List[int], int] # noqa: E501
181+
temp_port: typing.Union[typing.List[int], int] = [
182+
int(p) for p in portspec.split(',')]
187183
else:
188184
temp_port = int(portspec)
189185
else:
@@ -274,7 +270,7 @@ def _parse_connect_dsn_and_args(*, dsn: typing.Optional[str],
274270

275271
if parsed.query:
276272
query = urllib.parse.parse_qs(parsed.query, strict_parsing=True)
277-
query_str = {} # type: typing.Dict[str, str]
273+
query_str: typing.Dict[str, str] = {}
278274
for key, val in query.items():
279275
if isinstance(val, list):
280276
query_str[key] = val[-1]
@@ -404,7 +400,7 @@ def _parse_connect_dsn_and_args(*, dsn: typing.Optional[str],
404400
database=database, user=user,
405401
passfile=passfile) # type: ignore[arg-type]
406402

407-
addrs = [] # type: typing.List[AddrType]
403+
addrs: typing.List[AddrType] = []
408404
for h, p in zip(host, port):
409405
if h.startswith('/'):
410406
# UNIX socket name
@@ -726,7 +722,7 @@ async def _connect(*, loop: typing.Optional[asyncio.AbstractEventLoop],
726722

727723
addrs, params, config = _parse_connect_arguments(timeout=timeout, **kwargs)
728724

729-
last_error = None # type: typing.Optional[BaseException]
725+
last_error: typing.Optional[BaseException] = None
730726
addr = None
731727
for addr in addrs:
732728
before = time.monotonic()

0 commit comments

Comments
 (0)