Skip to content

Commit 95db9f9

Browse files
committed
Remove static imports to graphene to increase readability due to ambivalent type names (in most cases)
Signed-off-by: Erik Wrede <[email protected]>
1 parent b1dd214 commit 95db9f9

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

graphene_sqlalchemy/converter.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
default_connection_field_factory)
2020
from .resolvers import get_attr_resolver, get_custom_resolver
2121
from .utils import (registry_sqlalchemy_model_from_str, safe_isinstance,
22-
singledispatchbymatchfunction, value_equals)
22+
singledispatchbymatchfunction, value_equals, DummyImport)
2323

2424
try:
2525
from typing import ForwardRef
@@ -28,13 +28,12 @@
2828
from typing import _ForwardRef as ForwardRef
2929

3030
try:
31-
from sqlalchemy_utils import (ChoiceType, JSONType, ScalarListType,
32-
TSVectorType, UUIDType)
31+
import sqlalchemy_utils as sqa_utils
3332
except ImportError:
34-
ChoiceType = JSONType = ScalarListType = TSVectorType = UUIDType = object
33+
sqlalchemy_utils = DummyImport()
3534

3635
try:
37-
from sqlalchemy_utils.types.choice import EnumTypeImpl
36+
from sqa_utils.types.choice import EnumTypeImpl
3837
except ImportError:
3938
EnumTypeImpl = object
4039

@@ -200,13 +199,16 @@ def convert_sqlalchemy_type(type, column, registry=None):
200199
@convert_sqlalchemy_type.register(sqa_types.UnicodeText)
201200
@convert_sqlalchemy_type.register(postgresql.INET)
202201
@convert_sqlalchemy_type.register(postgresql.CIDR)
203-
@convert_sqlalchemy_type.register(TSVectorType)
202+
@convert_sqlalchemy_type.register(sqa_utils.TSVectorType)
203+
@convert_sqlalchemy_type.register(sqa_utils.EmailType)
204+
@convert_sqlalchemy_type.register(sqa_utils.URLType)
205+
@convert_sqlalchemy_type.register(sqa_utils.IPAddressType)
204206
def convert_column_to_string(type, column, registry=None):
205207
return graphene.String
206208

207209

208210
@convert_sqlalchemy_type.register(postgresql.UUID)
209-
@convert_sqlalchemy_type.register(UUIDType)
211+
@convert_sqlalchemy_type.register(sqa_utils.UUIDType)
210212
def convert_column_to_uuid(type, column, registry=None):
211213
return graphene.UUID
212214

@@ -251,7 +253,7 @@ def convert_enum_to_enum(type, column, registry=None):
251253

252254

253255
# TODO Make ChoiceType conversion consistent with other enums
254-
@convert_sqlalchemy_type.register(ChoiceType)
256+
@convert_sqlalchemy_type.register(sqa_utils.ChoiceType)
255257
def convert_choice_to_enum(type, column, registry=None):
256258
name = "{}_{}".format(column.table.name, column.key).upper()
257259
if isinstance(type.type_impl, EnumTypeImpl):
@@ -262,7 +264,7 @@ def convert_choice_to_enum(type, column, registry=None):
262264
return graphene.Enum(name, type.choices)
263265

264266

265-
@convert_sqlalchemy_type.register(ScalarListType)
267+
@convert_sqlalchemy_type.register(sqa_utils.ScalarListType)
266268
def convert_scalar_list_to_list(type, column, registry=None):
267269
return graphene.List(graphene.String)
268270

@@ -285,7 +287,7 @@ def convert_json_to_string(type, column, registry=None):
285287
return JSONString
286288

287289

288-
@convert_sqlalchemy_type.register(JSONType)
290+
@convert_sqlalchemy_type.register(sqa_utils.JSONType)
289291
@convert_sqlalchemy_type.register(sqa_types.JSON)
290292
def convert_json_type_to_string(type, column, registry=None):
291293
return JSONString

graphene_sqlalchemy/tests/test_converter.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
from sqlalchemy.ext.hybrid import hybrid_property
1010
from sqlalchemy.inspection import inspect
1111
from sqlalchemy.orm import column_property, composite
12-
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, UUIDType
12+
import sqlalchemy_utils as sqa_utils
1313

14-
import graphene
15-
from graphene import Boolean, Float, Int, Scalar, String
1614
from graphene.relay import Node
17-
from graphene.types.datetime import Date, DateTime, Time
18-
from graphene.types.json import JSONString
19-
from graphene.types.structures import List, Structure
15+
import graphene
16+
from graphene.types.structures import Structure
17+
2018

2119
from ..converter import (convert_sqlalchemy_column,
2220
convert_sqlalchemy_composite,
@@ -187,7 +185,7 @@ def test_should_numeric_convert_float():
187185

188186

189187
def test_should_choice_convert_enum():
190-
field = get_field(ChoiceType([(u"es", u"Spanish"), (u"en", u"English")]))
188+
field = get_field(sqa_utils.ChoiceType([(u"es", u"Spanish"), (u"en", u"English")]))
191189
graphene_type = field.type
192190
assert issubclass(graphene_type, graphene.Enum)
193191
assert graphene_type._meta.name == "MODEL_COLUMN"
@@ -200,7 +198,7 @@ class TestEnum(enum.Enum):
200198
es = u"Spanish"
201199
en = u"English"
202200

203-
field = get_field(ChoiceType(TestEnum, impl=types.String()))
201+
field = get_field(sqa_utils.ChoiceType(TestEnum, impl=types.String()))
204202
graphene_type = field.type
205203
assert issubclass(graphene_type, graphene.Enum)
206204
assert graphene_type._meta.name == "MODEL_COLUMN"
@@ -218,7 +216,7 @@ class TestEnum(enum.Enum):
218216
es = u"Spanish"
219217
en = u"English"
220218

221-
testChoice = Column("% descuento1", ChoiceType(TestEnum, impl=types.String()), key="descuento1")
219+
testChoice = Column("% descuento1", sqa_utils.ChoiceType(TestEnum, impl=types.String()), key="descuento1")
222220
field = get_field_from_column(testChoice)
223221

224222
graphene_type = field.type
@@ -233,7 +231,7 @@ class TestEnum(enum.IntEnum):
233231
one = 1
234232
two = 2
235233

236-
field = get_field(ChoiceType(TestEnum, impl=types.String()))
234+
field = get_field(sqa_utils.ChoiceType(TestEnum, impl=types.String()))
237235
graphene_type = field.type
238236
assert issubclass(graphene_type, graphene.Enum)
239237
assert graphene_type._meta.name == "MODEL_COLUMN"
@@ -250,14 +248,14 @@ def test_should_columproperty_convert():
250248

251249

252250
def test_should_scalar_list_convert_list():
253-
field = get_field(ScalarListType())
251+
field = get_field(sqa_utils.ScalarListType())
254252
assert isinstance(field.type, graphene.List)
255253
assert field.type.of_type == graphene.String
256254

257255

258256
def test_should_jsontype_convert_jsonstring():
259-
assert get_field(JSONType()).type == JSONString
260-
assert get_field(types.JSON).type == JSONString
257+
assert get_field(sqa_utils.JSONType()).type == graphene.JSONString
258+
assert get_field(types.JSON).type == graphene.JSONString
261259

262260

263261
def test_should_variant_int_convert_int():
@@ -369,7 +367,7 @@ def test_should_postgresql_uuid_convert():
369367

370368

371369
def test_should_sqlalchemy_utils_uuid_convert():
372-
assert get_field(UUIDType()).type == graphene.UUID
370+
assert get_field(sqa_utils.UUIDType()).type == graphene.UUID
373371

374372

375373
def test_should_postgresql_enum_convert():
@@ -483,8 +481,8 @@ class Meta:
483481
# Check ShoppingCartItem's Properties and Return Types
484482
#######################################################
485483

486-
shopping_cart_item_expected_types: Dict[str, Union[Scalar, Structure]] = {
487-
'hybrid_prop_shopping_cart': List(ShoppingCartType)
484+
shopping_cart_item_expected_types: Dict[str, Union[graphene.Scalar, Structure]] = {
485+
'hybrid_prop_shopping_cart': graphene.List(ShoppingCartType)
488486
}
489487

490488
assert sorted(list(ShoppingCartItemType._meta.fields.keys())) == sorted([
@@ -509,27 +507,27 @@ class Meta:
509507
# Check ShoppingCart's Properties and Return Types
510508
###################################################
511509

512-
shopping_cart_expected_types: Dict[str, Union[Scalar, Structure]] = {
510+
shopping_cart_expected_types: Dict[str, Union[graphene.Scalar, Structure]] = {
513511
# Basic types
514-
"hybrid_prop_str": String,
515-
"hybrid_prop_int": Int,
516-
"hybrid_prop_float": Float,
517-
"hybrid_prop_bool": Boolean,
518-
"hybrid_prop_decimal": String, # Decimals should be serialized Strings
519-
"hybrid_prop_date": Date,
520-
"hybrid_prop_time": Time,
521-
"hybrid_prop_datetime": DateTime,
512+
"hybrid_prop_str": graphene.String,
513+
"hybrid_prop_int": graphene.Int,
514+
"hybrid_prop_float": graphene.Float,
515+
"hybrid_prop_bool": graphene.Boolean,
516+
"hybrid_prop_decimal": graphene.String, # Decimals should be serialized Strings
517+
"hybrid_prop_date": graphene.Date,
518+
"hybrid_prop_time": graphene.Time,
519+
"hybrid_prop_datetime": graphene.DateTime,
522520
# Lists and Nested Lists
523-
"hybrid_prop_list_int": List(Int),
524-
"hybrid_prop_list_date": List(Date),
525-
"hybrid_prop_nested_list_int": List(List(Int)),
526-
"hybrid_prop_deeply_nested_list_int": List(List(List(Int))),
521+
"hybrid_prop_list_int": graphene.List(graphene.Int),
522+
"hybrid_prop_list_date": graphene.List(graphene.Date),
523+
"hybrid_prop_nested_list_int": graphene.List(graphene.List(graphene.Int)),
524+
"hybrid_prop_deeply_nested_list_int": graphene.List(graphene.List(graphene.List(graphene.Int))),
527525
"hybrid_prop_first_shopping_cart_item": ShoppingCartItemType,
528-
"hybrid_prop_shopping_cart_item_list": List(ShoppingCartItemType),
529-
"hybrid_prop_unsupported_type_tuple": String,
526+
"hybrid_prop_shopping_cart_item_list": graphene.List(ShoppingCartItemType),
527+
"hybrid_prop_unsupported_type_tuple": graphene.String,
530528
# Self Referential List
531529
"hybrid_prop_self_referential": ShoppingCartType,
532-
"hybrid_prop_self_referential_list": List(ShoppingCartType),
530+
"hybrid_prop_self_referential_list": graphene.List(ShoppingCartType),
533531
# Optionals
534532
"hybrid_prop_optional_self_referential": ShoppingCartType,
535533
}

0 commit comments

Comments
 (0)