Skip to content

Commit 62e58bd

Browse files
authored
Merge pull request #419 from yen223/fix-docstring-whitespace
Fix docstring whitespace
2 parents 215d131 + c592e94 commit 62e58bd

File tree

9 files changed

+53
-7
lines changed

9 files changed

+53
-7
lines changed

graphene/types/enum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import six
44

55
from ..utils.is_base_type import is_base_type
6+
from ..utils.trim_docstring import trim_docstring
67
from .options import Options
78
from .unmountedtype import UnmountedType
89

@@ -23,7 +24,7 @@ def __new__(cls, name, bases, attrs):
2324
options = Options(
2425
attrs.pop('Meta', None),
2526
name=name,
26-
description=attrs.get('__doc__'),
27+
description=trim_docstring(attrs.get('__doc__')),
2728
enum=None,
2829
)
2930
if not options.enum:

graphene/types/inputobjecttype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import six
22

33
from ..utils.is_base_type import is_base_type
4+
from ..utils.trim_docstring import trim_docstring
45
from .abstracttype import AbstractTypeMeta
56
from .inputfield import InputField
67
from .options import Options
@@ -19,7 +20,7 @@ def __new__(cls, name, bases, attrs):
1920
options = Options(
2021
attrs.pop('Meta', None),
2122
name=name,
22-
description=attrs.get('__doc__'),
23+
description=trim_docstring(attrs.get('__doc__')),
2324
local_fields=None,
2425
)
2526

graphene/types/interface.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import six
22

33
from ..utils.is_base_type import is_base_type
4+
from ..utils.trim_docstring import trim_docstring
45
from .abstracttype import AbstractTypeMeta
56
from .field import Field
67
from .options import Options
@@ -18,7 +19,7 @@ def __new__(cls, name, bases, attrs):
1819
options = Options(
1920
attrs.pop('Meta', None),
2021
name=name,
21-
description=attrs.get('__doc__'),
22+
description=trim_docstring(attrs.get('__doc__')),
2223
local_fields=None,
2324
)
2425

graphene/types/objecttype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import six
44

55
from ..utils.is_base_type import is_base_type
6+
from ..utils.trim_docstring import trim_docstring
67
from .abstracttype import AbstractTypeMeta
78
from .field import Field
89
from .interface import Interface
@@ -22,7 +23,7 @@ def __new__(cls, name, bases, attrs):
2223
options = _meta or Options(
2324
attrs.pop('Meta', None),
2425
name=name,
25-
description=attrs.get('__doc__'),
26+
description=trim_docstring(attrs.get('__doc__')),
2627
interfaces=(),
2728
local_fields=OrderedDict(),
2829
)

graphene/types/scalars.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import six
2-
32
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
43
StringValue)
54

65
from ..utils.is_base_type import is_base_type
6+
from ..utils.trim_docstring import trim_docstring
77
from .options import Options
88
from .unmountedtype import UnmountedType
99

@@ -19,7 +19,7 @@ def __new__(cls, name, bases, attrs):
1919
options = Options(
2020
attrs.pop('Meta', None),
2121
name=name,
22-
description=attrs.get('__doc__'),
22+
description=trim_docstring(attrs.get('__doc__')),
2323
)
2424

2525
return type.__new__(cls, name, bases, dict(attrs, _meta=options))

graphene/types/tests/test_objecttype.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,14 @@ def test_objecttype_container_benchmark(benchmark):
173173
@benchmark
174174
def create_objecttype():
175175
Container(field1='field1', field2='field2')
176+
177+
178+
def test_generate_objecttype_description():
179+
class MyObjectType(ObjectType):
180+
'''
181+
Documentation
182+
183+
Documentation line 2
184+
'''
185+
186+
assert MyObjectType._meta.description == "Documentation\n\nDocumentation line 2"

graphene/types/union.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import six
22

33
from ..utils.is_base_type import is_base_type
4+
from ..utils.trim_docstring import trim_docstring
45
from .options import Options
56
from .unmountedtype import UnmountedType
67

@@ -16,7 +17,7 @@ def __new__(cls, name, bases, attrs):
1617
options = Options(
1718
attrs.pop('Meta', None),
1819
name=name,
19-
description=attrs.get('__doc__'),
20+
description=trim_docstring(attrs.get('__doc__')),
2021
types=(),
2122
)
2223

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from ..trim_docstring import trim_docstring
2+
3+
4+
def test_trim_docstring():
5+
class WellDocumentedObject(object):
6+
"""
7+
This object is very well-documented. It has multiple lines in its
8+
description.
9+
10+
Multiple paragraphs too
11+
"""
12+
pass
13+
14+
assert (trim_docstring(WellDocumentedObject.__doc__) ==
15+
"This object is very well-documented. It has multiple lines in its\n"
16+
"description.\n\nMultiple paragraphs too")
17+
18+
class UndocumentedObject(object):
19+
pass
20+
21+
assert trim_docstring(UndocumentedObject.__doc__) is None

graphene/utils/trim_docstring.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import inspect
2+
3+
4+
def trim_docstring(docstring):
5+
# Cleans up whitespaces from an indented docstring
6+
#
7+
# See https://www.python.org/dev/peps/pep-0257/
8+
# and https://docs.python.org/2/library/inspect.html#inspect.cleandoc
9+
return inspect.cleandoc(docstring) if docstring else None

0 commit comments

Comments
 (0)