prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  `experimental` decorator adds extra indents to docs
  An extra indent appears in the docs when the [`experimental`](https://github.com/optuna/optuna/blob/2d5f24b06eed56ece72b8dfa878135bb4bb63779/optuna/_experimental.py#L60) decorator is added to a method of a class. This does not happen with a top level (i.e., not nested) class or function.
  
  ## Steps to reproduce
  
  1. add an `experimental` decorator to a method of a class
  2. make the document
  
  ## Reproducible examples
  The following figure shows the docs of [`Study.enqueue_trial`](https://optuna.readthedocs.io/en/latest/reference/study.html#optuna.study.Study.enqueue_trial). An extra indent appears after the second paragraph.
  
  ![image](https://user-images.githubusercontent.com/1061922/81144389-e6f32f80-8fae-11ea-8dd2-33368293dafa.png)
  
  ## Expected behavior
  no extra indent is shown after the second paragraph
  
  For example, the docs of [`Study.get_trials`](https://optuna.readthedocs.io/en/latest/reference/study.html#optuna.study.Study.get_trials), which does not have the `experimental` decorator, appears as expected.
  
  ![image](https://user-images.githubusercontent.com/1061922/81143489-f1142e80-8fac-11ea-9896-e56086228168.png)
  
  The following figure shows another example. The docs of the [`train`](https://optuna.readthedocs.io/en/latest/reference/integration.html#optuna.integration.lightgbm.train) function of LightGBMTuner has no extra indent. Although the function has an `experimental` decorator, it belongs to the top level classes.
  
  ![image](https://user-images.githubusercontent.com/1061922/81146606-98945f80-8fb3-11ea-8d67-5cf1cfbf8768.png)
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `optuna/_experimental.py`
  Content:
  ```
  1 import functools
  2 import inspect
  3 from typing import Any
  4 from typing import Callable
  5 import warnings
  6 
  7 from optuna.exceptions import ExperimentalWarning
  8 
  9 
  10 # White spaces of each line are necessary to beautifully rendered documentation.
  11 # NOTE(crcrpar): When `experimental` decorator is applied to member methods, these lines require
  12 # another four spaces.
  13 _EXPERIMENTAL_DOCSTRING_TEMPLATE = """
  14 
  15     .. note::
  16         Added in v{ver} as an experimental feature. The interface may change in newer versions
  17         without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.
  18 """
  19 
  20 
  21 def _validate_version(version: str) -> None:
  22 
  23     if not isinstance(version, str) or len(version.split(".")) != 3:
  24         raise ValueError(
  25             "Invalid version specification. Must follow `x.y.z` format but `{}` is given".format(
  26                 version
  27             )
  28         )
  29 
  30 
  31 def experimental(version: str, name: str = None) -> Any:
  32     """Decorate class or function as experimental.
  33 
  34     Args:
  35         version: The first version that supports the target feature.
  36         name: The name of the feature. Defaults to the function or class name. Optional.
  37     """
  38 
  39     _validate_version(version)
  40 
  41     def _experimental_wrapper(f: Any) -> Any:
  42         # f is either func or class.
  43 
  44         def _experimental_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]:
  45 
  46             docstring = _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)
  47             if func.__doc__ is None:
  48                 func.__doc__ = ""
  49             func.__doc__ += docstring
  50 
  51             # TODO(crcrpar): Annotate this correctly.
  52             @functools.wraps(func)
  53             def new_func(*args: Any, **kwargs: Any) -> Any:
  54                 """Wrapped function."""
  55 
  56                 warnings.warn(
  57                     "{} is experimental (supported from v{}). "
  58                     "The interface can change in the future.".format(
  59                         name if name is not None else func.__name__, version
  60                     ),
  61                     ExperimentalWarning,
  62                 )
  63 
  64                 return func(*args, **kwargs)  # type: ignore
  65 
  66             return new_func
  67 
  68         def _experimental_class(cls: Any) -> Any:
  69             """Decorates a class as experimental.
  70 
  71             This decorator is supposed to be applied to the experimental class.
  72             """
  73 
  74             _original_init = cls.__init__
  75 
  76             @functools.wraps(_original_init)
  77             def wrapped_init(self, *args, **kwargs) -> None:  # type: ignore
  78                 warnings.warn(
  79                     "{} is experimental (supported from v{}). "
  80                     "The interface can change in the future.".format(
  81                         name if name is not None else cls.__name__, version
  82                     ),
  83                     ExperimentalWarning,
  84                 )
  85 
  86                 _original_init(self, *args, **kwargs)
  87 
  88             cls.__init__ = wrapped_init
  89 
  90             if cls.__doc__ is None:
  91                 cls.__doc__ = ""
  92             cls.__doc__ += _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)
  93             return cls
  94 
  95         return _experimental_class(f) if inspect.isclass(f) else _experimental_func(f)
  96 
  97     return _experimental_wrapper
  98
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/optuna/_experimental.py b/optuna/_experimental.py
  --- a/optuna/_experimental.py
  +++ b/optuna/_experimental.py
  @@ -1,5 +1,6 @@
   import functools
   import inspect
  +import textwrap
   from typing import Any
   from typing import Callable
   import warnings
  @@ -7,14 +8,11 @@
   from optuna.exceptions import ExperimentalWarning
   
   
  -# White spaces of each line are necessary to beautifully rendered documentation.
  -# NOTE(crcrpar): When `experimental` decorator is applied to member methods, these lines require
  -# another four spaces.
  -_EXPERIMENTAL_DOCSTRING_TEMPLATE = """
  +_EXPERIMENTAL_NOTE_TEMPLATE = """
   
  -    .. note::
  -        Added in v{ver} as an experimental feature. The interface may change in newer versions
  -        without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.
  +.. note::
  +    Added in v{ver} as an experimental feature. The interface may change in newer versions
  +    without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.
   """
   
   
  @@ -28,6 +26,10 @@
           )
   
   
  +def _get_docstring_indent(docstring: str) -> str:
  +    return docstring.split("\n")[-1] if "\n" in docstring else ""
  +
  +
   def experimental(version: str, name: str = None) -> Any:
       """Decorate class or function as experimental.
   
  @@ -43,16 +45,16 @@
   
           def _experimental_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]:
   
  -            docstring = _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)
               if func.__doc__ is None:
                   func.__doc__ = ""
  -            func.__doc__ += docstring
  +
  +            note = _EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)
  +            indent = _get_docstring_indent(func.__doc__)
  +            func.__doc__ = func.__doc__.strip() + textwrap.indent(note, indent) + indent
   
               # TODO(crcrpar): Annotate this correctly.
               @functools.wraps(func)
               def new_func(*args: Any, **kwargs: Any) -> Any:
  -                """Wrapped function."""
  -
                   warnings.warn(
                       "{} is experimental (supported from v{}). "
                       "The interface can change in the future.".format(
  @@ -70,7 +72,6 @@
   
               This decorator is supposed to be applied to the experimental class.
               """
  -
               _original_init = cls.__init__
   
               @functools.wraps(_original_init)
  @@ -89,7 +90,11 @@
   
               if cls.__doc__ is None:
                   cls.__doc__ = ""
  -            cls.__doc__ += _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)
  +
  +            note = _EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)
  +            indent = _get_docstring_indent(cls.__doc__)
  +            cls.__doc__ = cls.__doc__.strip() + textwrap.indent(note, indent) + indent
  +
               return cls
   
           return _experimental_class(f) if inspect.isclass(f) else _experimental_func(f)
  
verification_info: {"golden_diff": "diff --git a/optuna/_experimental.py b/optuna/_experimental.py\n--- a/optuna/_experimental.py\n+++ b/optuna/_experimental.py\n@@ -1,5 +1,6 @@\n import functools\n import inspect\n+import textwrap\n from typing import Any\n from typing import Callable\n import warnings\n@@ -7,14 +8,11 @@\n from optuna.exceptions import ExperimentalWarning\n \n \n-# White spaces of each line are necessary to beautifully rendered documentation.\n-# NOTE(crcrpar): When `experimental` decorator is applied to member methods, these lines require\n-# another four spaces.\n-_EXPERIMENTAL_DOCSTRING_TEMPLATE = \"\"\"\n+_EXPERIMENTAL_NOTE_TEMPLATE = \"\"\"\n \n-    .. note::\n-        Added in v{ver} as an experimental feature. The interface may change in newer versions\n-        without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.\n+.. note::\n+    Added in v{ver} as an experimental feature. The interface may change in newer versions\n+    without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.\n \"\"\"\n \n \n@@ -28,6 +26,10 @@\n         )\n \n \n+def _get_docstring_indent(docstring: str) -> str:\n+    return docstring.split(\"\\n\")[-1] if \"\\n\" in docstring else \"\"\n+\n+\n def experimental(version: str, name: str = None) -> Any:\n     \"\"\"Decorate class or function as experimental.\n \n@@ -43,16 +45,16 @@\n \n         def _experimental_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]:\n \n-            docstring = _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)\n             if func.__doc__ is None:\n                 func.__doc__ = \"\"\n-            func.__doc__ += docstring\n+\n+            note = _EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)\n+            indent = _get_docstring_indent(func.__doc__)\n+            func.__doc__ = func.__doc__.strip() + textwrap.indent(note, indent) + indent\n \n             # TODO(crcrpar): Annotate this correctly.\n             @functools.wraps(func)\n             def new_func(*args: Any, **kwargs: Any) -> Any:\n-                \"\"\"Wrapped function.\"\"\"\n-\n                 warnings.warn(\n                     \"{} is experimental (supported from v{}). \"\n                     \"The interface can change in the future.\".format(\n@@ -70,7 +72,6 @@\n \n             This decorator is supposed to be applied to the experimental class.\n             \"\"\"\n-\n             _original_init = cls.__init__\n \n             @functools.wraps(_original_init)\n@@ -89,7 +90,11 @@\n \n             if cls.__doc__ is None:\n                 cls.__doc__ = \"\"\n-            cls.__doc__ += _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)\n+\n+            note = _EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)\n+            indent = _get_docstring_indent(cls.__doc__)\n+            cls.__doc__ = cls.__doc__.strip() + textwrap.indent(note, indent) + indent\n+\n             return cls\n \n         return _experimental_class(f) if inspect.isclass(f) else _experimental_func(f)\n", "issue": "`experimental` decorator adds extra indents to docs\nAn extra indent appears in the docs when the [`experimental`](https://github.com/optuna/optuna/blob/2d5f24b06eed56ece72b8dfa878135bb4bb63779/optuna/_experimental.py#L60) decorator is added to a method of a class. This does not happen with a top level (i.e., not nested) class or function.\r\n\r\n## Steps to reproduce\r\n\r\n1. add an `experimental` decorator to a method of a class\r\n2. make the document\r\n\r\n## Reproducible examples\r\nThe following figure shows the docs of [`Study.enqueue_trial`](https://optuna.readthedocs.io/en/latest/reference/study.html#optuna.study.Study.enqueue_trial). An extra indent appears after the second paragraph.\r\n\r\n![image](https://user-images.githubusercontent.com/1061922/81144389-e6f32f80-8fae-11ea-8dd2-33368293dafa.png)\r\n\r\n## Expected behavior\r\nno extra indent is shown after the second paragraph\r\n\r\nFor example, the docs of [`Study.get_trials`](https://optuna.readthedocs.io/en/latest/reference/study.html#optuna.study.Study.get_trials), which does not have the `experimental` decorator, appears as expected.\r\n\r\n![image](https://user-images.githubusercontent.com/1061922/81143489-f1142e80-8fac-11ea-9896-e56086228168.png)\r\n\r\nThe following figure shows another example. The docs of the [`train`](https://optuna.readthedocs.io/en/latest/reference/integration.html#optuna.integration.lightgbm.train) function of LightGBMTuner has no extra indent. Although the function has an `experimental` decorator, it belongs to the top level classes.\r\n\r\n![image](https://user-images.githubusercontent.com/1061922/81146606-98945f80-8fb3-11ea-8d67-5cf1cfbf8768.png)\r\n\n", "before_files": [{"content": "import functools\nimport inspect\nfrom typing import Any\nfrom typing import Callable\nimport warnings\n\nfrom optuna.exceptions import ExperimentalWarning\n\n\n# White spaces of each line are necessary to beautifully rendered documentation.\n# NOTE(crcrpar): When `experimental` decorator is applied to member methods, these lines require\n# another four spaces.\n_EXPERIMENTAL_DOCSTRING_TEMPLATE = \"\"\"\n\n    .. note::\n        Added in v{ver} as an experimental feature. The interface may change in newer versions\n        without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.\n\"\"\"\n\n\ndef _validate_version(version: str) -> None:\n\n    if not isinstance(version, str) or len(version.split(\".\")) != 3:\n        raise ValueError(\n            \"Invalid version specification. Must follow `x.y.z` format but `{}` is given\".format(\n                version\n            )\n        )\n\n\ndef experimental(version: str, name: str = None) -> Any:\n    \"\"\"Decorate class or function as experimental.\n\n    Args:\n        version: The first version that supports the target feature.\n        name: The name of the feature. Defaults to the function or class name. Optional.\n    \"\"\"\n\n    _validate_version(version)\n\n    def _experimental_wrapper(f: Any) -> Any:\n        # f is either func or class.\n\n        def _experimental_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]:\n\n            docstring = _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)\n            if func.__doc__ is None:\n                func.__doc__ = \"\"\n            func.__doc__ += docstring\n\n            # TODO(crcrpar): Annotate this correctly.\n            @functools.wraps(func)\n            def new_func(*args: Any, **kwargs: Any) -> Any:\n                \"\"\"Wrapped function.\"\"\"\n\n                warnings.warn(\n                    \"{} is experimental (supported from v{}). \"\n                    \"The interface can change in the future.\".format(\n                        name if name is not None else func.__name__, version\n                    ),\n                    ExperimentalWarning,\n                )\n\n                return func(*args, **kwargs)  # type: ignore\n\n            return new_func\n\n        def _experimental_class(cls: Any) -> Any:\n            \"\"\"Decorates a class as experimental.\n\n            This decorator is supposed to be applied to the experimental class.\n            \"\"\"\n\n            _original_init = cls.__init__\n\n            @functools.wraps(_original_init)\n            def wrapped_init(self, *args, **kwargs) -> None:  # type: ignore\n                warnings.warn(\n                    \"{} is experimental (supported from v{}). \"\n                    \"The interface can change in the future.\".format(\n                        name if name is not None else cls.__name__, version\n                    ),\n                    ExperimentalWarning,\n                )\n\n                _original_init(self, *args, **kwargs)\n\n            cls.__init__ = wrapped_init\n\n            if cls.__doc__ is None:\n                cls.__doc__ = \"\"\n            cls.__doc__ += _EXPERIMENTAL_DOCSTRING_TEMPLATE.format(ver=version)\n            return cls\n\n        return _experimental_class(f) if inspect.isclass(f) else _experimental_func(f)\n\n    return _experimental_wrapper\n", "path": "optuna/_experimental.py"}], "after_files": [{"content": "import functools\nimport inspect\nimport textwrap\nfrom typing import Any\nfrom typing import Callable\nimport warnings\n\nfrom optuna.exceptions import ExperimentalWarning\n\n\n_EXPERIMENTAL_NOTE_TEMPLATE = \"\"\"\n\n.. note::\n    Added in v{ver} as an experimental feature. The interface may change in newer versions\n    without prior notice. See https://github.com/optuna/optuna/releases/tag/v{ver}.\n\"\"\"\n\n\ndef _validate_version(version: str) -> None:\n\n    if not isinstance(version, str) or len(version.split(\".\")) != 3:\n        raise ValueError(\n            \"Invalid version specification. Must follow `x.y.z` format but `{}` is given\".format(\n                version\n            )\n        )\n\n\ndef _get_docstring_indent(docstring: str) -> str:\n    return docstring.split(\"\\n\")[-1] if \"\\n\" in docstring else \"\"\n\n\ndef experimental(version: str, name: str = None) -> Any:\n    \"\"\"Decorate class or function as experimental.\n\n    Args:\n        version: The first version that supports the target feature.\n        name: The name of the feature. Defaults to the function or class name. Optional.\n    \"\"\"\n\n    _validate_version(version)\n\n    def _experimental_wrapper(f: Any) -> Any:\n        # f is either func or class.\n\n        def _experimental_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]:\n\n            if func.__doc__ is None:\n                func.__doc__ = \"\"\n\n            note = _EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)\n            indent = _get_docstring_indent(func.__doc__)\n            func.__doc__ = func.__doc__.strip() + textwrap.indent(note, indent) + indent\n\n            # TODO(crcrpar): Annotate this correctly.\n            @functools.wraps(func)\n            def new_func(*args: Any, **kwargs: Any) -> Any:\n                warnings.warn(\n                    \"{} is experimental (supported from v{}). \"\n                    \"The interface can change in the future.\".format(\n                        name if name is not None else func.__name__, version\n                    ),\n                    ExperimentalWarning,\n                )\n\n                return func(*args, **kwargs)  # type: ignore\n\n            return new_func\n\n        def _experimental_class(cls: Any) -> Any:\n            \"\"\"Decorates a class as experimental.\n\n            This decorator is supposed to be applied to the experimental class.\n            \"\"\"\n            _original_init = cls.__init__\n\n            @functools.wraps(_original_init)\n            def wrapped_init(self, *args, **kwargs) -> None:  # type: ignore\n                warnings.warn(\n                    \"{} is experimental (supported from v{}). \"\n                    \"The interface can change in the future.\".format(\n                        name if name is not None else cls.__name__, version\n                    ),\n                    ExperimentalWarning,\n                )\n\n                _original_init(self, *args, **kwargs)\n\n            cls.__init__ = wrapped_init\n\n            if cls.__doc__ is None:\n                cls.__doc__ = \"\"\n\n            note = _EXPERIMENTAL_NOTE_TEMPLATE.format(ver=version)\n            indent = _get_docstring_indent(cls.__doc__)\n            cls.__doc__ = cls.__doc__.strip() + textwrap.indent(note, indent) + indent\n\n            return cls\n\n        return _experimental_class(f) if inspect.isclass(f) else _experimental_func(f)\n\n    return _experimental_wrapper\n", "path": "optuna/_experimental.py"}]}