Skip to content

Commit c89cf80

Browse files
authored
Fix createConnectionField deprecation warnings (#229)
* Fix deprecation warnings * Release `2.2.1` * Add unrelated test to increase test coverage and unblock PR
1 parent 9a0f740 commit c89cf80

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

graphene_sqlalchemy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .fields import SQLAlchemyConnectionField
33
from .utils import get_query, get_session
44

5-
__version__ = "2.2.0"
5+
__version__ = "2.2.1"
66

77
__all__ = [
88
"__version__",

graphene_sqlalchemy/fields.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import logging
1+
import warnings
22
from functools import partial
33

44
from promise import Promise, is_thenable
@@ -10,8 +10,6 @@
1010

1111
from .utils import get_query
1212

13-
log = logging.getLogger()
14-
1513

1614
class UnsortedSQLAlchemyConnectionField(ConnectionField):
1715
@property
@@ -100,34 +98,37 @@ def __init__(self, type, *args, **kwargs):
10098
def default_connection_field_factory(relationship, registry, **field_kwargs):
10199
model = relationship.mapper.entity
102100
model_type = registry.get_type_for_model(model)
103-
return createConnectionField(model_type, **field_kwargs)
101+
return __connectionFactory(model_type, **field_kwargs)
104102

105103

106104
# TODO Remove in next major version
107105
__connectionFactory = UnsortedSQLAlchemyConnectionField
108106

109107

110108
def createConnectionField(_type, **field_kwargs):
111-
log.warning(
109+
warnings.warn(
112110
'createConnectionField is deprecated and will be removed in the next '
113-
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
111+
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
112+
DeprecationWarning,
114113
)
115114
return __connectionFactory(_type, **field_kwargs)
116115

117116

118117
def registerConnectionFieldFactory(factoryMethod):
119-
log.warning(
118+
warnings.warn(
120119
'registerConnectionFieldFactory is deprecated and will be removed in the next '
121-
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
120+
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
121+
DeprecationWarning,
122122
)
123123
global __connectionFactory
124124
__connectionFactory = factoryMethod
125125

126126

127127
def unregisterConnectionFieldFactory():
128-
log.warning(
128+
warnings.warn(
129129
'registerConnectionFieldFactory is deprecated and will be removed in the next '
130-
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
130+
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
131+
DeprecationWarning,
131132
)
132133
global __connectionFactory
133134
__connectionFactory = UnsortedSQLAlchemyConnectionField

graphene_sqlalchemy/tests/test_types.py

+42-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ..converter import convert_sqlalchemy_composite
99
from ..fields import (SQLAlchemyConnectionField,
10-
UnsortedSQLAlchemyConnectionField,
10+
UnsortedSQLAlchemyConnectionField, createConnectionField,
1111
registerConnectionFieldFactory,
1212
unregisterConnectionFieldFactory)
1313
from ..types import ORMField, SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
@@ -224,6 +224,19 @@ class Meta:
224224
assert pets_field.type().description == 'Overridden'
225225

226226

227+
def test_invalid_model_attr():
228+
err_msg = (
229+
"Cannot map ORMField to a model attribute.\n"
230+
"Field: 'ReporterType.first_name'"
231+
)
232+
with pytest.raises(ValueError, match=err_msg):
233+
class ReporterType(SQLAlchemyObjectType):
234+
class Meta:
235+
model = Reporter
236+
237+
first_name = ORMField(model_attr='does_not_exist')
238+
239+
227240
def test_only_fields():
228241
class ReporterType(SQLAlchemyObjectType):
229242
class Meta:
@@ -364,33 +377,40 @@ class Meta:
364377

365378

366379
def test_deprecated_registerConnectionFieldFactory():
367-
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
380+
with pytest.warns(DeprecationWarning):
381+
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
368382

369-
class ReporterType(SQLAlchemyObjectType):
370-
class Meta:
371-
model = Reporter
372-
interfaces = (Node,)
383+
class ReporterType(SQLAlchemyObjectType):
384+
class Meta:
385+
model = Reporter
386+
interfaces = (Node,)
373387

374-
class ArticleType(SQLAlchemyObjectType):
375-
class Meta:
376-
model = Article
377-
interfaces = (Node,)
388+
class ArticleType(SQLAlchemyObjectType):
389+
class Meta:
390+
model = Article
391+
interfaces = (Node,)
378392

379-
assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
393+
assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
380394

381395

382396
def test_deprecated_unregisterConnectionFieldFactory():
383-
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
384-
unregisterConnectionFieldFactory()
397+
with pytest.warns(DeprecationWarning):
398+
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
399+
unregisterConnectionFieldFactory()
385400

386-
class ReporterType(SQLAlchemyObjectType):
387-
class Meta:
388-
model = Reporter
389-
interfaces = (Node,)
401+
class ReporterType(SQLAlchemyObjectType):
402+
class Meta:
403+
model = Reporter
404+
interfaces = (Node,)
405+
406+
class ArticleType(SQLAlchemyObjectType):
407+
class Meta:
408+
model = Article
409+
interfaces = (Node,)
410+
411+
assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
390412

391-
class ArticleType(SQLAlchemyObjectType):
392-
class Meta:
393-
model = Article
394-
interfaces = (Node,)
395413

396-
assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
414+
def test_deprecated_createConnectionField():
415+
with pytest.warns(DeprecationWarning):
416+
createConnectionField(None)

graphene_sqlalchemy/types.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def construct_fields(
133133
for orm_field_name, orm_field in custom_orm_fields_items:
134134
attr_name = orm_field.kwargs.get('model_attr', orm_field_name)
135135
if attr_name not in all_model_attrs:
136-
raise Exception('Cannot map ORMField "{}" to SQLAlchemy model property'.format(orm_field_name))
136+
raise ValueError((
137+
"Cannot map ORMField to a model attribute.\n"
138+
"Field: '{}.{}'"
139+
).format(obj_type.__name__, orm_field_name,))
137140
orm_field.kwargs['model_attr'] = attr_name
138141

139142
# Merge automatic fields with custom ORM fields

0 commit comments

Comments
 (0)