Skip to content

Commit

Permalink
Add typings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bryanforbes committed Feb 15, 2024
1 parent c2c8d20 commit 276bb46
Show file tree
Hide file tree
Showing 35 changed files with 3,849 additions and 1,315 deletions.
4 changes: 3 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[flake8]
select = C90,E,F,W,Y0
ignore = E402,E731,W503,W504,E252
exclude = .git,__pycache__,build,dist,.eggs,.github,.local,.venv,.tox
exclude = .git,__pycache__,build,dist,.eggs,.github,.local,.venv*,.tox
per-file-ignores = *.pyi: F401, F403, F405, F811, E127, E128, E203, E266, E301, E302, E305, E501, E701, E704, E741, B303, W503, W504
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
github_token: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
version_file: asyncpg/_version.py
version_line_pattern: |
__version__\s*=\s*(?:['"])([[:PEP440:]])(?:['"])
__version__(?:\s*:\s*typing\.Final)?\s*=\s*(?:['"])([[:PEP440:]])(?:['"])
- name: Stop if not approved
if: steps.checkver.outputs.approved != 'true'
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ docs/_build
/.eggs
/.vscode
/.mypy_cache
/.venv*
/.tox
/.vim
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "asyncpg/pgproto"]
path = asyncpg/pgproto
url = https://github.com/MagicStack/py-pgproto.git
url = https://github.com/bryanforbes/py-pgproto.git
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
recursive-include docs *.py *.rst Makefile *.css
recursive-include examples *.py
recursive-include tests *.py *.pem
recursive-include asyncpg *.pyx *.pxd *.pxi *.py *.c *.h
recursive-include asyncpg *.pyx *.pxd *.pxi *.py *.pyi *.c *.h
include LICENSE README.rst Makefile performance.png .flake8
include asyncpg/py.typed
5 changes: 3 additions & 2 deletions asyncpg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0

from __future__ import annotations

from .connection import connect, Connection # NOQA
from .exceptions import * # NOQA
from .pool import create_pool, Pool # NOQA
from .protocol import Record # NOQA
from .types import * # NOQA


from . import exceptions
from ._version import __version__ # NOQA


__all__ = ('connect', 'create_pool', 'Pool', 'Record', 'Connection')
__all__ = ['connect', 'create_pool', 'Pool', 'Record', 'Connection']
__all__ += exceptions.__all__ # NOQA
16 changes: 13 additions & 3 deletions asyncpg/_asyncio_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
#
# SPDX-License-Identifier: PSF-2.0

from __future__ import annotations

import asyncio
import functools
import sys
import typing

if typing.TYPE_CHECKING:
from . import compat

if sys.version_info < (3, 11):
from async_timeout import timeout as timeout_ctx
else:
from asyncio import timeout as timeout_ctx


async def wait_for(fut, timeout):
_T = typing.TypeVar('_T')


async def wait_for(
fut: compat.Awaitable[_T], timeout: float | None
) -> _T:
"""Wait for the single Future or coroutine to complete, with timeout.
Coroutine will be wrapped in Task.
Expand Down Expand Up @@ -65,7 +75,7 @@ async def wait_for(fut, timeout):
return await fut


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

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


def _release_waiter(waiter, *args):
def _release_waiter(waiter: asyncio.Future[typing.Any], *args: object) -> None:
if not waiter.done():
waiter.set_result(None)
6 changes: 5 additions & 1 deletion asyncpg/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
# supported platforms, publish the packages on PyPI, merge the PR
# to the target branch, create a Git tag pointing to the commit.

__version__ = '0.30.0.dev0'
from __future__ import annotations

import typing

__version__: typing.Final = '0.30.0.dev0'
Loading

0 comments on commit 276bb46

Please sign in to comment.