Merge pull request #419 from yen223/fix-docstring-whitespace

Fix docstring whitespace
This commit is contained in:
Syrus Akbary 2017-02-22 19:52:21 -08:00 committed by GitHub
commit 62e58bd953
9 changed files with 53 additions and 7 deletions

View File

@ -3,6 +3,7 @@ from collections import OrderedDict
import six
from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .options import Options
from .unmountedtype import UnmountedType
@ -23,7 +24,7 @@ class EnumTypeMeta(type):
options = Options(
attrs.pop('Meta', None),
name=name,
description=attrs.get('__doc__'),
description=trim_docstring(attrs.get('__doc__')),
enum=None,
)
if not options.enum:

View File

@ -1,6 +1,7 @@
import six
from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .abstracttype import AbstractTypeMeta
from .inputfield import InputField
from .options import Options
@ -19,7 +20,7 @@ class InputObjectTypeMeta(AbstractTypeMeta):
options = Options(
attrs.pop('Meta', None),
name=name,
description=attrs.get('__doc__'),
description=trim_docstring(attrs.get('__doc__')),
local_fields=None,
)

View File

@ -1,6 +1,7 @@
import six
from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .abstracttype import AbstractTypeMeta
from .field import Field
from .options import Options
@ -18,7 +19,7 @@ class InterfaceMeta(AbstractTypeMeta):
options = Options(
attrs.pop('Meta', None),
name=name,
description=attrs.get('__doc__'),
description=trim_docstring(attrs.get('__doc__')),
local_fields=None,
)

View File

@ -3,6 +3,7 @@ from collections import OrderedDict
import six
from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .abstracttype import AbstractTypeMeta
from .field import Field
from .interface import Interface
@ -22,7 +23,7 @@ class ObjectTypeMeta(AbstractTypeMeta):
options = _meta or Options(
attrs.pop('Meta', None),
name=name,
description=attrs.get('__doc__'),
description=trim_docstring(attrs.get('__doc__')),
interfaces=(),
local_fields=OrderedDict(),
)

View File

@ -1,9 +1,9 @@
import six
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
StringValue)
from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .options import Options
from .unmountedtype import UnmountedType
@ -19,7 +19,7 @@ class ScalarTypeMeta(type):
options = Options(
attrs.pop('Meta', None),
name=name,
description=attrs.get('__doc__'),
description=trim_docstring(attrs.get('__doc__')),
)
return type.__new__(cls, name, bases, dict(attrs, _meta=options))

View File

@ -173,3 +173,14 @@ def test_objecttype_container_benchmark(benchmark):
@benchmark
def create_objecttype():
Container(field1='field1', field2='field2')
def test_generate_objecttype_description():
class MyObjectType(ObjectType):
'''
Documentation
Documentation line 2
'''
assert MyObjectType._meta.description == "Documentation\n\nDocumentation line 2"

View File

@ -1,6 +1,7 @@
import six
from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .options import Options
from .unmountedtype import UnmountedType
@ -16,7 +17,7 @@ class UnionMeta(type):
options = Options(
attrs.pop('Meta', None),
name=name,
description=attrs.get('__doc__'),
description=trim_docstring(attrs.get('__doc__')),
types=(),
)

View File

@ -0,0 +1,21 @@
from ..trim_docstring import trim_docstring
def test_trim_docstring():
class WellDocumentedObject(object):
"""
This object is very well-documented. It has multiple lines in its
description.
Multiple paragraphs too
"""
pass
assert (trim_docstring(WellDocumentedObject.__doc__) ==
"This object is very well-documented. It has multiple lines in its\n"
"description.\n\nMultiple paragraphs too")
class UndocumentedObject(object):
pass
assert trim_docstring(UndocumentedObject.__doc__) is None

View File

@ -0,0 +1,9 @@
import inspect
def trim_docstring(docstring):
# Cleans up whitespaces from an indented docstring
#
# See https://www.python.org/dev/peps/pep-0257/
# and https://docs.python.org/2/library/inspect.html#inspect.cleandoc
return inspect.cleandoc(docstring) if docstring else None