Repository: pandas-dev/pandas

Issue:
pandas is no longer importable with -OO optimization
#### Code Sample, a copy-pastable example if possible

In your shell:
```
$ python -OO -c 'import pandas'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/__init__.py", line 42, in <module>
    from pandas.core.api import *
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/api.py", line 10, in <module>
    from pandas.core.groupby.groupby import Grouper
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/groupby/__init__.py", line 2, in <module>
    from pandas.core.groupby.groupby import (
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 46, in <module>
    from pandas.core.index import (Index, MultiIndex,
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/index.py", line 2, in <module>
    from pandas.core.indexes.api import *
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/indexes/api.py", line 4, in <module>
    from pandas.core.indexes.base import (Index,
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 7, in <module>
    from pandas._libs import (lib, index as libindex, tslib as libts,
  File "pandas/_libs/index.pyx", line 28, in init pandas._libs.index
  File "pandas/_libs/tslibs/period.pyx", line 59, in init pandas._libs.tslibs.period
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/tseries/offsets.py", line 1092, in <module>
    class CustomBusinessMonthEnd(_CustomBusinessMonth):
  File "/Users/shoyer/miniconda3/envs/xarray-py36/lib/python3.6/site-packages/pandas/tseries/offsets.py", line 1093, in CustomBusinessMonthEnd
    __doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end')
AttributeError: 'NoneType' object has no attribute 'replace'
```

#### Problem description

`-OO` optimization strips out docstrings, which may give a minor performance boost (I honestly don't know). Nonetheless, users requested that xarray import properly properly with the `-OO` flag (https://github.com/pydata/xarray/issues/1706), so we added a regression test that caught this in the latest pandas release (https://github.com/pydata/xarray/pull/1708).

#### Expected Output

Pandas should be imported without any errors.

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.22.0
pytest: 3.5.0
pip: 9.0.1
setuptools: 39.0.1
Cython: None
numpy: 1.14.2
scipy: None
pyarrow: None
xarray: None
IPython: 6.3.0
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
</details>



Discussion:
Hmm OK. Just running locally it looks like it could be fixed adding a condition in 3 places that assume we have a docstring, but don't if you run with those flags.

Just out of curiosity do you only have this in your travis configuration or have you placed it somewhere in your unit testing? Think it would be easier to track if we had the latter but off the top of my head not sure how to do that - curious if you have any insight
You could probably test this with a Python subprocess, but I didn’t bother for xarray so it’s just in our Travis config.

Resolving patch:
diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py
--- a/pandas/tseries/offsets.py
+++ b/pandas/tseries/offsets.py
@@ -1090,12 +1090,17 @@ def apply(self, other):
 
 
 class CustomBusinessMonthEnd(_CustomBusinessMonth):
-    __doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end')
+    # TODO(py27): Replace condition with Subsitution after dropping Py27
+    if _CustomBusinessMonth.__doc__:
+        __doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end')
     _prefix = 'CBM'
 
 
 class CustomBusinessMonthBegin(_CustomBusinessMonth):
-    __doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'beginning')
+    # TODO(py27): Replace condition with Subsitution after dropping Py27
+    if _CustomBusinessMonth.__doc__:
+        __doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]',
+                                                       'beginning')
     _prefix = 'CBMS'
 
 
diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py
--- a/pandas/util/_decorators.py
+++ b/pandas/util/_decorators.py
@@ -4,7 +4,7 @@
 import types
 import warnings
 from textwrap import dedent, wrap
-from functools import wraps, update_wrapper
+from functools import wraps, update_wrapper, WRAPPER_ASSIGNMENTS
 
 
 def deprecate(name, alternative, version, alt_name=None,
@@ -20,18 +20,18 @@ def deprecate(name, alternative, version, alt_name=None,
     Parameters
     ----------
     name : str
-        Name of function to deprecate
-    alternative : str
-        Name of function to use instead
+        Name of function to deprecate.
+    alternative : func
+        Function to use instead.
     version : str
-        Version of pandas in which the method has been deprecated
+        Version of pandas in which the method has been deprecated.
     alt_name : str, optional
-        Name to use in preference of alternative.__name__
+        Name to use in preference of alternative.__name__.
     klass : Warning, default FutureWarning
     stacklevel : int, default 2
     msg : str
-          The message to display in the warning.
-          Default is '{name} is deprecated. Use {alt_name} instead.'
+        The message to display in the warning.
+        Default is '{name} is deprecated. Use {alt_name} instead.'
     """
 
     alt_name = alt_name or alternative.__name__
@@ -39,25 +39,26 @@ def deprecate(name, alternative, version, alt_name=None,
     warning_msg = msg or '{} is deprecated, use {} instead'.format(name,
                                                                    alt_name)
 
-    @wraps(alternative)
+    # adding deprecated directive to the docstring
+    msg = msg or 'Use `{alt_name}` instead.'.format(alt_name=alt_name)
+    msg = '\n    '.join(wrap(msg, 70))
+
+    @Substitution(version=version, msg=msg)
+    @Appender(alternative.__doc__)
     def wrapper(*args, **kwargs):
+        """
+        .. deprecated:: %(version)s
+
+           %(msg)s
+
+        """
         warnings.warn(warning_msg, klass, stacklevel=stacklevel)
         return alternative(*args, **kwargs)
 
-    # adding deprecated directive to the docstring
-    msg = msg or 'Use `{alt_name}` instead.'.format(alt_name=alt_name)
-    tpl = dedent("""
-    .. deprecated:: {version}
-
-       {msg}
-
-    {rest}
-    """)
-    rest = getattr(wrapper, '__doc__', '')
-    docstring = tpl.format(version=version,
-                           msg='\n    '.join(wrap(msg, 70)),
-                           rest=dedent(rest))
-    wrapper.__doc__ = docstring
+    # Since we are using Substitution to create the required docstring,
+    # remove that from the attributes that should be assigned to the wrapper
+    assignments = tuple(x for x in WRAPPER_ASSIGNMENTS if x != '__doc__')
+    update_wrapper(wrapper, alternative, assigned=assignments)
 
     return wrapper
 


Test patch:
diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py
--- a/pandas/tests/test_downstream.py
+++ b/pandas/tests/test_downstream.py
@@ -2,6 +2,8 @@
 """
 Testing that we work in the downstream packages
 """
+import subprocess
+
 import pytest
 import numpy as np  # noqa
 from pandas import DataFrame
@@ -53,6 +55,11 @@ def test_xarray(df):
     assert df.to_xarray() is not None
 
 
+def test_oo_optimizable():
+    # GH 21071
+    subprocess.check_call(["python", "-OO", "-c", "import pandas"])
+
+
 @tm.network
 def test_statsmodels():