Skip to content

Commit

Permalink
Pick up the docstrings of hybrid properties
Browse files Browse the repository at this point in the history
Use the docstring of hybrid properties as the description.

Bumped build requirments for sqlalchemy becase as noted in the
documentation before version 1.3.19 the ordering for
all_orm_descriptors was not deterministic:

```
.. versionchanged:: 1.3.19 ensured deterministic ordering for
   :meth:`_orm.Mapper.all_orm_descriptors`.
```
  • Loading branch information
bim9262 committed Feb 3, 2021
1 parent 20ecaea commit 377ee77
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions graphene_sqlalchemy/converter.py
Expand Up @@ -114,6 +114,9 @@ def convert_sqlalchemy_hybrid_method(hybrid_prop, resolver, **field_kwargs):
# TODO The default type should be dependent on the type of the property propety.
field_kwargs['type'] = String

if 'description' not in field_kwargs:
field_kwargs['description'] = getattr(hybrid_prop, "__doc__", None)

return Field(
resolver=resolver,
**field_kwargs
Expand Down
5 changes: 5 additions & 0 deletions graphene_sqlalchemy/tests/models.py
Expand Up @@ -65,6 +65,11 @@ class Reporter(Base):
articles = relationship("Article", backref="reporter")
favorite_article = relationship("Article", uselist=False)

@hybrid_property
def hybrid_prop_with_doc(self):
"""Docstring test"""
return self.first_name

@hybrid_property
def hybrid_prop(self):
return self.first_name
Expand Down
17 changes: 16 additions & 1 deletion graphene_sqlalchemy/tests/test_types.py
Expand Up @@ -82,6 +82,7 @@ class Meta:
# Composite
"composite_prop",
# Hybrid
"hybrid_prop_with_doc",
"hybrid_prop",
# Relationship
"pets",
Expand Down Expand Up @@ -112,6 +113,12 @@ class Meta:
# "doc" is ignored by hybrid_property
assert hybrid_prop.description is None

# hybrid_prop_with_doc
hybrid_prop_with_doc = ReporterType._meta.fields['hybrid_prop_with_doc']
assert hybrid_prop_with_doc.type == String
# docstring is picked up from hybrid_prop_with_doc
assert hybrid_prop_with_doc.description == "Docstring test"

# relationship
favorite_article_field = ReporterType._meta.fields['favorite_article']
assert isinstance(favorite_article_field, Dynamic)
Expand Down Expand Up @@ -145,6 +152,7 @@ class Meta:
composite_prop = ORMField()

# hybrid_property
hybrid_prop_with_doc = ORMField(description='Overridden')
hybrid_prop = ORMField(description='Overridden')

# relationships
Expand Down Expand Up @@ -172,6 +180,7 @@ class Meta:
"email_v2",
"column_prop",
"composite_prop",
"hybrid_prop_with_doc",
"hybrid_prop",
"favorite_article",
"articles",
Expand Down Expand Up @@ -207,6 +216,11 @@ class Meta:
assert hybrid_prop_field.description == "Overridden"
assert hybrid_prop_field.deprecation_reason is None

hybrid_prop_with_doc_field = ReporterType._meta.fields['hybrid_prop_with_doc']
assert hybrid_prop_with_doc_field.type == String
assert hybrid_prop_with_doc_field.description == "Overridden"
assert hybrid_prop_with_doc_field.deprecation_reason is None

column_prop_field_v2 = ReporterType._meta.fields['column_prop']
assert column_prop_field_v2.type == String
assert column_prop_field_v2.description is None
Expand Down Expand Up @@ -275,6 +289,7 @@ class Meta:
"email",
"favorite_pet_kind",
"composite_prop",
"hybrid_prop_with_doc",
"hybrid_prop",
"pets",
"articles",
Expand Down Expand Up @@ -384,7 +399,7 @@ class Meta:

assert issubclass(CustomReporterType, ObjectType)
assert CustomReporterType._meta.model == Reporter
assert len(CustomReporterType._meta.fields) == 11
assert len(CustomReporterType._meta.fields) == 12


# Test Custom SQLAlchemyObjectType with Custom Options
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -15,8 +15,8 @@
# To keep things simple, we only support newer versions of Graphene
"graphene>=2.1.3,<3",
"promise>=2.3",
# Tests fail with 1.0.19
"SQLAlchemy>=1.2,<2",
# Tests fail with 1.3.18
"SQLAlchemy>=1.3.19,<2",
"six>=1.10.0,<2",
"singledispatch>=3.4.0.3,<4",
]
Expand Down
6 changes: 2 additions & 4 deletions tox.ini
@@ -1,14 +1,12 @@
[tox]
envlist = pre-commit,py{27,35,36,37}-sql{11,12,13}
envlist = pre-commit,py{27,35,36,37}-sql13
skipsdist = true
minversion = 3.7.0

[testenv]
deps =
.[test]
sql11: sqlalchemy>=1.1,<1.2
sql12: sqlalchemy>=1.2,<1.3
sql13: sqlalchemy>=1.3,<1.4
sql13: sqlalchemy>=1.3.19,<1.4
commands =
pytest graphene_sqlalchemy --cov=graphene_sqlalchemy {posargs}

Expand Down

0 comments on commit 377ee77

Please sign in to comment.