Skip to content

Commit

Permalink
Fix for metaclass errors in Python 3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanforbes committed Aug 23, 2020
1 parent 8332c03 commit 18f3e1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
17 changes: 15 additions & 2 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,22 @@ def __call__(
...


class ConnectionMeta(type):
class _ConnectionMeta(type):

def __instancecheck__(cls, instance: typing.Any) -> bool:
mro = type(instance).__mro__
return Connection in mro or _ConnectionProxy in mro


class Connection(typing.Generic[_Record], metaclass=ConnectionMeta):
if sys.version_info >= (3, 7):
ConnectionMeta = _ConnectionMeta
else:
# see: https://github.com/python/typing/issues/449
class ConnectionMeta(_ConnectionMeta, typing.GenericMeta):
...


class Connection(typing.Generic[_Record], metaclass=ConnectionMeta): # type: ignore[misc] # noqa: E501
"""A representation of a database session.
Connections are created by calling :func:`~asyncpg.connection.connect`.
Expand Down Expand Up @@ -2375,6 +2383,11 @@ async def _do_execute(
return result, stmt


if sys.version_info < (3, 7):
# see: https://bugs.python.org/issue41451
del Connection.__slots__


@typing.overload
async def connect(dsn: typing.Optional[str] = ..., *,
host: typing.Optional[connect_utils.HostType] = ...,
Expand Down
14 changes: 12 additions & 2 deletions asyncpg/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import functools
import inspect
import logging
import sys
import time
import typing
import warnings
Expand All @@ -24,7 +25,7 @@
_Record = typing.TypeVar('_Record', bound=protocol.Record)


class PoolConnectionProxyMeta(type):
class _PoolConnectionProxyMeta(type):

def __new__(mcls, name, bases, dct, *, wrap=False):
if wrap:
Expand Down Expand Up @@ -65,7 +66,16 @@ def call_con_method(self, *args, **kwargs):
return call_con_method


class PoolConnectionProxy(connection._ConnectionProxy,
if sys.version_info >= (3, 7):
PoolConnectionProxyMeta = _PoolConnectionProxyMeta
else:
# see: https://github.com/python/typing/issues/449
class PoolConnectionProxyMeta(_PoolConnectionProxyMeta,
typing.GenericMeta):
...


class PoolConnectionProxy(connection._ConnectionProxy[_Record],
metaclass=PoolConnectionProxyMeta,
wrap=True):

Expand Down

0 comments on commit 18f3e1b

Please sign in to comment.