Skip to content

Commit 276bb46

Browse files
committed
Add typings
Add typings to the project and check the project using mypy. `PostgresMessage` and `PoolConnectionProxy` were broken out into their own files to make it easier to add typing via stub (pyi) files. Since they are metaclasses which generate dynamic objects, we can't type them directly in their python module.
1 parent c2c8d20 commit 276bb46

35 files changed

+3849
-1315
lines changed

.flake8

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
[flake8]
2+
select = C90,E,F,W,Y0
23
ignore = E402,E731,W503,W504,E252
3-
exclude = .git,__pycache__,build,dist,.eggs,.github,.local,.venv,.tox
4+
exclude = .git,__pycache__,build,dist,.eggs,.github,.local,.venv*,.tox
5+
per-file-ignores = *.pyi: F401, F403, F405, F811, E127, E128, E203, E266, E301, E302, E305, E501, E701, E704, E741, B303, W503, W504

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
github_token: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
2323
version_file: asyncpg/_version.py
2424
version_line_pattern: |
25-
__version__\s*=\s*(?:['"])([[:PEP440:]])(?:['"])
25+
__version__(?:\s*:\s*typing\.Final)?\s*=\s*(?:['"])([[:PEP440:]])(?:['"])
2626
2727
- name: Stop if not approved
2828
if: steps.checkver.outputs.approved != 'true'

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ docs/_build
3434
/.eggs
3535
/.vscode
3636
/.mypy_cache
37+
/.venv*
38+
/.tox
39+
/.vim

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "asyncpg/pgproto"]
22
path = asyncpg/pgproto
3-
url = https://github.com/MagicStack/py-pgproto.git
3+
url = https://github.com/bryanforbes/py-pgproto.git

MANIFEST.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
recursive-include docs *.py *.rst Makefile *.css
22
recursive-include examples *.py
33
recursive-include tests *.py *.pem
4-
recursive-include asyncpg *.pyx *.pxd *.pxi *.py *.c *.h
4+
recursive-include asyncpg *.pyx *.pxd *.pxi *.py *.pyi *.c *.h
55
include LICENSE README.rst Makefile performance.png .flake8
6+
include asyncpg/py.typed

asyncpg/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
# This module is part of asyncpg and is released under
55
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
66

7+
from __future__ import annotations
78

89
from .connection import connect, Connection # NOQA
910
from .exceptions import * # NOQA
1011
from .pool import create_pool, Pool # NOQA
1112
from .protocol import Record # NOQA
1213
from .types import * # NOQA
1314

14-
15+
from . import exceptions
1516
from ._version import __version__ # NOQA
1617

1718

18-
__all__ = ('connect', 'create_pool', 'Pool', 'Record', 'Connection')
19+
__all__ = ['connect', 'create_pool', 'Pool', 'Record', 'Connection']
1920
__all__ += exceptions.__all__ # NOQA

asyncpg/_asyncio_compat.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@
44
#
55
# SPDX-License-Identifier: PSF-2.0
66

7+
from __future__ import annotations
78

89
import asyncio
910
import functools
1011
import sys
12+
import typing
13+
14+
if typing.TYPE_CHECKING:
15+
from . import compat
1116

1217
if sys.version_info < (3, 11):
1318
from async_timeout import timeout as timeout_ctx
1419
else:
1520
from asyncio import timeout as timeout_ctx
1621

1722

18-
async def wait_for(fut, timeout):
23+
_T = typing.TypeVar('_T')
24+
25+
26+
async def wait_for(
27+
fut: compat.Awaitable[_T], timeout: float | None
28+
) -> _T:
1929
"""Wait for the single Future or coroutine to complete, with timeout.
2030
2131
Coroutine will be wrapped in Task.
@@ -65,7 +75,7 @@ async def wait_for(fut, timeout):
6575
return await fut
6676

6777

68-
async def _cancel_and_wait(fut):
78+
async def _cancel_and_wait(fut: asyncio.Future[_T]) -> None:
6979
"""Cancel the *fut* future or task and wait until it completes."""
7080

7181
loop = asyncio.get_running_loop()
@@ -82,6 +92,6 @@ async def _cancel_and_wait(fut):
8292
fut.remove_done_callback(cb)
8393

8494

85-
def _release_waiter(waiter, *args):
95+
def _release_waiter(waiter: asyncio.Future[typing.Any], *args: object) -> None:
8696
if not waiter.done():
8797
waiter.set_result(None)

asyncpg/_version.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
# supported platforms, publish the packages on PyPI, merge the PR
1111
# to the target branch, create a Git tag pointing to the commit.
1212

13-
__version__ = '0.30.0.dev0'
13+
from __future__ import annotations
14+
15+
import typing
16+
17+
__version__: typing.Final = '0.30.0.dev0'

0 commit comments

Comments
 (0)