From 713026d2a9d966f5b1631167fbcc666caf1737bd Mon Sep 17 00:00:00 2001 From: Nikita Grishko Date: Mon, 29 Apr 2019 17:26:56 +0300 Subject: [PATCH] [feature] add Python 3.6 support --- .github/main.workflow | 17 +++++++++++++++-- CHANGELOG.md | 2 ++ Makefile | 5 ++++- README.md | 6 ++++-- aiodogstatsd/client.py | 17 +++++++++++++---- examples/contextmanager.py | 3 ++- examples/timing.py | 3 ++- pyproject.toml | 2 +- 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/.github/main.workflow b/.github/main.workflow index bcaf52a..30d820b 100644 --- a/.github/main.workflow +++ b/.github/main.workflow @@ -3,8 +3,19 @@ workflow "run linters and tests" { resolves = ["notify build succeeded"] } +action "py3.6 linting and testing" { + uses = "docker://python:3.6" + runs = ["sh", "-c", "make ci-quality-basic"] + secrets = [ + "CODECOV_TOKEN", + ] +} + action "py3.7 linting and testing" { - uses = "docker://python:3.7.2" + needs = [ + "py3.6 linting and testing", + ] + uses = "docker://python:3.7" runs = ["sh", "-c", "make ci-quality"] secrets = [ "CODECOV_TOKEN", @@ -12,7 +23,9 @@ action "py3.7 linting and testing" { } action "notify build succeeded" { - needs = "py3.7 linting and testing" + needs = [ + "py3.7 linting and testing", + ] uses = "docker://gr1n/the-telegram-action:master" env = { TELEGRAM_MESSAGE = "`aiodogstatsd` build succeeded" diff --git a/CHANGELOG.md b/CHANGELOG.md index 414a566..cd7599f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.4.0 (2019-XX-XX) +- Added Python 3.6.* support. + ## 0.3.0 (2019-04-21) - Fixed datagram format. diff --git a/Makefile b/Makefile index 9088a21..36b7179 100644 --- a/Makefile +++ b/Makefile @@ -50,8 +50,11 @@ test: publish: @$(POETRY) publish --username=$(PYPI_USERNAME) --password=$(PYPI_PASSWORD) --build +.PHONY: ci-quality-basic +ci-quality-basic: install lint test + .PHONY: ci-quality -ci-quality: install lint test +ci-quality: ci-quality-basic @$(POETRY) run codecov --token=$(CODECOV_TOKEN) .PHONY: ci-publish diff --git a/README.md b/README.md index 1e97bcd..2d65562 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ async def main(): await client.close() -asyncio.run(main()) +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) ``` ...or you can also use client as a context manager: @@ -47,7 +48,8 @@ async def main(): client.increment("users.online") -asyncio.run(main()) +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) ``` Look at `examples/` to find more examples of library usage. diff --git a/aiodogstatsd/client.py b/aiodogstatsd/client.py index c5a7da4..71465e9 100644 --- a/aiodogstatsd/client.py +++ b/aiodogstatsd/client.py @@ -1,6 +1,5 @@ -from __future__ import annotations - import asyncio +import sys from asyncio.transports import DatagramTransport from random import random from typing import Optional @@ -61,7 +60,7 @@ def __init__( self._read_timeout = read_timeout self._close_timeout = close_timeout - async def __aenter__(self) -> Client: + async def __aenter__(self) -> "Client": await self.connect() return self @@ -69,7 +68,7 @@ async def __aexit__(self, *args) -> None: await self.close() async def connect(self) -> None: - loop = asyncio.get_running_loop() + loop = _get_event_loop() await loop.create_datagram_endpoint( lambda: self._protocol, remote_addr=(self._host, self._port) ) @@ -249,3 +248,13 @@ def send(self, data: bytes) -> None: except Exception: # Errors should fail silently so they don't affect anything else pass + + +def _get_event_loop_factory(): # pragma: no cover + if sys.version_info >= (3, 7): + return asyncio.get_running_loop # type: ignore + + return asyncio.get_event_loop + + +_get_event_loop = _get_event_loop_factory() diff --git a/examples/contextmanager.py b/examples/contextmanager.py index 41f546c..3b715e4 100644 --- a/examples/contextmanager.py +++ b/examples/contextmanager.py @@ -13,4 +13,5 @@ async def main(): if __name__ == "__main__": - asyncio.run(main()) + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/examples/timing.py b/examples/timing.py index a881442..5c2054d 100644 --- a/examples/timing.py +++ b/examples/timing.py @@ -17,4 +17,5 @@ async def main(): if __name__ == "__main__": - asyncio.run(main()) + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/pyproject.toml b/pyproject.toml index e539f57..5282cbc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,7 @@ classifiers = [ ] [tool.poetry.dependencies] -python = "^3.7" +python = "^3.6" [tool.poetry.dev-dependencies] black = { version = ">=18.9b0", allows-prereleases = true }