Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/pip/pytest-7.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Aug 9, 2023
2 parents b7dece8 + 3cfabe2 commit a5d58bb
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.1.1
uses: dependabot/fetch-metadata@v1.3.4
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Enable auto-merge for Dependabot PRs
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
needs: lint
strategy:
matrix:
pyver: ["3.7", "3.8", "3.9", "3.10", "3.11-dev"]
pyver: ["3.7", "3.8", "3.9", "3.10", "3.11"]
os: [ubuntu, macos, windows]
include:
- pyver: pypy-3.9
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
python -m pytest tests
python -m coverage xml
- name: Upload coverage
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unit
Expand All @@ -110,7 +110,7 @@ jobs:
run:
python -m build
- name: Make Release
uses: aio-libs/create-release@v1.2.3
uses: aio-libs/create-release@v1.6.6
with:
changes_file: CHANGES.rst
name: async-timeout
Expand Down
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.3.0'
rev: 'v4.4.0'
hooks:
- id: check-merge-conflict
exclude: "rst$"
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
rev: v1.5.0
hooks:
- id: yesqa
- repo: https://github.com/PyCQA/isort
rev: '5.10.1'
rev: '5.12.0'
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: '22.3.0'
rev: '23.7.0'
hooks:
- id: black
language_version: python3 # Should be a command that runs python3.6+
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.3.0'
rev: 'v4.4.0'
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -35,17 +35,17 @@ repos:
- id: debug-statements
# Another entry is required to apply file-contents-sorter to another file
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.3.0'
rev: 'v4.4.0'
hooks:
- id: file-contents-sorter
files: |
.gitignore
- repo: https://github.com/asottile/pyupgrade
rev: 'v2.34.0'
rev: 'v3.10.1'
hooks:
- id: pyupgrade
args: ['--py36-plus']
- repo: https://github.com/PyCQA/flake8
rev: '4.0.1'
rev: '6.1.0'
hooks:
- id: flake8
1 change: 1 addition & 0 deletions CHANGES/333.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for Python 3.11.
1 change: 1 addition & 0 deletions CHANGES/362.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix compatibility with asyncio.timeout() on python 3.11+.
29 changes: 23 additions & 6 deletions async_timeout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
from typing_extensions import final


if sys.version_info >= (3, 11):

def _uncancel_task(task: "asyncio.Task[object]") -> None:
task.uncancel()

else:

def _uncancel_task(task: "asyncio.Task[object]") -> None:
pass


__version__ = "4.0.2"


Expand Down Expand Up @@ -84,14 +95,15 @@ class Timeout:
# The purpose is to time out as soon as possible
# without waiting for the next await expression.

__slots__ = ("_deadline", "_loop", "_state", "_timeout_handler")
__slots__ = ("_deadline", "_loop", "_state", "_timeout_handler", "_task")

def __init__(
self, deadline: Optional[float], loop: asyncio.AbstractEventLoop
) -> None:
self._loop = loop
self._state = _State.INIT

self._task: Optional["asyncio.Task[object]"] = None
self._timeout_handler = None # type: Optional[asyncio.Handle]
if deadline is None:
self._deadline = None # type: Optional[float]
Expand Down Expand Up @@ -147,6 +159,7 @@ def reject(self) -> None:
self._reject()

def _reject(self) -> None:
self._task = None
if self._timeout_handler is not None:
self._timeout_handler.cancel()
self._timeout_handler = None
Expand Down Expand Up @@ -194,11 +207,11 @@ def _reschedule(self) -> None:
if self._timeout_handler is not None:
self._timeout_handler.cancel()

task = asyncio.current_task()
self._task = asyncio.current_task()
if deadline <= now:
self._timeout_handler = self._loop.call_soon(self._on_timeout, task)
self._timeout_handler = self._loop.call_soon(self._on_timeout)
else:
self._timeout_handler = self._loop.call_at(deadline, self._on_timeout, task)
self._timeout_handler = self._loop.call_at(deadline, self._on_timeout)

def _do_enter(self) -> None:
if self._state != _State.INIT:
Expand All @@ -208,15 +221,19 @@ def _do_enter(self) -> None:

def _do_exit(self, exc_type: Optional[Type[BaseException]]) -> None:
if exc_type is asyncio.CancelledError and self._state == _State.TIMEOUT:
assert self._task is not None
_uncancel_task(self._task)
self._timeout_handler = None
self._task = None
raise asyncio.TimeoutError
# timeout has not expired
self._state = _State.EXIT
self._reject()
return None

def _on_timeout(self, task: "asyncio.Task[None]") -> None:
task.cancel()
def _on_timeout(self) -> None:
assert self._task is not None
self._task.cancel()
self._state = _State.TIMEOUT
# drop the reference early
self._timeout_handler = None
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ classifiers =
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11

[options]
python_requires = >=3.7
Expand Down
8 changes: 7 additions & 1 deletion tests/test_timeout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import sys
import time
from functools import wraps
from typing import Any, Callable, List, TypeVar
Expand Down Expand Up @@ -39,6 +40,10 @@ async def long_running_task() -> None:
await long_running_task()
assert t._loop is asyncio.get_event_loop()
assert canceled_raised, "CancelledError was not raised"
if sys.version_info >= (3, 11):
task = asyncio.current_task()
assert task is not None
assert not task.cancelling()


@pytest.mark.asyncio
Expand Down Expand Up @@ -144,7 +149,6 @@ async def test_timeout_time() -> None:

@pytest.mark.asyncio
async def test_outer_coro_is_not_cancelled() -> None:

has_timeout = False

async def outer() -> None:
Expand All @@ -159,6 +163,8 @@ async def outer() -> None:
await task
assert has_timeout
assert not task.cancelled()
if sys.version_info >= (3, 11):
assert not task.cancelling()
assert task.done()


Expand Down

0 comments on commit a5d58bb

Please sign in to comment.