Skip to content

Commit a47dbb3

Browse files
authored
Pick up the docstrings of hybrid properties (#344)
1 parent b0aa63c commit a47dbb3

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

graphene_sqlalchemy/converter.py

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def convert_sqlalchemy_hybrid_method(hybrid_prop, resolver, **field_kwargs):
130130
if 'type_' not in field_kwargs:
131131
field_kwargs['type_'] = convert_hybrid_property_return_type(hybrid_prop)
132132

133+
if 'description' not in field_kwargs:
134+
field_kwargs['description'] = getattr(hybrid_prop, "__doc__", None)
135+
133136
return Field(
134137
resolver=resolver,
135138
**field_kwargs

graphene_sqlalchemy/tests/models.py

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Reporter(Base):
6868
articles = relationship("Article", backref="reporter")
6969
favorite_article = relationship("Article", uselist=False)
7070

71+
@hybrid_property
72+
def hybrid_prop_with_doc(self):
73+
"""Docstring test"""
74+
return self.first_name
75+
7176
@hybrid_property
7277
def hybrid_prop(self):
7378
return self.first_name

graphene_sqlalchemy/tests/test_types.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Meta:
8585
# Composite
8686
"composite_prop",
8787
# Hybrid
88+
"hybrid_prop_with_doc",
8889
"hybrid_prop",
8990
"hybrid_prop_str",
9091
"hybrid_prop_int",
@@ -150,6 +151,12 @@ class Meta:
150151
# "doc" is ignored by hybrid_property
151152
assert hybrid_prop_list.description is None
152153

154+
# hybrid_prop_with_doc
155+
hybrid_prop_with_doc = ReporterType._meta.fields['hybrid_prop_with_doc']
156+
assert hybrid_prop_with_doc.type == String
157+
# docstring is picked up from hybrid_prop_with_doc
158+
assert hybrid_prop_with_doc.description == "Docstring test"
159+
153160
# relationship
154161
favorite_article_field = ReporterType._meta.fields['favorite_article']
155162
assert isinstance(favorite_article_field, Dynamic)
@@ -183,6 +190,7 @@ class Meta:
183190
composite_prop = ORMField()
184191

185192
# hybrid_property
193+
hybrid_prop_with_doc = ORMField(description='Overridden')
186194
hybrid_prop = ORMField(description='Overridden')
187195

188196
# relationships
@@ -210,6 +218,7 @@ class Meta:
210218
"email_v2",
211219
"column_prop",
212220
"composite_prop",
221+
"hybrid_prop_with_doc",
213222
"hybrid_prop",
214223
"favorite_article",
215224
"articles",
@@ -250,6 +259,11 @@ class Meta:
250259
assert hybrid_prop_field.description == "Overridden"
251260
assert hybrid_prop_field.deprecation_reason is None
252261

262+
hybrid_prop_with_doc_field = ReporterType._meta.fields['hybrid_prop_with_doc']
263+
assert hybrid_prop_with_doc_field.type == String
264+
assert hybrid_prop_with_doc_field.description == "Overridden"
265+
assert hybrid_prop_with_doc_field.deprecation_reason is None
266+
253267
column_prop_field_v2 = ReporterType._meta.fields['column_prop']
254268
assert column_prop_field_v2.type == String
255269
assert column_prop_field_v2.description is None
@@ -318,6 +332,7 @@ class Meta:
318332
"email",
319333
"favorite_pet_kind",
320334
"composite_prop",
335+
"hybrid_prop_with_doc",
321336
"hybrid_prop",
322337
"hybrid_prop_str",
323338
"hybrid_prop_int",
@@ -432,7 +447,7 @@ class Meta:
432447

433448
assert issubclass(CustomReporterType, ObjectType)
434449
assert CustomReporterType._meta.model == Reporter
435-
assert len(CustomReporterType._meta.fields) == 16
450+
assert len(CustomReporterType._meta.fields) == 17
436451

437452

438453
# Test Custom SQLAlchemyObjectType with Custom Options

0 commit comments

Comments
 (0)