Skip to content

Commit 8116751

Browse files
committed
Use black autoformatting
1 parent 9e70ee7 commit 8116751

16 files changed

+432
-401
lines changed

graphene_sqlalchemy/__init__.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
from .types import (
2-
SQLAlchemyObjectType,
3-
)
4-
from .fields import (
5-
SQLAlchemyConnectionField
6-
)
7-
from .utils import (
8-
get_query,
9-
get_session
10-
)
1+
from .types import SQLAlchemyObjectType
2+
from .fields import SQLAlchemyConnectionField
3+
from .utils import get_query, get_session
114

12-
__version__ = '2.0.0'
5+
__version__ = "2.0.0"
136

147
__all__ = [
15-
'__version__',
16-
'SQLAlchemyObjectType',
17-
'SQLAlchemyConnectionField',
18-
'get_query',
19-
'get_session'
8+
"__version__",
9+
"SQLAlchemyObjectType",
10+
"SQLAlchemyConnectionField",
11+
"get_query",
12+
"get_session",
2013
]

graphene_sqlalchemy/converter.py

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
from sqlalchemy.dialects import postgresql
44
from sqlalchemy.orm import interfaces
55

6-
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
7-
String)
6+
from graphene import ID, Boolean, Dynamic, Enum, Field, Float, Int, List, String
87
from graphene.types.json import JSONString
98

109
from .fields import createConnectionField
1110

1211
try:
13-
from sqlalchemy_utils import (
14-
ChoiceType, JSONType, ScalarListType, TSVectorType)
12+
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, TSVectorType
1513
except ImportError:
1614
ChoiceType = JSONType = ScalarListType = TSVectorType = object
1715

1816

1917
def get_column_doc(column):
20-
return getattr(column, 'doc', None)
18+
return getattr(column, "doc", None)
2119

2220

2321
def is_column_nullable(column):
24-
return bool(getattr(column, 'nullable', True))
22+
return bool(getattr(column, "nullable", True))
2523

2624

2725
def convert_sqlalchemy_relationship(relationship, registry):
@@ -43,46 +41,51 @@ def dynamic_type():
4341

4442

4543
def convert_sqlalchemy_hybrid_method(hybrid_item):
46-
return String(description=getattr(hybrid_item, '__doc__', None),
47-
required=False)
44+
return String(description=getattr(hybrid_item, "__doc__", None), required=False)
4845

4946

5047
def convert_sqlalchemy_composite(composite, registry):
5148
converter = registry.get_converter_for_composite(composite.composite_class)
5249
if not converter:
5350
try:
5451
raise Exception(
55-
"Don't know how to convert the composite field %s (%s)" %
56-
(composite, composite.composite_class))
52+
"Don't know how to convert the composite field %s (%s)"
53+
% (composite, composite.composite_class)
54+
)
5755
except AttributeError:
5856
# handle fields that are not attached to a class yet (don't have a parent)
5957
raise Exception(
60-
"Don't know how to convert the composite field %r (%s)" %
61-
(composite, composite.composite_class))
58+
"Don't know how to convert the composite field %r (%s)"
59+
% (composite, composite.composite_class)
60+
)
6261
return converter(composite, registry)
6362

6463

6564
def _register_composite_class(cls, registry=None):
6665
if registry is None:
6766
from .registry import get_global_registry
67+
6868
registry = get_global_registry()
6969

7070
def inner(fn):
7171
registry.register_composite_converter(cls, fn)
72+
7273
return inner
7374

7475

7576
convert_sqlalchemy_composite.register = _register_composite_class
7677

7778

7879
def convert_sqlalchemy_column(column, registry=None):
79-
return convert_sqlalchemy_type(getattr(column, 'type', None), column, registry)
80+
return convert_sqlalchemy_type(getattr(column, "type", None), column, registry)
8081

8182

8283
@singledispatch
8384
def convert_sqlalchemy_type(type, column, registry=None):
8485
raise Exception(
85-
"Don't know how to convert the SQLAlchemy field %s (%s)" % (column, column.__class__))
86+
"Don't know how to convert the SQLAlchemy field %s (%s)"
87+
% (column, column.__class__)
88+
)
8689

8790

8891
@convert_sqlalchemy_type.register(types.Date)
@@ -96,40 +99,49 @@ def convert_sqlalchemy_type(type, column, registry=None):
9699
@convert_sqlalchemy_type.register(postgresql.CIDR)
97100
@convert_sqlalchemy_type.register(TSVectorType)
98101
def convert_column_to_string(type, column, registry=None):
99-
return String(description=get_column_doc(column),
100-
required=not(is_column_nullable(column)))
102+
return String(
103+
description=get_column_doc(column), required=not (is_column_nullable(column))
104+
)
101105

102106

103107
@convert_sqlalchemy_type.register(types.DateTime)
104108
def convert_column_to_datetime(type, column, registry=None):
105109
from graphene.types.datetime import DateTime
106-
return DateTime(description=get_column_doc(column),
107-
required=not(is_column_nullable(column)))
110+
111+
return DateTime(
112+
description=get_column_doc(column), required=not (is_column_nullable(column))
113+
)
108114

109115

110116
@convert_sqlalchemy_type.register(types.SmallInteger)
111117
@convert_sqlalchemy_type.register(types.Integer)
112118
def convert_column_to_int_or_id(type, column, registry=None):
113119
if column.primary_key:
114-
return ID(description=get_column_doc(column),
115-
required=not (is_column_nullable(column)))
120+
return ID(
121+
description=get_column_doc(column),
122+
required=not (is_column_nullable(column)),
123+
)
116124
else:
117-
return Int(description=get_column_doc(column),
118-
required=not (is_column_nullable(column)))
125+
return Int(
126+
description=get_column_doc(column),
127+
required=not (is_column_nullable(column)),
128+
)
119129

120130

121131
@convert_sqlalchemy_type.register(types.Boolean)
122132
def convert_column_to_boolean(type, column, registry=None):
123-
return Boolean(description=get_column_doc(column),
124-
required=not(is_column_nullable(column)))
133+
return Boolean(
134+
description=get_column_doc(column), required=not (is_column_nullable(column))
135+
)
125136

126137

127138
@convert_sqlalchemy_type.register(types.Float)
128139
@convert_sqlalchemy_type.register(types.Numeric)
129140
@convert_sqlalchemy_type.register(types.BigInteger)
130141
def convert_column_to_float(type, column, registry=None):
131-
return Float(description=get_column_doc(column),
132-
required=not(is_column_nullable(column)))
142+
return Float(
143+
description=get_column_doc(column), required=not (is_column_nullable(column))
144+
)
133145

134146

135147
@convert_sqlalchemy_type.register(types.Enum)
@@ -138,14 +150,16 @@ def convert_enum_to_enum(type, column, registry=None):
138150
items = type.enum_class.__members__.items()
139151
except AttributeError:
140152
items = zip(type.enums, type.enums)
141-
return Field(Enum(type.name, items),
142-
description=get_column_doc(column),
143-
required=not(is_column_nullable(column)))
153+
return Field(
154+
Enum(type.name, items),
155+
description=get_column_doc(column),
156+
required=not (is_column_nullable(column)),
157+
)
144158

145159

146160
@convert_sqlalchemy_type.register(ChoiceType)
147161
def convert_column_to_enum(type, column, registry=None):
148-
name = '{}_{}'.format(column.table.name, column.name).upper()
162+
name = "{}_{}".format(column.table.name, column.name).upper()
149163
return Enum(name, type.choices, description=get_column_doc(column))
150164

151165

@@ -158,19 +172,24 @@ def convert_scalar_list_to_list(type, column, registry=None):
158172
def convert_postgres_array_to_list(_type, column, registry=None):
159173
graphene_type = convert_sqlalchemy_type(column.type.item_type, column)
160174
inner_type = type(graphene_type)
161-
return List(inner_type, description=get_column_doc(column),
162-
required=not(is_column_nullable(column)))
175+
return List(
176+
inner_type,
177+
description=get_column_doc(column),
178+
required=not (is_column_nullable(column)),
179+
)
163180

164181

165182
@convert_sqlalchemy_type.register(postgresql.HSTORE)
166183
@convert_sqlalchemy_type.register(postgresql.JSON)
167184
@convert_sqlalchemy_type.register(postgresql.JSONB)
168185
def convert_json_to_string(type, column, registry=None):
169-
return JSONString(description=get_column_doc(column),
170-
required=not(is_column_nullable(column)))
186+
return JSONString(
187+
description=get_column_doc(column), required=not (is_column_nullable(column))
188+
)
171189

172190

173191
@convert_sqlalchemy_type.register(JSONType)
174192
def convert_json_type_to_string(type, column, registry=None):
175-
return JSONString(description=get_column_doc(column),
176-
required=not(is_column_nullable(column)))
193+
return JSONString(
194+
description=get_column_doc(column), required=not (is_column_nullable(column))
195+
)

graphene_sqlalchemy/fields.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010

1111

1212
class UnsortedSQLAlchemyConnectionField(ConnectionField):
13-
1413
@property
1514
def type(self):
1615
from .types import SQLAlchemyObjectType
16+
1717
_type = super(ConnectionField, self).type
1818
if issubclass(_type, Connection):
1919
return _type
2020
assert issubclass(_type, SQLAlchemyObjectType), (
2121
"SQLALchemyConnectionField only accepts SQLAlchemyObjectType types, not {}"
2222
).format(_type.__name__)
23-
assert _type._meta.connection, "The type {} doesn't have a connection".format(_type.__name__)
23+
assert _type._meta.connection, "The type {} doesn't have a connection".format(
24+
_type.__name__
25+
)
2426
return _type._meta.connection
2527

2628
@property
@@ -74,20 +76,21 @@ def get_resolver(self, parent_resolver):
7476

7577

7678
class SQLAlchemyConnectionField(UnsortedSQLAlchemyConnectionField):
77-
7879
def __init__(self, type, *args, **kwargs):
79-
if 'sort' not in kwargs and issubclass(type, Connection):
80+
if "sort" not in kwargs and issubclass(type, Connection):
8081
# Let super class raise if type is not a Connection
8182
try:
8283
model = type.Edge.node._type._meta.model
83-
kwargs.setdefault('sort', sort_argument_for_model(model))
84+
kwargs.setdefault("sort", sort_argument_for_model(model))
8485
except Exception:
8586
raise Exception(
8687
'Cannot create sort argument for {}. A model is required. Set the "sort" argument'
87-
' to None to disabling the creation of the sort query argument'.format(type.__name__)
88+
" to None to disabling the creation of the sort query argument".format(
89+
type.__name__
90+
)
8891
)
89-
elif 'sort' in kwargs and kwargs['sort'] is None:
90-
del kwargs['sort']
92+
elif "sort" in kwargs and kwargs["sort"] is None:
93+
del kwargs["sort"]
9194
super(SQLAlchemyConnectionField, self).__init__(type, *args, **kwargs)
9295

9396

graphene_sqlalchemy/registry.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Registry(object):
2-
32
def __init__(self):
43
self._registry = {}
54
self._registry_models = {}
65
self._registry_composites = {}
76

87
def register(self, cls):
98
from .types import SQLAlchemyObjectType
9+
1010
assert issubclass(cls, SQLAlchemyObjectType), (
11-
'Only classes of type SQLAlchemyObjectType can be registered, '
11+
"Only classes of type SQLAlchemyObjectType can be registered, "
1212
'received "{}"'
1313
).format(cls.__name__)
14-
assert cls._meta.registry == self, 'Registry for a Model have to match.'
14+
assert cls._meta.registry == self, "Registry for a Model have to match."
1515
# assert self.get_type_for_model(cls._meta.model) in [None, cls], (
1616
# 'SQLAlchemy model "{}" already associated with '
1717
# 'another type "{}".'

graphene_sqlalchemy/tests/models.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,36 @@
88

99
Base = declarative_base()
1010

11-
association_table = Table('association', Base.metadata,
12-
Column('pet_id', Integer, ForeignKey('pets.id')),
13-
Column('reporter_id', Integer, ForeignKey('reporters.id')))
11+
association_table = Table(
12+
"association",
13+
Base.metadata,
14+
Column("pet_id", Integer, ForeignKey("pets.id")),
15+
Column("reporter_id", Integer, ForeignKey("reporters.id")),
16+
)
1417

1518

1619
class Editor(Base):
17-
__tablename__ = 'editors'
20+
__tablename__ = "editors"
1821
editor_id = Column(Integer(), primary_key=True)
1922
name = Column(String(100))
2023

2124

2225
class Pet(Base):
23-
__tablename__ = 'pets'
26+
__tablename__ = "pets"
2427
id = Column(Integer(), primary_key=True)
2528
name = Column(String(30))
26-
pet_kind = Column(Enum('cat', 'dog', name='pet_kind'), nullable=False)
27-
reporter_id = Column(Integer(), ForeignKey('reporters.id'))
29+
pet_kind = Column(Enum("cat", "dog", name="pet_kind"), nullable=False)
30+
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
2831

2932

3033
class Reporter(Base):
31-
__tablename__ = 'reporters'
34+
__tablename__ = "reporters"
3235
id = Column(Integer(), primary_key=True)
3336
first_name = Column(String(30))
3437
last_name = Column(String(30))
3538
email = Column(String())
36-
pets = relationship('Pet', secondary=association_table, backref='reporters')
37-
articles = relationship('Article', backref='reporter')
39+
pets = relationship("Pet", secondary=association_table, backref="reporters")
40+
articles = relationship("Article", backref="reporter")
3841
favorite_article = relationship("Article", uselist=False)
3942

4043
# total = column_property(
@@ -45,19 +48,21 @@ class Reporter(Base):
4548

4649

4750
class Article(Base):
48-
__tablename__ = 'articles'
51+
__tablename__ = "articles"
4952
id = Column(Integer(), primary_key=True)
5053
headline = Column(String(100))
5154
pub_date = Column(Date())
52-
reporter_id = Column(Integer(), ForeignKey('reporters.id'))
55+
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
5356

5457

5558
class ReflectedEditor(type):
5659
"""Same as Editor, but using reflected table."""
60+
5761
@classmethod
5862
def __subclasses__(cls):
5963
return []
6064

61-
editor_table = Table('editors', Base.metadata, autoload=True)
65+
66+
editor_table = Table("editors", Base.metadata, autoload=True)
6267

6368
mapper(ReflectedEditor, editor_table)

0 commit comments

Comments
 (0)