Skip to content

Commit

Permalink
Fix deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jnak committed Jun 15, 2019
1 parent 9a0f740 commit dac341a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion graphene_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .fields import SQLAlchemyConnectionField
from .utils import get_query, get_session

__version__ = "2.2.0"
__version__ = "2.2.1"

__all__ = [
"__version__",
Expand Down
21 changes: 11 additions & 10 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
import warnings
from functools import partial

from promise import Promise, is_thenable
Expand All @@ -10,8 +10,6 @@

from .utils import get_query

log = logging.getLogger()


class UnsortedSQLAlchemyConnectionField(ConnectionField):
@property
Expand Down Expand Up @@ -100,34 +98,37 @@ def __init__(self, type, *args, **kwargs):
def default_connection_field_factory(relationship, registry, **field_kwargs):
model = relationship.mapper.entity
model_type = registry.get_type_for_model(model)
return createConnectionField(model_type, **field_kwargs)
return __connectionFactory(model_type, **field_kwargs)


# TODO Remove in next major version
__connectionFactory = UnsortedSQLAlchemyConnectionField


def createConnectionField(_type, **field_kwargs):
log.warning(
warnings.warn(
'createConnectionField is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
return __connectionFactory(_type, **field_kwargs)


def registerConnectionFieldFactory(factoryMethod):
log.warning(
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
global __connectionFactory
__connectionFactory = factoryMethod


def unregisterConnectionFieldFactory():
log.warning(
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
global __connectionFactory
__connectionFactory = UnsortedSQLAlchemyConnectionField
44 changes: 23 additions & 21 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,33 +364,35 @@ class Meta:


def test_deprecated_registerConnectionFieldFactory():
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
with pytest.warns(DeprecationWarning):
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)

class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)
class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)


def test_deprecated_unregisterConnectionFieldFactory():
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
unregisterConnectionFieldFactory()
with pytest.warns(DeprecationWarning):
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
unregisterConnectionFieldFactory()

class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)
class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)

0 comments on commit dac341a

Please sign in to comment.