From 5fb0138a3220161703e6ab1087319a669d14e7f4 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 4 Jul 2020 12:21:36 -0400 Subject: Implement rudimentary asyncio support w/ asyncpg Using the approach introduced at https://gist.github.com/zzzeek/6287e28054d3baddc07fa21a7227904e We can now create asyncio endpoints that are then handled in "implicit IO" form within the majority of the Core internals. Then coroutines are re-exposed at the point at which we call into asyncpg methods. Patch includes: * asyncpg dialect * asyncio package * engine, result, ORM session classes * new test fixtures, tests * some work with pep-484 and a short plugin for the pyannotate package, which seems to have so-so results Change-Id: Idbcc0eff72c4cad572914acdd6f40ddb1aef1a7d Fixes: #3414 --- lib/sqlalchemy/testing/plugin/pytestplugin.py | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'lib/sqlalchemy/testing/plugin/pytestplugin.py') diff --git a/lib/sqlalchemy/testing/plugin/pytestplugin.py b/lib/sqlalchemy/testing/plugin/pytestplugin.py index 015598952..3df239afa 100644 --- a/lib/sqlalchemy/testing/plugin/pytestplugin.py +++ b/lib/sqlalchemy/testing/plugin/pytestplugin.py @@ -25,6 +25,11 @@ else: if typing.TYPE_CHECKING: from typing import Sequence +try: + import asyncio +except ImportError: + pass + try: import xdist # noqa @@ -101,6 +106,24 @@ def pytest_configure(config): plugin_base.set_fixture_functions(PytestFixtureFunctions) + if config.option.dump_pyannotate: + global DUMP_PYANNOTATE + DUMP_PYANNOTATE = True + + +DUMP_PYANNOTATE = False + + +@pytest.fixture(autouse=True) +def collect_types_fixture(): + if DUMP_PYANNOTATE: + from pyannotate_runtime import collect_types + + collect_types.start() + yield + if DUMP_PYANNOTATE: + collect_types.stop() + def pytest_sessionstart(session): plugin_base.post_begin() @@ -109,6 +132,31 @@ def pytest_sessionstart(session): def pytest_sessionfinish(session): plugin_base.final_process_cleanup() + if session.config.option.dump_pyannotate: + from pyannotate_runtime import collect_types + + collect_types.dump_stats(session.config.option.dump_pyannotate) + + +def pytest_collection_finish(session): + if session.config.option.dump_pyannotate: + from pyannotate_runtime import collect_types + + lib_sqlalchemy = os.path.abspath("lib/sqlalchemy") + + def _filter(filename): + filename = os.path.normpath(os.path.abspath(filename)) + if "lib/sqlalchemy" not in os.path.commonpath( + [filename, lib_sqlalchemy] + ): + return None + if "testing" in filename: + return None + + return filename + + collect_types.init_types_collection(filter_filename=_filter) + if has_xdist: import uuid @@ -518,3 +566,10 @@ class PytestFixtureFunctions(plugin_base.FixtureFunctions): def get_current_test_name(self): return os.environ.get("PYTEST_CURRENT_TEST") + + def async_test(self, fn): + @_pytest_fn_decorator + def decorate(fn, *args, **kwargs): + asyncio.get_event_loop().run_until_complete(fn(*args, **kwargs)) + + return decorate(fn) -- cgit v1.2.1